1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 import sys, os, re, string, time, types
39 import Tkinter
40
41
42 import tkinit
43 from tkconst import tkversion
44 from tkfont import getFont
45
46
47
48
49
50
53
55 need_fix = os.name == "nt" and tkversion < (8, 3, 0, 0)
56 if need_fix:
57
58 try:
59 window.wm_iconify()
60 window.update_idletasks()
61 except Tkinter.TclError:
62
63 pass
64 window.wm_deiconify()
65
66 -def wm_map(window, maximized=0):
67 if window.wm_state() != "iconic":
68 if maximized and os.name == "nt":
69 window.wm_state("zoomed")
70 else:
71 wm_deiconify(window)
72
74 if not filename:
75 return
76 if os.name == "posix":
77 window.wm_iconbitmap("@" + filename)
78 window.wm_iconmask("@" + filename)
79
80 __wm_get_geometry_re = re.compile(r"^(\d+)x(\d+)\+([\-]?\d+)\+([\-]?\d+)$")
81
83 g = window.wm_geometry()
84 m = __wm_get_geometry_re.search(g)
85 if not m:
86 raise Tkinter.TclError, "invalid geometry " + str(g)
87 l = map(int, m.groups())
88 if window.wm_state() == "zoomed":
89
90 l[2] = l[3] = 0
91 return l
92
93
94
95
96
97
98 -def setTransient(window, parent, relx=None, rely=None, expose=1):
99
100
101
102
103
104
105
106 window.wm_withdraw()
107 window.wm_group(parent)
108 need_fix = os.name == "nt" and tkversion < (8, 3, 0, 0)
109 if need_fix:
110
111 window.wm_geometry("+%d+%d" % (-10000, -10000))
112 if expose and parent is not None:
113
114 window.wm_iconify()
115 if parent and parent.wm_state() != "withdrawn":
116 window.wm_transient(parent)
117
118 window.update_idletasks()
119
120 x, y = __getWidgetXY(window, parent, relx=relx, rely=rely)
121 if need_fix:
122 if expose:
123 wm_deiconify(window)
124 window.wm_geometry("+%d+%d" % (x, y))
125 else:
126 window.wm_geometry("+%d+%d" % (x, y))
127 if expose:
128 window.wm_deiconify()
129
131
132
133
134
135 if class_:
136 window = Tkinter.Toplevel(parent, class_=class_)
137 else:
138 window = Tkinter.Toplevel(parent)
139
140
141 if os.name == "posix":
142 window.wm_command("/bin/true")
143
144 if title:
145 window.wm_title(title)
146 window.wm_iconname(title)
147 return window
148
149 makeHelpToplevel = makeToplevel
150
194
195
196
197
198
199
200 __mfx_bindings = {}
201 __mfx_wm_protocols = ("WM_DELETE_WINDOW", "WM_TAKE_FOCUS", "WM_SAVE_YOURSELF")
202
203 -def bind(widget, sequence, func, add=None):
204 assert callable(func)
205 if sequence in __mfx_wm_protocols:
206 funcid = widget._register(func)
207 widget.tk.call("wm", "protocol", widget._w, sequence, funcid)
208 elif add is None:
209 funcid = widget.bind(sequence, func)
210 else:
211
212 funcid = widget.bind(sequence, func, add)
213 k = id(widget)
214 if __mfx_bindings.has_key(k):
215 __mfx_bindings[k].append((sequence, funcid))
216 else:
217 __mfx_bindings[k] = [(sequence, funcid)]
218
220 if widget is None:
221 return
222 k = id(widget)
223 if __mfx_bindings.has_key(k):
224 for sequence, funcid in __mfx_bindings[k]:
225
226 try:
227 if sequence in __mfx_wm_protocols:
228 widget.tk.call("wm", "protocol", widget._w, sequence, "")
229
230 else:
231 widget.unbind(sequence, funcid)
232 except Tkinter.TclError:
233 pass
234 del __mfx_bindings[k]
235
236
237
238
239
240
241
242
243 -def after(widget, ms, func, *args):
244 timer = apply(widget.after, (ms, func) + args)
245 command = widget._tclCommands[-1]
246 return (timer, command, widget)
247
249 return apply(after, (widget, "idle", func) + args)
250
252 if t is not None:
253 t[2].after_cancel(t[0])
254 try:
255 t[2].deletecommand(t[1])
256 except Tkinter.TclError:
257 pass
258
259
260
261
262
263
264 -def makeImage(file=None, data=None, dither=None, alpha=None):
265 kw = {}
266 if data is None:
267 assert file is not None
268 kw["file"] = file
269 else:
270 assert data is not None
271 kw["data"] = data
272 if os.name == "nt":
273 if dither is not None:
274 kw["dither"] = dither
275 if alpha is not None:
276 kw["alpha"] = alpha
277 return apply(Tkinter.PhotoImage, (), kw)
278
279 loadImage = makeImage
280
282 dest = Tkinter.PhotoImage(width=width, height=height)
283 assert dest.width() == width
284 assert dest.height() == height
285 dest.blank()
286 image.tk.call(dest, "copy", image.name, "-from", x, y, x+width, y+height)
287 assert dest.width() == width
288 assert dest.height() == height
289 return dest
290
292 if not fill and not outline:
293 return
294 width = image.width()
295 height = image.height()
296 ow = 1
297 if width <= 2*ow or height <= 2*ow:
298 fill = fill or outline
299 outline = None
300 if not outline:
301 f = (fill,) * width
302 f = (f,) * height
303 assert len(f) == height
304 image.put(f)
305 elif not fill:
306 l = ((outline,) * width,)
307 for y in range(0, ow):
308 image.put(l, (0, y))
309 for y in range(height-ow, height):
310 image.put(l, (0, y))
311 p = ((outline,) * ow,)
312 for y in range(ow, height-ow):
313 image.put(p, (0, y))
314 image.put(p, (width-ow, y))
315 else:
316 l1 = (outline,) * width
317 l2 = (outline,) * ow + (fill,) * (width-2*ow) + (outline,) * ow
318 f = (l1,) * ow + (l2,) * (height-2*ow) + (l1,) * ow
319 assert len(f) == height
320 image.put(f)
321
323 image = Tkinter.PhotoImage(width=width, height=height)
324 assert image.width() == width
325 assert image.height() == height
326 image.blank()
327 fillImage(image, fill, outline)
328 return image
329