"""Search dialog for Find, Find Again, and Find Selectionfunctionality.Inherits from SearchDialogBase for GUI and uses searchengineto prepare search pattern."""from tkinter import TclErrorfrom idlelib import searchenginefrom idlelib.searchbase import SearchDialogBasedef _setup(text):"""Return the new or existing singleton SearchDialog instance.The singleton dialog saves user entries and preferencesacross instances.Args:text: Text widget containing the text to be searched."""root = text._root()engine = searchengine.get(root)if not hasattr(engine, "_searchdialog"):engine._searchdialog = SearchDialog(root, engine)return engine._searchdialogdef find(text):"""Open the search dialog.Module-level function to access the singleton SearchDialoginstance and open the dialog. If text is selected, it isused as the search phrase; otherwise, the previous entryis used. No search is done with this command."""pat = text.get("sel.first", "sel.last")return _setup(text).open(text, pat) # Open is inherited from SDBase.def find_again(text):"""Repeat the search for the last pattern and preferences.Module-level function to access the singleton SearchDialoginstance to search again using the user entries and preferencesfrom the last dialog. If there was no prior search, open thesearch dialog; otherwise, perform the search without showing thedialog."""return _setup(text).find_again(text)def find_selection(text):"""Search for the selected pattern in the text.Module-level function to access the singleton SearchDialoginstance to search using the selected text. With a textselection, perform the search without displaying the dialog.Without a selection, use the prior entry as the search phraseand don't display the dialog. If there has been no priorsearch, open the search dialog."""return _setup(text).find_selection(text)class SearchDialog(SearchDialogBase):"Dialog for finding a pattern in text."def create_widgets(self):"Create the base search dialog and add a button for Find Next."SearchDialogBase.create_widgets(self)# TODO - why is this here and not in a create_command_buttons?self.make_button("Find Next", self.default_command, isdef=True)def default_command(self, event=None):"Handle the Find Next button as the default command."if not self.engine.getprog():returnself.find_again(self.text)def find_again(self, text):"""Repeat the last search.If no search was previously run, open a new search dialog. Inthis case, no search is done.If a search was previously run, the search dialog won't beshown and the options from the previous search (including thesearch pattern) will be used to find the next occurrenceof the pattern. Next is relative based on direction.Position the window to display the located occurrence in thetext.Return True if the search was successful and False otherwise."""if not self.engine.getpat():self.open(text)return Falseif not self.engine.getprog():return Falseres = self.engine.search_text(text)if res:line, m = resi, j = m.span()first = "%d.%d" % (line, i)last = "%d.%d" % (line, j)try:selfirst = text.index("sel.first")sellast = text.index("sel.last")if selfirst == first and sellast == last:self.bell()return Falseexcept TclError:passtext.tag_remove("sel", "1.0", "end")text.tag_add("sel", first, last)text.mark_set("insert", self.engine.isback() and first or last)text.see("insert")return Trueelse:self.bell()return Falsedef find_selection(self, text):"""Search for selected text with previous dialog preferences.Instead of using the same pattern for searching (as FindAgain does), this first resets the pattern to the currentlyselected text. If the selected text isn't changed, then usethe prior search phrase."""pat = text.get("sel.first", "sel.last")if pat:self.engine.setcookedpat(pat)return self.find_again(text)def _search_dialog(parent): # htest #"Display search test box."from tkinter import Toplevel, Textfrom tkinter.ttk import Frame, Buttontop = Toplevel(parent)top.title("Test SearchDialog")x, y = map(int, parent.geometry().split('+')[1:])top.geometry("+%d+%d" % (x, y + 175))frame = Frame(top)frame.pack()text = Text(frame, inactiveselectbackground='gray')text.pack()text.insert("insert","This is a sample string.\n"*5)def show_find():text.tag_add('sel', '1.0', 'end')_setup(text).open(text)text.tag_remove('sel', '1.0', 'end')button = Button(frame, text="Search (selection ignored)", command=show_find)button.pack()if __name__ == '__main__':from unittest import mainmain('idlelib.idle_test.test_search', verbosity=2, exit=False)from idlelib.idle_test.htest import runrun(_search_dialog)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。