annotate Tools/webchecker/tktools.py @ 5817:48b94de85bf3 legacy-trunk

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