/Tools/webchecker/tktools.py

http://unladen-swallow.googlecode.com/ · Python · 366 lines · 237 code · 54 blank · 75 comment · 36 complexity · 22ab51512a419b72da9b1f9ea0635ff5 MD5 · raw file

  1. """Assorted Tk-related subroutines used in Grail."""
  2. from types import *
  3. from Tkinter import *
  4. def _clear_entry_widget(event):
  5. try:
  6. widget = event.widget
  7. widget.delete(0, INSERT)
  8. except: pass
  9. def install_keybindings(root):
  10. root.bind_class('Entry', '<Control-u>', _clear_entry_widget)
  11. def make_toplevel(master, title=None, class_=None):
  12. """Create a Toplevel widget.
  13. This is a shortcut for a Toplevel() instantiation plus calls to
  14. set the title and icon name of the widget.
  15. """
  16. if class_:
  17. widget = Toplevel(master, class_=class_)
  18. else:
  19. widget = Toplevel(master)
  20. if title:
  21. widget.title(title)
  22. widget.iconname(title)
  23. return widget
  24. def set_transient(widget, master, relx=0.5, rely=0.3, expose=1):
  25. """Make an existing toplevel widget transient for a master.
  26. The widget must exist but should not yet have been placed; in
  27. other words, this should be called after creating all the
  28. subwidget but before letting the user interact.
  29. """
  30. widget.withdraw() # Remain invisible while we figure out the geometry
  31. widget.transient(master)
  32. widget.update_idletasks() # Actualize geometry information
  33. if master.winfo_ismapped():
  34. m_width = master.winfo_width()
  35. m_height = master.winfo_height()
  36. m_x = master.winfo_rootx()
  37. m_y = master.winfo_rooty()
  38. else:
  39. m_width = master.winfo_screenwidth()
  40. m_height = master.winfo_screenheight()
  41. m_x = m_y = 0
  42. w_width = widget.winfo_reqwidth()
  43. w_height = widget.winfo_reqheight()
  44. x = m_x + (m_width - w_width) * relx
  45. y = m_y + (m_height - w_height) * rely
  46. widget.geometry("+%d+%d" % (x, y))
  47. if expose:
  48. widget.deiconify() # Become visible at the desired location
  49. return widget
  50. def make_scrollbars(parent, hbar, vbar, pack=1, class_=None, name=None,
  51. takefocus=0):
  52. """Subroutine to create a frame with scrollbars.
  53. This is used by make_text_box and similar routines.
  54. Note: the caller is responsible for setting the x/y scroll command
  55. properties (e.g. by calling set_scroll_commands()).
  56. Return a tuple containing the hbar, the vbar, and the frame, where
  57. hbar and vbar are None if not requested.
  58. """
  59. if class_:
  60. if name: frame = Frame(parent, class_=class_, name=name)
  61. else: frame = Frame(parent, class_=class_)
  62. else:
  63. if name: frame = Frame(parent, name=name)
  64. else: frame = Frame(parent)
  65. if pack:
  66. frame.pack(fill=BOTH, expand=1)
  67. corner = None
  68. if vbar:
  69. if not hbar:
  70. vbar = Scrollbar(frame, takefocus=takefocus)
  71. vbar.pack(fill=Y, side=RIGHT)
  72. else:
  73. vbarframe = Frame(frame, borderwidth=0)
  74. vbarframe.pack(fill=Y, side=RIGHT)
  75. vbar = Scrollbar(frame, name="vbar", takefocus=takefocus)
  76. vbar.pack(in_=vbarframe, expand=1, fill=Y, side=TOP)
  77. sbwidth = vbar.winfo_reqwidth()
  78. corner = Frame(vbarframe, width=sbwidth, height=sbwidth)
  79. corner.propagate(0)
  80. corner.pack(side=BOTTOM)
  81. else:
  82. vbar = None
  83. if hbar:
  84. hbar = Scrollbar(frame, orient=HORIZONTAL, name="hbar",
  85. takefocus=takefocus)
  86. hbar.pack(fill=X, side=BOTTOM)
  87. else:
  88. hbar = None
  89. return hbar, vbar, frame
  90. def set_scroll_commands(widget, hbar, vbar):
  91. """Link a scrollable widget to its scroll bars.
  92. The scroll bars may be empty.
  93. """
  94. if vbar:
  95. widget['yscrollcommand'] = (vbar, 'set')
  96. vbar['command'] = (widget, 'yview')
  97. if hbar:
  98. widget['xscrollcommand'] = (hbar, 'set')
  99. hbar['command'] = (widget, 'xview')
  100. widget.vbar = vbar
  101. widget.hbar = hbar
  102. def make_text_box(parent, width=0, height=0, hbar=0, vbar=1,
  103. fill=BOTH, expand=1, wrap=WORD, pack=1,
  104. class_=None, name=None, takefocus=None):
  105. """Subroutine to create a text box.
  106. Create:
  107. - a both-ways filling and expanding frame, containing:
  108. - a text widget on the left, and
  109. - possibly a vertical scroll bar on the right.
  110. - possibly a horizonta; scroll bar at the bottom.
  111. Return the text widget and the frame widget.
  112. """
  113. hbar, vbar, frame = make_scrollbars(parent, hbar, vbar, pack,
  114. class_=class_, name=name,
  115. takefocus=takefocus)
  116. widget = Text(frame, wrap=wrap, name="text")
  117. if width: widget.config(width=width)
  118. if height: widget.config(height=height)
  119. widget.pack(expand=expand, fill=fill, side=LEFT)
  120. set_scroll_commands(widget, hbar, vbar)
  121. return widget, frame
  122. def make_list_box(parent, width=0, height=0, hbar=0, vbar=1,
  123. fill=BOTH, expand=1, pack=1, class_=None, name=None,
  124. takefocus=None):
  125. """Subroutine to create a list box.
  126. Like make_text_box().
  127. """
  128. hbar, vbar, frame = make_scrollbars(parent, hbar, vbar, pack,
  129. class_=class_, name=name,
  130. takefocus=takefocus)
  131. widget = Listbox(frame, name="listbox")
  132. if width: widget.config(width=width)
  133. if height: widget.config(height=height)
  134. widget.pack(expand=expand, fill=fill, side=LEFT)
  135. set_scroll_commands(widget, hbar, vbar)
  136. return widget, frame
  137. def make_canvas(parent, width=0, height=0, hbar=1, vbar=1,
  138. fill=BOTH, expand=1, pack=1, class_=None, name=None,
  139. takefocus=None):
  140. """Subroutine to create a canvas.
  141. Like make_text_box().
  142. """
  143. hbar, vbar, frame = make_scrollbars(parent, hbar, vbar, pack,
  144. class_=class_, name=name,
  145. takefocus=takefocus)
  146. widget = Canvas(frame, scrollregion=(0, 0, width, height), name="canvas")
  147. if width: widget.config(width=width)
  148. if height: widget.config(height=height)
  149. widget.pack(expand=expand, fill=fill, side=LEFT)
  150. set_scroll_commands(widget, hbar, vbar)
  151. return widget, frame
  152. def make_form_entry(parent, label, borderwidth=None):
  153. """Subroutine to create a form entry.
  154. Create:
  155. - a horizontally filling and expanding frame, containing:
  156. - a label on the left, and
  157. - a text entry on the right.
  158. Return the entry widget and the frame widget.
  159. """
  160. frame = Frame(parent)
  161. frame.pack(fill=X)
  162. label = Label(frame, text=label)
  163. label.pack(side=LEFT)
  164. if borderwidth is None:
  165. entry = Entry(frame, relief=SUNKEN)
  166. else:
  167. entry = Entry(frame, relief=SUNKEN, borderwidth=borderwidth)
  168. entry.pack(side=LEFT, fill=X, expand=1)
  169. return entry, frame
  170. # This is a slightly modified version of the function above. This
  171. # version does the proper alighnment of labels with their fields. It
  172. # should probably eventually replace make_form_entry altogether.
  173. #
  174. # The one annoying bug is that the text entry field should be
  175. # expandable while still aligning the colons. This doesn't work yet.
  176. #
  177. def make_labeled_form_entry(parent, label, entrywidth=20, entryheight=1,
  178. labelwidth=0, borderwidth=None,
  179. takefocus=None):
  180. """Subroutine to create a form entry.
  181. Create:
  182. - a horizontally filling and expanding frame, containing:
  183. - a label on the left, and
  184. - a text entry on the right.
  185. Return the entry widget and the frame widget.
  186. """
  187. if label and label[-1] != ':': label = label + ':'
  188. frame = Frame(parent)
  189. label = Label(frame, text=label, width=labelwidth, anchor=E)
  190. label.pack(side=LEFT)
  191. if entryheight == 1:
  192. if borderwidth is None:
  193. entry = Entry(frame, relief=SUNKEN, width=entrywidth)
  194. else:
  195. entry = Entry(frame, relief=SUNKEN, width=entrywidth,
  196. borderwidth=borderwidth)
  197. entry.pack(side=RIGHT, expand=1, fill=X)
  198. frame.pack(fill=X)
  199. else:
  200. entry = make_text_box(frame, entrywidth, entryheight, 1, 1,
  201. takefocus=takefocus)
  202. frame.pack(fill=BOTH, expand=1)
  203. return entry, frame, label
  204. def make_double_frame(master=None, class_=None, name=None, relief=RAISED,
  205. borderwidth=1):
  206. """Create a pair of frames suitable for 'hosting' a dialog."""
  207. if name:
  208. if class_: frame = Frame(master, class_=class_, name=name)
  209. else: frame = Frame(master, name=name)
  210. else:
  211. if class_: frame = Frame(master, class_=class_)
  212. else: frame = Frame(master)
  213. top = Frame(frame, name="topframe", relief=relief,
  214. borderwidth=borderwidth)
  215. bottom = Frame(frame, name="bottomframe")
  216. bottom.pack(fill=X, padx='1m', pady='1m', side=BOTTOM)
  217. top.pack(expand=1, fill=BOTH, padx='1m', pady='1m')
  218. frame.pack(expand=1, fill=BOTH)
  219. top = Frame(top)
  220. top.pack(expand=1, fill=BOTH, padx='2m', pady='2m')
  221. return frame, top, bottom
  222. def make_group_frame(master, name=None, label=None, fill=Y,
  223. side=None, expand=None, font=None):
  224. """Create nested frames with a border and optional label.
  225. The outer frame is only used to provide the decorative border, to
  226. control packing, and to host the label. The inner frame is packed
  227. to fill the outer frame and should be used as the parent of all
  228. sub-widgets. Only the inner frame is returned.
  229. """
  230. font = font or "-*-helvetica-medium-r-normal-*-*-100-*-*-*-*-*-*"
  231. outer = Frame(master, borderwidth=2, relief=GROOVE)
  232. outer.pack(expand=expand, fill=fill, side=side)
  233. if label:
  234. Label(outer, text=label, font=font, anchor=W).pack(fill=X)
  235. inner = Frame(master, borderwidth='1m', name=name)
  236. inner.pack(expand=1, fill=BOTH, in_=outer)
  237. inner.forget = outer.forget
  238. return inner
  239. def unify_button_widths(*buttons):
  240. """Make buttons passed in all have the same width.
  241. Works for labels and other widgets with the 'text' option.
  242. """
  243. wid = 0
  244. for btn in buttons:
  245. wid = max(wid, len(btn["text"]))
  246. for btn in buttons:
  247. btn["width"] = wid
  248. def flatten(msg):
  249. """Turn a list or tuple into a single string -- recursively."""
  250. t = type(msg)
  251. if t in (ListType, TupleType):
  252. msg = ' '.join(map(flatten, msg))
  253. elif t is ClassType:
  254. msg = msg.__name__
  255. else:
  256. msg = str(msg)
  257. return msg
  258. def boolean(s):
  259. """Test whether a string is a Tk boolean, without error checking."""
  260. if s.lower() in ('', '0', 'no', 'off', 'false'): return 0
  261. else: return 1
  262. def test():
  263. """Test make_text_box(), make_form_entry(), flatten(), boolean()."""
  264. import sys
  265. root = Tk()
  266. entry, eframe = make_form_entry(root, 'Boolean:')
  267. text, tframe = make_text_box(root)
  268. def enter(event, entry=entry, text=text):
  269. s = boolean(entry.get()) and '\nyes' or '\nno'
  270. text.insert('end', s)
  271. entry.bind('<Return>', enter)
  272. entry.insert(END, flatten(sys.argv))
  273. root.mainloop()
  274. if __name__ == '__main__':
  275. test()