searchbase.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. '''Define SearchDialogBase used by Search, Replace, and Grep dialogs.'''
  2. from tkinter import Toplevel
  3. from tkinter.ttk import Frame, Entry, Label, Button, Checkbutton, Radiobutton
  4. from tkinter.simpledialog import _setup_dialog
  5. class SearchDialogBase:
  6. '''Create most of a 3 or 4 row, 3 column search dialog.
  7. The left and wide middle column contain:
  8. 1 or 2 labeled text entry lines (make_entry, create_entries);
  9. a row of standard Checkbuttons (make_frame, create_option_buttons),
  10. each of which corresponds to a search engine Variable;
  11. a row of dialog-specific Check/Radiobuttons (create_other_buttons).
  12. The narrow right column contains command buttons
  13. (make_button, create_command_buttons).
  14. These are bound to functions that execute the command.
  15. Except for command buttons, this base class is not limited to items
  16. common to all three subclasses. Rather, it is the Find dialog minus
  17. the "Find Next" command, its execution function, and the
  18. default_command attribute needed in create_widgets. The other
  19. dialogs override attributes and methods, the latter to replace and
  20. add widgets.
  21. '''
  22. title = "Search Dialog" # replace in subclasses
  23. icon = "Search"
  24. needwrapbutton = 1 # not in Find in Files
  25. def __init__(self, root, engine):
  26. '''Initialize root, engine, and top attributes.
  27. top (level widget): set in create_widgets() called from open().
  28. frame: container for all widgets in dialog.
  29. text (Text searched): set in open(), only used in subclasses().
  30. ent (ry): created in make_entry() called from create_entry().
  31. row (of grid): 0 in create_widgets(), +1 in make_entry/frame().
  32. default_command: set in subclasses, used in create_widgets().
  33. title (of dialog): class attribute, override in subclasses.
  34. icon (of dialog): ditto, use unclear if cannot minimize dialog.
  35. '''
  36. self.root = root
  37. self.bell = root.bell
  38. self.engine = engine
  39. self.top = None
  40. def open(self, text, searchphrase=None):
  41. "Make dialog visible on top of others and ready to use."
  42. self.text = text
  43. if not self.top:
  44. self.create_widgets()
  45. else:
  46. self.top.deiconify()
  47. self.top.tkraise()
  48. self.top.transient(text.winfo_toplevel())
  49. if searchphrase:
  50. self.ent.delete(0,"end")
  51. self.ent.insert("end",searchphrase)
  52. self.ent.focus_set()
  53. self.ent.selection_range(0, "end")
  54. self.ent.icursor(0)
  55. self.top.grab_set()
  56. def close(self, event=None):
  57. "Put dialog away for later use."
  58. if self.top:
  59. self.top.grab_release()
  60. self.top.transient('')
  61. self.top.withdraw()
  62. def create_widgets(self):
  63. '''Create basic 3 row x 3 col search (find) dialog.
  64. Other dialogs override subsidiary create_x methods as needed.
  65. Replace and Find-in-Files add another entry row.
  66. '''
  67. top = Toplevel(self.root)
  68. top.bind("<Return>", self.default_command)
  69. top.bind("<Escape>", self.close)
  70. top.protocol("WM_DELETE_WINDOW", self.close)
  71. top.wm_title(self.title)
  72. top.wm_iconname(self.icon)
  73. _setup_dialog(top)
  74. self.top = top
  75. self.frame = Frame(top, padding="5px")
  76. self.frame.grid(sticky="nwes")
  77. top.grid_columnconfigure(0, weight=100)
  78. top.grid_rowconfigure(0, weight=100)
  79. self.row = 0
  80. self.frame.grid_columnconfigure(0, pad=2, weight=0)
  81. self.frame.grid_columnconfigure(1, pad=2, minsize=100, weight=100)
  82. self.create_entries() # row 0 (and maybe 1), cols 0, 1
  83. self.create_option_buttons() # next row, cols 0, 1
  84. self.create_other_buttons() # next row, cols 0, 1
  85. self.create_command_buttons() # col 2, all rows
  86. def make_entry(self, label_text, var):
  87. '''Return (entry, label), .
  88. entry - gridded labeled Entry for text entry.
  89. label - Label widget, returned for testing.
  90. '''
  91. label = Label(self.frame, text=label_text)
  92. label.grid(row=self.row, column=0, sticky="nw")
  93. entry = Entry(self.frame, textvariable=var, exportselection=0)
  94. entry.grid(row=self.row, column=1, sticky="nwe")
  95. self.row = self.row + 1
  96. return entry, label
  97. def create_entries(self):
  98. "Create one or more entry lines with make_entry."
  99. self.ent = self.make_entry("Find:", self.engine.patvar)[0]
  100. def make_frame(self,labeltext=None):
  101. '''Return (frame, label).
  102. frame - gridded labeled Frame for option or other buttons.
  103. label - Label widget, returned for testing.
  104. '''
  105. if labeltext:
  106. label = Label(self.frame, text=labeltext)
  107. label.grid(row=self.row, column=0, sticky="nw")
  108. else:
  109. label = ''
  110. frame = Frame(self.frame)
  111. frame.grid(row=self.row, column=1, columnspan=1, sticky="nwe")
  112. self.row = self.row + 1
  113. return frame, label
  114. def create_option_buttons(self):
  115. '''Return (filled frame, options) for testing.
  116. Options is a list of searchengine booleanvar, label pairs.
  117. A gridded frame from make_frame is filled with a Checkbutton
  118. for each pair, bound to the var, with the corresponding label.
  119. '''
  120. frame = self.make_frame("Options")[0]
  121. engine = self.engine
  122. options = [(engine.revar, "Regular expression"),
  123. (engine.casevar, "Match case"),
  124. (engine.wordvar, "Whole word")]
  125. if self.needwrapbutton:
  126. options.append((engine.wrapvar, "Wrap around"))
  127. for var, label in options:
  128. btn = Checkbutton(frame, variable=var, text=label)
  129. btn.pack(side="left", fill="both")
  130. return frame, options
  131. def create_other_buttons(self):
  132. '''Return (frame, others) for testing.
  133. Others is a list of value, label pairs.
  134. A gridded frame from make_frame is filled with radio buttons.
  135. '''
  136. frame = self.make_frame("Direction")[0]
  137. var = self.engine.backvar
  138. others = [(1, 'Up'), (0, 'Down')]
  139. for val, label in others:
  140. btn = Radiobutton(frame, variable=var, value=val, text=label)
  141. btn.pack(side="left", fill="both")
  142. return frame, others
  143. def make_button(self, label, command, isdef=0):
  144. "Return command button gridded in command frame."
  145. b = Button(self.buttonframe,
  146. text=label, command=command,
  147. default=isdef and "active" or "normal")
  148. cols,rows=self.buttonframe.grid_size()
  149. b.grid(pady=1,row=rows,column=0,sticky="ew")
  150. self.buttonframe.grid(rowspan=rows+1)
  151. return b
  152. def create_command_buttons(self):
  153. "Place buttons in vertical command frame gridded on right."
  154. f = self.buttonframe = Frame(self.frame)
  155. f.grid(row=0,column=2,padx=2,pady=2,ipadx=2,ipady=2)
  156. b = self.make_button("Close", self.close)
  157. b.lower()
  158. class _searchbase(SearchDialogBase): # htest #
  159. "Create auto-opening dialog with no text connection."
  160. def __init__(self, parent):
  161. import re
  162. from idlelib import searchengine
  163. self.root = parent
  164. self.engine = searchengine.get(parent)
  165. self.create_widgets()
  166. print(parent.geometry())
  167. width,height, x,y = list(map(int, re.split('[x+]', parent.geometry())))
  168. self.top.geometry("+%d+%d" % (x + 40, y + 175))
  169. def default_command(self, dummy): pass
  170. if __name__ == '__main__':
  171. from unittest import main
  172. main('idlelib.idle_test.test_searchbase', verbosity=2, exit=False)
  173. from idlelib.idle_test.htest import run
  174. run(_searchbase)