blob: 523db9e5cf0062690ce9bc1e084e69ec5c711dca [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# Tkinter.py -- Tk/Tcl widget wrappers
Guido van Rossum2dcf5291994-07-06 09:23:20 +00002
Guido van Rossum37dcab11996-05-16 16:00:19 +00003__version__ = "$Revision$"
4
Guido van Rossum95806091997-02-15 18:33:24 +00005import _tkinter # If this fails your Python is not configured for Tk
6tkinter = _tkinter # b/w compat for export
7TclError = _tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +00008from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +00009from Tkconstants import *
Guido van Rossum37dcab11996-05-16 16:00:19 +000010import string; _string = string; del string
Guido van Rossumf0c891a1998-04-29 21:43:36 +000011try:
12 import MacOS; _MacOS = MacOS; del MacOS
13except ImportError:
14 _MacOS = None
Guido van Rossum18468821994-06-20 07:49:28 +000015
Guido van Rossum95806091997-02-15 18:33:24 +000016TkVersion = _string.atof(_tkinter.TK_VERSION)
17TclVersion = _string.atof(_tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000018
Guido van Rossumd6615ab1997-08-05 02:35:01 +000019READABLE = _tkinter.READABLE
20WRITABLE = _tkinter.WRITABLE
21EXCEPTION = _tkinter.EXCEPTION
Guido van Rossumf53c86c1997-08-14 14:15:54 +000022
23# These are not always defined, e.g. not on Win32 with Tk 8.0 :-(
24try: _tkinter.createfilehandler
25except AttributeError: _tkinter.createfilehandler = None
26try: _tkinter.deletefilehandler
27except AttributeError: _tkinter.deletefilehandler = None
Guido van Rossum36269991996-05-16 17:11:27 +000028
29
Guido van Rossum2dcf5291994-07-06 09:23:20 +000030def _flatten(tuple):
31 res = ()
32 for item in tuple:
33 if type(item) in (TupleType, ListType):
34 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000035 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000036 res = res + (item,)
37 return res
38
39def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000040 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000041 return cnfs
42 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000043 return cnfs
44 else:
45 cnf = {}
46 for c in _flatten(cnfs):
Guido van Rossum65c78e11997-07-19 20:02:04 +000047 try:
48 cnf.update(c)
49 except (AttributeError, TypeError), msg:
50 print "_cnfmerge: fallback due to:", msg
51 for k, v in c.items():
52 cnf[k] = v
Guido van Rossum2dcf5291994-07-06 09:23:20 +000053 return cnf
54
55class Event:
56 pass
57
Guido van Rossumc4570481998-03-20 20:45:49 +000058_support_default_root = 1
Guido van Rossumaec5dc91994-06-27 07:55:12 +000059_default_root = None
60
Guido van Rossumc4570481998-03-20 20:45:49 +000061def NoDefaultRoot():
62 global _support_default_root
63 _support_default_root = 0
64 del _default_root
65
Guido van Rossum45853db1994-06-20 12:19:19 +000066def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000067 pass
68
Guido van Rossum97aeca11994-07-07 13:12:12 +000069def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000070 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000071
Guido van Rossumaec5dc91994-06-27 07:55:12 +000072_varnum = 0
73class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000074 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000075 def __init__(self, master=None):
Guido van Rossumaec5dc91994-06-27 07:55:12 +000076 global _varnum
Guido van Rossume2c6e201998-01-14 16:44:34 +000077 if not master:
78 master = _default_root
79 self._master = master
80 self._tk = master.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +000081 self._name = 'PY_VAR' + `_varnum`
82 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000083 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000084 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000085 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000086 def __str__(self):
87 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000088 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000089 return self._tk.globalsetvar(self._name, value)
Guido van Rossume2c6e201998-01-14 16:44:34 +000090 def trace_variable(self, mode, callback):
91 cbname = self._master._register(callback)
92 self._tk.call("trace", "variable", self._name, mode, cbname)
93 return cbname
94 trace = trace_variable
95 def trace_vdelete(self, mode, cbname):
96 self._tk.call("trace", "vdelete", self._name, mode, cbname)
Guido van Rossum0001a111998-02-19 21:20:30 +000097 self._master.deletecommand(cbname)
Guido van Rossume2c6e201998-01-14 16:44:34 +000098 def trace_vinfo(self):
99 return map(self._tk.split, self._tk.splitlist(
100 self._tk.call("trace", "vinfo", self._name)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000101
102class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000103 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000104 def __init__(self, master=None):
105 Variable.__init__(self, master)
106 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000107 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000108
109class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000110 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000111 def __init__(self, master=None):
112 Variable.__init__(self, master)
113 def get(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000114 return getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000115
116class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000117 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000118 def __init__(self, master=None):
119 Variable.__init__(self, master)
120 def get(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000121 return getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000122
123class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000124 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000125 def __init__(self, master=None):
126 Variable.__init__(self, master)
127 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000128 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000129
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000130def mainloop(n=0):
131 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000132
Guido van Rossum0132f691998-04-30 17:50:36 +0000133getint = int
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000134
Guido van Rossum0132f691998-04-30 17:50:36 +0000135getdouble = float
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000136
137def getboolean(s):
138 return _default_root.tk.getboolean(s)
139
Guido van Rossum368e06b1997-11-07 20:38:49 +0000140# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000141class Misc:
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000142 # XXX font command?
Fred Drake526749b1997-05-03 04:16:23 +0000143 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000144 def destroy(self):
145 if self._tclCommands is not None:
146 for name in self._tclCommands:
147 #print '- Tkinter: deleted command', name
148 self.tk.deletecommand(name)
149 self._tclCommands = None
150 def deletecommand(self, name):
151 #print '- Tkinter: deleted command', name
152 self.tk.deletecommand(name)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000153 try:
154 self._tclCommands.remove(name)
155 except ValueError:
156 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000157 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000158 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000159 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000160 def tk_bisque(self):
161 self.tk.call('tk_bisque')
162 def tk_setPalette(self, *args, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +0000163 self.tk.call(('tk_setPalette',)
Fred Drake3faf9b41996-10-04 19:23:04 +0000164 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000165 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000166 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000167 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000168 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000169 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000170 def wait_window(self, window=None):
171 if window == None:
172 window = self
173 self.tk.call('tkwait', 'window', window._w)
174 def wait_visibility(self, window=None):
175 if window == None:
176 window = self
177 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000178 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000179 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000180 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000181 return self.tk.getvar(name)
Guido van Rossum0132f691998-04-30 17:50:36 +0000182 getint = int
183 getdouble = float
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000184 def getboolean(self, s):
185 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000186 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000187 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000188 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000189 def focus_force(self):
190 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000191 def focus_get(self):
192 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000193 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000194 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000195 def focus_displayof(self):
196 name = self.tk.call('focus', '-displayof', self._w)
197 if name == 'none' or not name: return None
198 return self._nametowidget(name)
199 def focus_lastfor(self):
200 name = self.tk.call('focus', '-lastfor', self._w)
201 if name == 'none' or not name: return None
202 return self._nametowidget(name)
203 def tk_focusFollowsMouse(self):
204 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000205 def tk_focusNext(self):
206 name = self.tk.call('tk_focusNext', self._w)
207 if not name: return None
208 return self._nametowidget(name)
209 def tk_focusPrev(self):
210 name = self.tk.call('tk_focusPrev', self._w)
211 if not name: return None
212 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000213 def after(self, ms, func=None, *args):
214 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000215 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000216 self.tk.call('after', ms)
217 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000218 # XXX Disgusting hack to clean up after calling func
219 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000220 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000221 try:
222 apply(func, args)
223 finally:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000224 self.deletecommand(tmp[0])
Guido van Rossum08a40381994-06-21 11:44:21 +0000225 name = self._register(callit)
226 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000227 return self.tk.call('after', ms, name)
228 def after_idle(self, func, *args):
229 return apply(self.after, ('idle', func) + args)
230 def after_cancel(self, id):
231 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000232 def bell(self, displayof=0):
Guido van Rossumf9756991998-04-29 21:57:08 +0000233 self.tk.call(('bell',) + self._displayof(displayof))
Fred Drake3c602d71996-09-27 14:06:54 +0000234 # Clipboard handling:
235 def clipboard_clear(self, **kw):
236 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000237 self.tk.call(('clipboard', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000238 def clipboard_append(self, string, **kw):
239 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000240 self.tk.call(('clipboard', 'append') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000241 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000242 # XXX grab current w/o window argument
243 def grab_current(self):
244 name = self.tk.call('grab', 'current', self._w)
245 if not name: return None
246 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000247 def grab_release(self):
248 self.tk.call('grab', 'release', self._w)
249 def grab_set(self):
250 self.tk.call('grab', 'set', self._w)
251 def grab_set_global(self):
252 self.tk.call('grab', 'set', '-global', self._w)
253 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000254 status = self.tk.call('grab', 'status', self._w)
255 if status == 'none': status = None
256 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000257 def lower(self, belowThis=None):
258 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000259 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000260 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000261 def option_clear(self):
262 self.tk.call('option', 'clear')
263 def option_get(self, name, className):
264 return self.tk.call('option', 'get', self._w, name, className)
265 def option_readfile(self, fileName, priority = None):
266 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000267 def selection_clear(self, **kw):
268 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000269 self.tk.call(('selection', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000270 def selection_get(self, **kw):
271 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000272 return self.tk.call(('selection', 'get') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000273 def selection_handle(self, command, **kw):
274 name = self._register(command)
Guido van Rossumf9756991998-04-29 21:57:08 +0000275 self.tk.call(('selection', 'handle') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000276 + (self._w, name))
277 def selection_own(self, **kw):
278 "Become owner of X selection."
Guido van Rossumf9756991998-04-29 21:57:08 +0000279 self.tk.call(('selection', 'own') +
280 self._options(kw) + (self._w,))
Fred Drake3c602d71996-09-27 14:06:54 +0000281 def selection_own_get(self, **kw):
282 "Find owner of X selection."
283 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000284 name = self.tk.call(('selection', 'own') + self._options(kw))
Guido van Rossum76f587b1997-01-21 23:22:03 +0000285 if not name: return None
286 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000287 def send(self, interp, cmd, *args):
Guido van Rossumf9756991998-04-29 21:57:08 +0000288 return self.tk.call(('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000289 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000290 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000291 def tkraise(self, aboveThis=None):
292 self.tk.call('raise', self._w, aboveThis)
293 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000294 def colormodel(self, value=None):
295 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000296 def winfo_atom(self, name, displayof=0):
297 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
Guido van Rossum0132f691998-04-30 17:50:36 +0000298 return getint(self.tk.call(args))
Fred Drake3c602d71996-09-27 14:06:54 +0000299 def winfo_atomname(self, id, displayof=0):
300 args = ('winfo', 'atomname') \
301 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000302 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000303 def winfo_cells(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000304 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000305 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000306 def winfo_children(self):
307 return map(self._nametowidget,
308 self.tk.splitlist(self.tk.call(
309 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000310 def winfo_class(self):
311 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000312 def winfo_colormapfull(self):
313 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000314 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000315 def winfo_containing(self, rootX, rootY, displayof=0):
316 args = ('winfo', 'containing') \
317 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumf9756991998-04-29 21:57:08 +0000318 name = self.tk.call(args)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000319 if not name: return None
320 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000321 def winfo_depth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000322 return getint(self.tk.call('winfo', 'depth', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000323 def winfo_exists(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000324 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000325 self.tk.call('winfo', 'exists', self._w))
326 def winfo_fpixels(self, number):
Guido van Rossum0132f691998-04-30 17:50:36 +0000327 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000328 'winfo', 'fpixels', self._w, number))
329 def winfo_geometry(self):
330 return self.tk.call('winfo', 'geometry', self._w)
331 def winfo_height(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000332 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000333 self.tk.call('winfo', 'height', self._w))
334 def winfo_id(self):
Guido van Rossumcef4c841998-06-19 04:35:45 +0000335 return self.tk.getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000336 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000337 def winfo_interps(self, displayof=0):
338 args = ('winfo', 'interps') + self._displayof(displayof)
Guido van Rossumf9756991998-04-29 21:57:08 +0000339 return self.tk.splitlist(self.tk.call(args))
Guido van Rossum18468821994-06-20 07:49:28 +0000340 def winfo_ismapped(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000341 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000342 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000343 def winfo_manager(self):
344 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000345 def winfo_name(self):
346 return self.tk.call('winfo', 'name', self._w)
347 def winfo_parent(self):
348 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000349 def winfo_pathname(self, id, displayof=0):
350 args = ('winfo', 'pathname') \
351 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000352 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000353 def winfo_pixels(self, number):
Guido van Rossum0132f691998-04-30 17:50:36 +0000354 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000355 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000356 def winfo_pointerx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000357 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000358 self.tk.call('winfo', 'pointerx', self._w))
359 def winfo_pointerxy(self):
360 return self._getints(
361 self.tk.call('winfo', 'pointerxy', self._w))
362 def winfo_pointery(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000363 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000364 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000365 def winfo_reqheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000366 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000367 self.tk.call('winfo', 'reqheight', self._w))
368 def winfo_reqwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000369 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000370 self.tk.call('winfo', 'reqwidth', self._w))
371 def winfo_rgb(self, color):
372 return self._getints(
373 self.tk.call('winfo', 'rgb', self._w, color))
374 def winfo_rootx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000375 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000376 self.tk.call('winfo', 'rootx', self._w))
377 def winfo_rooty(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000378 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000379 self.tk.call('winfo', 'rooty', self._w))
380 def winfo_screen(self):
381 return self.tk.call('winfo', 'screen', self._w)
382 def winfo_screencells(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000383 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000384 self.tk.call('winfo', 'screencells', self._w))
385 def winfo_screendepth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000386 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000387 self.tk.call('winfo', 'screendepth', self._w))
388 def winfo_screenheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000389 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000390 self.tk.call('winfo', 'screenheight', self._w))
391 def winfo_screenmmheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000392 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000393 self.tk.call('winfo', 'screenmmheight', self._w))
394 def winfo_screenmmwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000395 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000396 self.tk.call('winfo', 'screenmmwidth', self._w))
397 def winfo_screenvisual(self):
398 return self.tk.call('winfo', 'screenvisual', self._w)
399 def winfo_screenwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000400 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000401 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000402 def winfo_server(self):
403 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000404 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000405 return self._nametowidget(self.tk.call(
406 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000407 def winfo_viewable(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000408 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000409 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000410 def winfo_visual(self):
411 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000412 def winfo_visualid(self):
413 return self.tk.call('winfo', 'visualid', self._w)
414 def winfo_visualsavailable(self, includeids=0):
415 data = self.tk.split(
416 self.tk.call('winfo', 'visualsavailable', self._w,
417 includeids and 'includeids' or None))
418 def parseitem(x, self=self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000419 return x[:1] + tuple(map(getint, x[1:]))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000420 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000421 def winfo_vrootheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000422 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000423 self.tk.call('winfo', 'vrootheight', self._w))
424 def winfo_vrootwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000425 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000426 self.tk.call('winfo', 'vrootwidth', self._w))
427 def winfo_vrootx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000428 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000429 self.tk.call('winfo', 'vrootx', self._w))
430 def winfo_vrooty(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000431 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000432 self.tk.call('winfo', 'vrooty', self._w))
433 def winfo_width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000434 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000435 self.tk.call('winfo', 'width', self._w))
436 def winfo_x(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000437 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000438 self.tk.call('winfo', 'x', self._w))
439 def winfo_y(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000440 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000441 self.tk.call('winfo', 'y', self._w))
442 def update(self):
443 self.tk.call('update')
444 def update_idletasks(self):
445 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000446 def bindtags(self, tagList=None):
447 if tagList is None:
448 return self.tk.splitlist(
449 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000450 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000451 self.tk.call('bindtags', self._w, tagList)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000452 def _bind(self, what, sequence, func, add, needcleanup=1):
Guido van Rossum88b63b81998-06-25 18:54:49 +0000453 if type(func) is StringType:
454 self.tk.call(what + (sequence, func))
455 elif func:
Guido van Rossum117a5a81998-03-27 21:26:51 +0000456 funcid = self._register(func, self._substitute,
457 needcleanup)
Guido van Rossumdc593401998-04-29 22:16:57 +0000458 cmd = ('%sif {"[%s %s]" == "break"} break\n'
459 %
460 (add and '+' or '',
461 funcid,
462 _string.join(self._subst_format)))
Guido van Rossumf9756991998-04-29 21:57:08 +0000463 self.tk.call(what + (sequence, cmd))
Guido van Rossum117a5a81998-03-27 21:26:51 +0000464 return funcid
Guido van Rossum37dcab11996-05-16 16:00:19 +0000465 else:
Guido van Rossumf9756991998-04-29 21:57:08 +0000466 return self.tk.call(what + (sequence,))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000467 def bind(self, sequence=None, func=None, add=None):
468 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossum117a5a81998-03-27 21:26:51 +0000469 def unbind(self, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +0000470 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +0000471 if funcid:
472 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000473 def bind_all(self, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000474 return self._bind(('bind', 'all'), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000475 def unbind_all(self, sequence):
476 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000477 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000478 return self._bind(('bind', className), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000479 def unbind_class(self, className, sequence):
480 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000481 def mainloop(self, n=0):
482 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000483 def quit(self):
484 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000485 def _getints(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000486 if string:
487 return tuple(map(getint, self.tk.splitlist(string)))
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000488 def _getdoubles(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000489 if string:
490 return tuple(map(getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000491 def _getboolean(self, string):
492 if string:
493 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000494 def _displayof(self, displayof):
495 if displayof:
496 return ('-displayof', displayof)
497 if displayof is None:
498 return ('-displayof', self._w)
499 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000500 def _options(self, cnf, kw = None):
501 if kw:
502 cnf = _cnfmerge((cnf, kw))
503 else:
504 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000505 res = ()
506 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000507 if v is not None:
508 if k[-1] == '_': k = k[:-1]
509 if callable(v):
510 v = self._register(v)
511 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000512 return res
Guido van Rossum98b9d771997-12-12 00:09:34 +0000513 def nametowidget(self, name):
Guido van Rossum45853db1994-06-20 12:19:19 +0000514 w = self
515 if name[0] == '.':
516 w = w._root()
517 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000518 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000519 while name:
520 i = find(name, '.')
521 if i >= 0:
522 name, tail = name[:i], name[i+1:]
523 else:
524 tail = ''
525 w = w.children[name]
526 name = tail
527 return w
Guido van Rossum98b9d771997-12-12 00:09:34 +0000528 _nametowidget = nametowidget
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000529 def _register(self, func, subst=None, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000530 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000531 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000532 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000533 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000534 except AttributeError:
535 pass
536 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000537 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000538 except AttributeError:
539 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000540 self.tk.createcommand(name, f)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000541 if needcleanup:
542 if self._tclCommands is None:
543 self._tclCommands = []
Guido van Rossumc4570481998-03-20 20:45:49 +0000544 self._tclCommands.append(name)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000545 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000546 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000547 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000548 def _root(self):
549 w = self
550 while w.master: w = w.master
551 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000552 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000553 '%s', '%t', '%w', '%x', '%y',
554 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
555 def _substitute(self, *args):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000556 if len(args) != len(self._subst_format): return args
Guido van Rossum0132f691998-04-30 17:50:36 +0000557 getboolean = self.tk.getboolean
558 getint = int
Guido van Rossum45853db1994-06-20 12:19:19 +0000559 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
560 # Missing: (a, c, d, m, o, v, B, R)
561 e = Event()
Guido van Rossum0132f691998-04-30 17:50:36 +0000562 e.serial = getint(nsign)
563 e.num = getint(b)
564 try: e.focus = getboolean(f)
Guido van Rossum45853db1994-06-20 12:19:19 +0000565 except TclError: pass
Guido van Rossum0132f691998-04-30 17:50:36 +0000566 e.height = getint(h)
567 e.keycode = getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000568 # For Visibility events, event state is a string and
569 # not an integer:
570 try:
Guido van Rossum0132f691998-04-30 17:50:36 +0000571 e.state = getint(s)
Guido van Rossumfe02efd1998-06-09 02:37:45 +0000572 except ValueError:
Guido van Rossum36269991996-05-16 17:11:27 +0000573 e.state = s
Guido van Rossum0132f691998-04-30 17:50:36 +0000574 e.time = getint(t)
575 e.width = getint(w)
576 e.x = getint(x)
577 e.y = getint(y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000578 e.char = A
Guido van Rossum0132f691998-04-30 17:50:36 +0000579 try: e.send_event = getboolean(E)
Guido van Rossum45853db1994-06-20 12:19:19 +0000580 except TclError: pass
581 e.keysym = K
Guido van Rossum0132f691998-04-30 17:50:36 +0000582 e.keysym_num = getint(N)
Guido van Rossum45853db1994-06-20 12:19:19 +0000583 e.type = T
Guido van Rossume86271a1998-04-27 19:32:59 +0000584 try:
585 e.widget = self._nametowidget(W)
586 except KeyError:
587 e.widget = W
Guido van Rossum0132f691998-04-30 17:50:36 +0000588 e.x_root = getint(X)
589 e.y_root = getint(Y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000590 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000591 def _report_exception(self):
592 import sys
593 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
594 root = self._root()
595 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000596 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000597 def configure(self, cnf=None, **kw):
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000598 # XXX ought to generalize this so tag_config etc. can use it
599 if kw:
600 cnf = _cnfmerge((cnf, kw))
601 elif cnf:
602 cnf = _cnfmerge(cnf)
603 if cnf is None:
604 cnf = {}
605 for x in self.tk.split(
606 self.tk.call(self._w, 'configure')):
607 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
608 return cnf
609 if type(cnf) is StringType:
610 x = self.tk.split(self.tk.call(
611 self._w, 'configure', '-'+cnf))
612 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +0000613 self.tk.call((self._w, 'configure')
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000614 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000615 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000616 def cget(self, key):
617 return self.tk.call(self._w, 'cget', '-' + key)
618 __getitem__ = cget
619 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +0000620 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000621 def keys(self):
622 return map(lambda x: x[0][1:],
623 self.tk.split(self.tk.call(self._w, 'configure')))
624 def __str__(self):
625 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +0000626 # Pack methods that apply to the master
627 _noarg_ = ['_noarg_']
628 def pack_propagate(self, flag=_noarg_):
629 if flag is Misc._noarg_:
630 return self._getboolean(self.tk.call(
631 'pack', 'propagate', self._w))
632 else:
633 self.tk.call('pack', 'propagate', self._w, flag)
634 propagate = pack_propagate
635 def pack_slaves(self):
636 return map(self._nametowidget,
637 self.tk.splitlist(
638 self.tk.call('pack', 'slaves', self._w)))
639 slaves = pack_slaves
640 # Place method that applies to the master
641 def place_slaves(self):
642 return map(self._nametowidget,
643 self.tk.splitlist(
644 self.tk.call(
645 'place', 'slaves', self._w)))
646 # Grid methods that apply to the master
647 def grid_bbox(self, column, row):
648 return self._getints(
649 self.tk.call(
650 'grid', 'bbox', self._w, column, row)) or None
651 bbox = grid_bbox
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000652 def _grid_configure(self, command, index, cnf, kw):
653 if type(cnf) is StringType and not kw:
654 if cnf[-1:] == '_':
655 cnf = cnf[:-1]
656 if cnf[:1] != '-':
657 cnf = '-'+cnf
658 options = (cnf,)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000659 else:
660 options = self._options(cnf, kw)
661 if not options:
662 res = self.tk.call('grid',
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000663 command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000664 words = self.tk.splitlist(res)
665 dict = {}
666 for i in range(0, len(words), 2):
667 key = words[i][1:]
668 value = words[i+1]
669 if not value:
670 value = None
671 elif '.' in value:
Guido van Rossum0132f691998-04-30 17:50:36 +0000672 value = getdouble(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000673 else:
Guido van Rossum0132f691998-04-30 17:50:36 +0000674 value = getint(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000675 dict[key] = value
676 return dict
Guido van Rossumf9756991998-04-29 21:57:08 +0000677 res = self.tk.call(
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000678 ('grid', command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000679 + options)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000680 if len(options) == 1:
681 if not res: return None
682 # In Tk 7.5, -width can be a float
Guido van Rossum0132f691998-04-30 17:50:36 +0000683 if '.' in res: return getdouble(res)
684 return getint(res)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000685 def grid_columnconfigure(self, index, cnf={}, **kw):
686 return self._grid_configure('columnconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000687 columnconfigure = grid_columnconfigure
688 def grid_propagate(self, flag=_noarg_):
689 if flag is Misc._noarg_:
690 return self._getboolean(self.tk.call(
691 'grid', 'propagate', self._w))
692 else:
693 self.tk.call('grid', 'propagate', self._w, flag)
694 def grid_rowconfigure(self, index, cnf={}, **kw):
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000695 return self._grid_configure('rowconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000696 rowconfigure = grid_rowconfigure
697 def grid_size(self):
698 return self._getints(
699 self.tk.call('grid', 'size', self._w)) or None
700 size = grid_size
Guido van Rossum1cd6a451997-12-30 04:07:19 +0000701 def grid_slaves(self, row=None, column=None):
702 args = ()
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000703 if row:
704 args = args + ('-row', row)
705 if column:
706 args = args + ('-column', column)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000707 return map(self._nametowidget,
Guido van Rossumf9756991998-04-29 21:57:08 +0000708 self.tk.splitlist(self.tk.call(
709 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +0000710
Guido van Rossum80f8be81997-12-02 19:51:39 +0000711 # Support for the "event" command, new in Tk 4.2.
712 # By Case Roole.
713
Guido van Rossum56c04b81998-04-06 03:10:03 +0000714 def event_add(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000715 args = ('event', 'add', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000716 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000717
Guido van Rossum56c04b81998-04-06 03:10:03 +0000718 def event_delete(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000719 args = ('event', 'delete', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000720 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000721
722 def event_generate(self, sequence, **kw):
723 args = ('event', 'generate', self._w, sequence)
Guido van Rossum56c04b81998-04-06 03:10:03 +0000724 for k, v in kw.items():
725 args = args + ('-%s' % k, str(v))
Guido van Rossumf9756991998-04-29 21:57:08 +0000726 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000727
Guido van Rossum56c04b81998-04-06 03:10:03 +0000728 def event_info(self, virtual=None):
729 return self.tk.splitlist(
730 self.tk.call('event', 'info', virtual))
Guido van Rossum80f8be81997-12-02 19:51:39 +0000731
Guido van Rossumc2966511998-04-10 19:16:10 +0000732 # Image related commands
733
734 def image_names(self):
735 return self.tk.call('image', 'names')
736
737 def image_types(self):
738 return self.tk.call('image', 'types')
739
Guido van Rossum80f8be81997-12-02 19:51:39 +0000740
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000741class CallWrapper:
742 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000743 self.func = func
744 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000745 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000746 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000747 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000748 if self.subst:
749 args = apply(self.subst, args)
750 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000751 except SystemExit, msg:
752 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000753 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000754 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000755
Guido van Rossume365a591998-05-01 19:48:20 +0000756
Guido van Rossum18468821994-06-20 07:49:28 +0000757class Wm:
Guido van Rossume365a591998-05-01 19:48:20 +0000758 def wm_aspect(self,
Guido van Rossum18468821994-06-20 07:49:28 +0000759 minNumer=None, minDenom=None,
760 maxNumer=None, maxDenom=None):
761 return self._getints(
762 self.tk.call('wm', 'aspect', self._w,
763 minNumer, minDenom,
764 maxNumer, maxDenom))
Guido van Rossume365a591998-05-01 19:48:20 +0000765 aspect = wm_aspect
766 def wm_client(self, name=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000767 return self.tk.call('wm', 'client', self._w, name)
Guido van Rossume365a591998-05-01 19:48:20 +0000768 client = wm_client
769 def wm_colormapwindows(self, *wlist):
Fred Drake3c602d71996-09-27 14:06:54 +0000770 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
Guido van Rossumf9756991998-04-29 21:57:08 +0000771 return map(self._nametowidget, self.tk.call(args))
Guido van Rossume365a591998-05-01 19:48:20 +0000772 colormapwindows = wm_colormapwindows
773 def wm_command(self, value=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000774 return self.tk.call('wm', 'command', self._w, value)
Guido van Rossume365a591998-05-01 19:48:20 +0000775 command = wm_command
776 def wm_deiconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000777 return self.tk.call('wm', 'deiconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000778 deiconify = wm_deiconify
779 def wm_focusmodel(self, model=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000780 return self.tk.call('wm', 'focusmodel', self._w, model)
Guido van Rossume365a591998-05-01 19:48:20 +0000781 focusmodel = wm_focusmodel
782 def wm_frame(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000783 return self.tk.call('wm', 'frame', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000784 frame = wm_frame
785 def wm_geometry(self, newGeometry=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000786 return self.tk.call('wm', 'geometry', self._w, newGeometry)
Guido van Rossume365a591998-05-01 19:48:20 +0000787 geometry = wm_geometry
788 def wm_grid(self,
Guido van Rossum21df8f51998-02-24 23:26:18 +0000789 baseWidth=None, baseHeight=None,
Guido van Rossum18468821994-06-20 07:49:28 +0000790 widthInc=None, heightInc=None):
791 return self._getints(self.tk.call(
792 'wm', 'grid', self._w,
Guido van Rossum4d9d3f11997-12-27 15:14:43 +0000793 baseWidth, baseHeight, widthInc, heightInc))
Guido van Rossume365a591998-05-01 19:48:20 +0000794 grid = wm_grid
795 def wm_group(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000796 return self.tk.call('wm', 'group', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000797 group = wm_group
798 def wm_iconbitmap(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000799 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000800 iconbitmap = wm_iconbitmap
801 def wm_iconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000802 return self.tk.call('wm', 'iconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000803 iconify = wm_iconify
804 def wm_iconmask(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000805 return self.tk.call('wm', 'iconmask', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000806 iconmask = wm_iconmask
807 def wm_iconname(self, newName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000808 return self.tk.call('wm', 'iconname', self._w, newName)
Guido van Rossume365a591998-05-01 19:48:20 +0000809 iconname = wm_iconname
810 def wm_iconposition(self, x=None, y=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000811 return self._getints(self.tk.call(
812 'wm', 'iconposition', self._w, x, y))
Guido van Rossume365a591998-05-01 19:48:20 +0000813 iconposition = wm_iconposition
814 def wm_iconwindow(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000815 return self.tk.call('wm', 'iconwindow', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000816 iconwindow = wm_iconwindow
817 def wm_maxsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000818 return self._getints(self.tk.call(
819 'wm', 'maxsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000820 maxsize = wm_maxsize
821 def wm_minsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000822 return self._getints(self.tk.call(
823 'wm', 'minsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000824 minsize = wm_minsize
825 def wm_overrideredirect(self, boolean=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000826 return self._getboolean(self.tk.call(
827 'wm', 'overrideredirect', self._w, boolean))
Guido van Rossume365a591998-05-01 19:48:20 +0000828 overrideredirect = wm_overrideredirect
829 def wm_positionfrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000830 return self.tk.call('wm', 'positionfrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000831 positionfrom = wm_positionfrom
832 def wm_protocol(self, name=None, func=None):
Guido van Rossumc4570481998-03-20 20:45:49 +0000833 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000834 command = self._register(func)
835 else:
836 command = func
837 return self.tk.call(
838 'wm', 'protocol', self._w, name, command)
Guido van Rossume365a591998-05-01 19:48:20 +0000839 protocol = wm_protocol
840 def wm_resizable(self, width=None, height=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000841 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossume365a591998-05-01 19:48:20 +0000842 resizable = wm_resizable
843 def wm_sizefrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000844 return self.tk.call('wm', 'sizefrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000845 sizefrom = wm_sizefrom
846 def wm_state(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000847 return self.tk.call('wm', 'state', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000848 state = wm_state
849 def wm_title(self, string=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000850 return self.tk.call('wm', 'title', self._w, string)
Guido van Rossume365a591998-05-01 19:48:20 +0000851 title = wm_title
852 def wm_transient(self, master=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000853 return self.tk.call('wm', 'transient', self._w, master)
Guido van Rossume365a591998-05-01 19:48:20 +0000854 transient = wm_transient
855 def wm_withdraw(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000856 return self.tk.call('wm', 'withdraw', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000857 withdraw = wm_withdraw
858
Guido van Rossum18468821994-06-20 07:49:28 +0000859
860class Tk(Misc, Wm):
861 _w = '.'
862 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000863 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000864 self.master = None
865 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000866 if baseName is None:
867 import sys, os
868 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000869 baseName, ext = os.path.splitext(baseName)
Fred Drake182c5901998-07-15 04:36:56 +0000870 if ext not in ('.py', '.pyc', '.pyo'):
871 baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000872 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000873 if _MacOS:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000874 # Disable event scanning except for Command-Period
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000875 _MacOS.SchedParams(1, 0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000876 # Work around nasty MacTk bug
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000877 # XXX Is this one still needed?
Guido van Rossum37dcab11996-05-16 16:00:19 +0000878 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000879 # Version sanity checks
880 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000881 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000882 raise RuntimeError, \
883 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000884 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000885 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000886 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000887 raise RuntimeError, \
888 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000889 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000890 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000891 raise RuntimeError, \
892 "Tk 4.0 or higher is required; found Tk %s" \
893 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000894 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000895 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000896 self.readprofile(baseName, className)
Guido van Rossumc4570481998-03-20 20:45:49 +0000897 if _support_default_root and not _default_root:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000898 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000899 def destroy(self):
900 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000901 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000902 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000903 global _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +0000904 if _support_default_root and _default_root is self:
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000905 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000906 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000907 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000908 if os.environ.has_key('HOME'): home = os.environ['HOME']
909 else: home = os.curdir
910 class_tcl = os.path.join(home, '.%s.tcl' % className)
911 class_py = os.path.join(home, '.%s.py' % className)
912 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
913 base_py = os.path.join(home, '.%s.py' % baseName)
914 dir = {'self': self}
915 exec 'from Tkinter import *' in dir
916 if os.path.isfile(class_tcl):
917 print 'source', `class_tcl`
918 self.tk.call('source', class_tcl)
919 if os.path.isfile(class_py):
920 print 'execfile', `class_py`
921 execfile(class_py, dir)
922 if os.path.isfile(base_tcl):
923 print 'source', `base_tcl`
924 self.tk.call('source', base_tcl)
925 if os.path.isfile(base_py):
926 print 'execfile', `base_py`
927 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000928 def report_callback_exception(self, exc, val, tb):
929 import traceback
930 print "Exception in Tkinter callback"
931 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000932
Guido van Rossum368e06b1997-11-07 20:38:49 +0000933# Ideally, the classes Pack, Place and Grid disappear, the
934# pack/place/grid methods are defined on the Widget class, and
935# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
936# ...), with pack(), place() and grid() being short for
937# pack_configure(), place_configure() and grid_columnconfigure(), and
938# forget() being short for pack_forget(). As a practical matter, I'm
939# afraid that there is too much code out there that may be using the
940# Pack, Place or Grid class, so I leave them intact -- but only as
941# backwards compatibility features. Also note that those methods that
942# take a master as argument (e.g. pack_propagate) have been moved to
943# the Misc class (which now incorporates all methods common between
944# toplevel and interior widgets). Again, for compatibility, these are
945# copied into the Pack, Place or Grid class.
946
Guido van Rossum18468821994-06-20 07:49:28 +0000947class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000948 def pack_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +0000949 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000950 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000951 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000952 pack = configure = config = pack_configure
953 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000954 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000955 forget = pack_forget
956 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000957 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000958 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000959 dict = {}
960 for i in range(0, len(words), 2):
961 key = words[i][1:]
962 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000963 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000964 value = self._nametowidget(value)
965 dict[key] = value
966 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000967 info = pack_info
968 propagate = pack_propagate = Misc.pack_propagate
969 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000970
971class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000972 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000973 for k in ['in_']:
974 if kw.has_key(k):
975 kw[k[:-1]] = kw[k]
976 del kw[k]
Guido van Rossumf9756991998-04-29 21:57:08 +0000977 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000978 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000979 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000980 place = configure = config = place_configure
981 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000982 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000983 forget = place_forget
984 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000985 words = self.tk.splitlist(
986 self.tk.call('place', 'info', self._w))
987 dict = {}
988 for i in range(0, len(words), 2):
989 key = words[i][1:]
990 value = words[i+1]
991 if value[:1] == '.':
992 value = self._nametowidget(value)
993 dict[key] = value
994 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000995 info = place_info
996 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000997
Guido van Rossum37dcab11996-05-16 16:00:19 +0000998class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000999 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001000 def grid_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001001 self.tk.call(
Guido van Rossum37dcab11996-05-16 16:00:19 +00001002 ('grid', 'configure', self._w)
1003 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001004 grid = configure = config = grid_configure
1005 bbox = grid_bbox = Misc.grid_bbox
1006 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
1007 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001008 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001009 forget = grid_forget
1010 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001011 words = self.tk.splitlist(
1012 self.tk.call('grid', 'info', self._w))
1013 dict = {}
1014 for i in range(0, len(words), 2):
1015 key = words[i][1:]
1016 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +00001017 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +00001018 value = self._nametowidget(value)
1019 dict[key] = value
1020 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001021 info = grid_info
1022 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001023 return self._getints(
1024 self.tk.call(
1025 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +00001026 location = grid_location
1027 propagate = grid_propagate = Misc.grid_propagate
1028 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
1029 size = grid_size = Misc.grid_size
1030 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00001031
Guido van Rossum368e06b1997-11-07 20:38:49 +00001032class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001033 def _setup(self, master, cnf):
Guido van Rossumc4570481998-03-20 20:45:49 +00001034 if _support_default_root:
1035 global _default_root
1036 if not master:
1037 if not _default_root:
1038 _default_root = Tk()
1039 master = _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +00001040 if not _default_root:
Guido van Rossumc4570481998-03-20 20:45:49 +00001041 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +00001042 self.master = master
1043 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001044 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001045 if cnf.has_key('name'):
1046 name = cnf['name']
1047 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001048 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001049 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001050 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001051 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001052 self._w = '.' + name
1053 else:
1054 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001055 self.children = {}
1056 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001057 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001058 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001059 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1060 if kw:
1061 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001062 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001063 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001064 classes = []
1065 for k in cnf.keys():
1066 if type(k) is ClassType:
1067 classes.append((k, cnf[k]))
1068 del cnf[k]
Guido van Rossumf9756991998-04-29 21:57:08 +00001069 self.tk.call(
1070 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001071 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001072 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001073 def destroy(self):
1074 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001075 if self.master.children.has_key(self._name):
1076 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001077 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001078 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001079 def _do(self, name, args=()):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001080 # XXX Obsolete -- better use self.tk.call directly!
Guido van Rossumf9756991998-04-29 21:57:08 +00001081 return self.tk.call((self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001082
Guido van Rossum368e06b1997-11-07 20:38:49 +00001083class Widget(BaseWidget, Pack, Place, Grid):
1084 pass
1085
1086class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001087 def __init__(self, master=None, cnf={}, **kw):
1088 if kw:
1089 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001090 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001091 for wmkey in ['screen', 'class_', 'class', 'visual',
1092 'colormap']:
1093 if cnf.has_key(wmkey):
1094 val = cnf[wmkey]
1095 # TBD: a hack needed because some keys
1096 # are not valid as keyword arguments
1097 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1098 else: opt = '-'+wmkey
1099 extra = extra + (opt, val)
1100 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001101 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001102 root = self._root()
1103 self.iconname(root.iconname())
1104 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +00001105
1106class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001107 def __init__(self, master=None, cnf={}, **kw):
1108 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001109 def tkButtonEnter(self, *dummy):
1110 self.tk.call('tkButtonEnter', self._w)
1111 def tkButtonLeave(self, *dummy):
1112 self.tk.call('tkButtonLeave', self._w)
1113 def tkButtonDown(self, *dummy):
1114 self.tk.call('tkButtonDown', self._w)
1115 def tkButtonUp(self, *dummy):
1116 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001117 def tkButtonInvoke(self, *dummy):
1118 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001119 def flash(self):
1120 self.tk.call(self._w, 'flash')
1121 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001122 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001123
1124# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001125# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001126def AtEnd():
1127 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001128def AtInsert(*args):
1129 s = 'insert'
1130 for a in args:
1131 if a: s = s + (' ' + a)
1132 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001133def AtSelFirst():
1134 return 'sel.first'
1135def AtSelLast():
1136 return 'sel.last'
1137def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001138 if y is None:
1139 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001140 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001141 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001142
1143class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001144 def __init__(self, master=None, cnf={}, **kw):
1145 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001146 def addtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001147 self.tk.call((self._w, 'addtag') + args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001148 def addtag_above(self, newtag, tagOrId):
1149 self.addtag(newtag, 'above', tagOrId)
1150 def addtag_all(self, newtag):
1151 self.addtag(newtag, 'all')
1152 def addtag_below(self, newtag, tagOrId):
1153 self.addtag(newtag, 'below', tagOrId)
1154 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1155 self.addtag(newtag, 'closest', x, y, halo, start)
1156 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1157 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1158 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1159 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1160 def addtag_withtag(self, newtag, tagOrId):
1161 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001162 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001163 return self._getints(
1164 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum117a5a81998-03-27 21:26:51 +00001165 def tag_unbind(self, tagOrId, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001166 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001167 if funcid:
1168 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001169 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001170 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001171 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001172 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001173 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001174 self._w, 'canvasx', screenx, gridspacing))
1175 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001176 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001177 self._w, 'canvasy', screeny, gridspacing))
1178 def coords(self, *args):
Guido van Rossum0001a111998-02-19 21:20:30 +00001179 # XXX Should use _flatten on args
Guido van Rossum0132f691998-04-30 17:50:36 +00001180 return map(getdouble,
Guido van Rossum0bd54331998-05-19 21:18:13 +00001181 self.tk.splitlist(
1182 self.tk.call((self._w, 'coords') + args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001183 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001184 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001185 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001186 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001187 args = args[:-1]
1188 else:
1189 cnf = {}
Guido van Rossum0132f691998-04-30 17:50:36 +00001190 return getint(apply(
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001191 self.tk.call,
1192 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001193 + args + self._options(cnf, kw)))
1194 def create_arc(self, *args, **kw):
1195 return self._create('arc', args, kw)
1196 def create_bitmap(self, *args, **kw):
1197 return self._create('bitmap', args, kw)
1198 def create_image(self, *args, **kw):
1199 return self._create('image', args, kw)
1200 def create_line(self, *args, **kw):
1201 return self._create('line', args, kw)
1202 def create_oval(self, *args, **kw):
1203 return self._create('oval', args, kw)
1204 def create_polygon(self, *args, **kw):
1205 return self._create('polygon', args, kw)
1206 def create_rectangle(self, *args, **kw):
1207 return self._create('rectangle', args, kw)
1208 def create_text(self, *args, **kw):
1209 return self._create('text', args, kw)
1210 def create_window(self, *args, **kw):
1211 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001212 def dchars(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001213 self.tk.call((self._w, 'dchars') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001214 def delete(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001215 self.tk.call((self._w, 'delete') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001216 def dtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001217 self.tk.call((self._w, 'dtag') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001218 def find(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001219 return self._getints(
1220 self.tk.call((self._w, 'find') + args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001221 def find_above(self, tagOrId):
1222 return self.find('above', tagOrId)
1223 def find_all(self):
1224 return self.find('all')
1225 def find_below(self, tagOrId):
1226 return self.find('below', tagOrId)
1227 def find_closest(self, x, y, halo=None, start=None):
1228 return self.find('closest', x, y, halo, start)
1229 def find_enclosed(self, x1, y1, x2, y2):
1230 return self.find('enclosed', x1, y1, x2, y2)
1231 def find_overlapping(self, x1, y1, x2, y2):
1232 return self.find('overlapping', x1, y1, x2, y2)
1233 def find_withtag(self, tagOrId):
1234 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001235 def focus(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001236 return self.tk.call((self._w, 'focus') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001237 def gettags(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001238 return self.tk.splitlist(
1239 self.tk.call((self._w, 'gettags') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001240 def icursor(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001241 self.tk.call((self._w, 'icursor') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001242 def index(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001243 return getint(self.tk.call((self._w, 'index') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001244 def insert(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001245 self.tk.call((self._w, 'insert') + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001246 def itemcget(self, tagOrId, option):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001247 return self.tk.call(
1248 (self._w, 'itemcget') + (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001249 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001250 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001251 cnf = {}
1252 for x in self.tk.split(
Guido van Rossum0bd54331998-05-19 21:18:13 +00001253 self.tk.call(self._w,
1254 'itemconfigure', tagOrId)):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001255 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1256 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001257 if type(cnf) == StringType and not kw:
Guido van Rossum0bd54331998-05-19 21:18:13 +00001258 x = self.tk.split(self.tk.call(
1259 self._w, 'itemconfigure', tagOrId, '-'+cnf))
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001260 return (x[0][1:],) + x[1:]
Guido van Rossum0bd54331998-05-19 21:18:13 +00001261 self.tk.call((self._w, 'itemconfigure', tagOrId) +
1262 self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001263 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001264 def lower(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001265 self.tk.call((self._w, 'lower') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001266 def move(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001267 self.tk.call((self._w, 'move') + args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001268 def postscript(self, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001269 return self.tk.call((self._w, 'postscript') +
1270 self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001271 def tkraise(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001272 self.tk.call((self._w, 'raise') + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001273 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001274 def scale(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001275 self.tk.call((self._w, 'scale') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001276 def scan_mark(self, x, y):
1277 self.tk.call(self._w, 'scan', 'mark', x, y)
1278 def scan_dragto(self, x, y):
1279 self.tk.call(self._w, 'scan', 'dragto', x, y)
1280 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001281 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001282 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001283 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001284 def select_from(self, tagOrId, index):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001285 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001286 def select_item(self):
1287 self.tk.call(self._w, 'select', 'item')
1288 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001289 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001290 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001291 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001292 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001293 if not args:
1294 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001295 self.tk.call((self._w, 'xview') + args)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001296 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001297 if not args:
1298 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001299 self.tk.call((self._w, 'yview') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001300
1301class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001302 def __init__(self, master=None, cnf={}, **kw):
1303 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001304 def deselect(self):
1305 self.tk.call(self._w, 'deselect')
1306 def flash(self):
1307 self.tk.call(self._w, 'flash')
1308 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001309 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001310 def select(self):
1311 self.tk.call(self._w, 'select')
1312 def toggle(self):
1313 self.tk.call(self._w, 'toggle')
1314
1315class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001316 def __init__(self, master=None, cnf={}, **kw):
1317 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001318 def delete(self, first, last=None):
1319 self.tk.call(self._w, 'delete', first, last)
1320 def get(self):
1321 return self.tk.call(self._w, 'get')
1322 def icursor(self, index):
1323 self.tk.call(self._w, 'icursor', index)
1324 def index(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001325 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001326 self._w, 'index', index))
1327 def insert(self, index, string):
1328 self.tk.call(self._w, 'insert', index, string)
1329 def scan_mark(self, x):
1330 self.tk.call(self._w, 'scan', 'mark', x)
1331 def scan_dragto(self, x):
1332 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001333 def selection_adjust(self, index):
1334 self.tk.call(self._w, 'selection', 'adjust', index)
1335 select_adjust = selection_adjust
1336 def selection_clear(self):
1337 self.tk.call(self._w, 'selection', 'clear')
1338 select_clear = selection_clear
1339 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001340 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001341 select_from = selection_from
1342 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001343 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001344 self.tk.call(self._w, 'selection', 'present'))
1345 select_present = selection_present
1346 def selection_range(self, start, end):
1347 self.tk.call(self._w, 'selection', 'range', start, end)
1348 select_range = selection_range
1349 def selection_to(self, index):
1350 self.tk.call(self._w, 'selection', 'to', index)
1351 select_to = selection_to
1352 def xview(self, index):
1353 self.tk.call(self._w, 'xview', index)
1354 def xview_moveto(self, fraction):
1355 self.tk.call(self._w, 'xview', 'moveto', fraction)
1356 def xview_scroll(self, number, what):
1357 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001358
1359class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001360 def __init__(self, master=None, cnf={}, **kw):
1361 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001362 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001363 if cnf.has_key('class_'):
1364 extra = ('-class', cnf['class_'])
1365 del cnf['class_']
1366 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001367 extra = ('-class', cnf['class'])
1368 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001369 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001370
1371class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001372 def __init__(self, master=None, cnf={}, **kw):
1373 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001374
Guido van Rossum18468821994-06-20 07:49:28 +00001375class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001376 def __init__(self, master=None, cnf={}, **kw):
1377 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001378 def activate(self, index):
1379 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001380 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001381 return self._getints(
1382 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001383 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001384 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001385 return self.tk.splitlist(self.tk.call(
1386 self._w, 'curselection'))
1387 def delete(self, first, last=None):
1388 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001389 def get(self, first, last=None):
1390 if last:
1391 return self.tk.splitlist(self.tk.call(
1392 self._w, 'get', first, last))
1393 else:
1394 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001395 def insert(self, index, *elements):
Guido van Rossumf9756991998-04-29 21:57:08 +00001396 self.tk.call((self._w, 'insert', index) + elements)
Guido van Rossum18468821994-06-20 07:49:28 +00001397 def nearest(self, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001398 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001399 self._w, 'nearest', y))
1400 def scan_mark(self, x, y):
1401 self.tk.call(self._w, 'scan', 'mark', x, y)
1402 def scan_dragto(self, x, y):
1403 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001404 def see(self, index):
1405 self.tk.call(self._w, 'see', index)
1406 def index(self, index):
1407 i = self.tk.call(self._w, 'index', index)
1408 if i == 'none': return None
Guido van Rossum0132f691998-04-30 17:50:36 +00001409 return getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001410 def select_anchor(self, index):
1411 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001412 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001413 def select_clear(self, first, last=None):
1414 self.tk.call(self._w,
1415 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001416 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001417 def select_includes(self, index):
1418 return self.tk.getboolean(self.tk.call(
1419 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001420 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001421 def select_set(self, first, last=None):
1422 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001423 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001424 def size(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001425 return getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001426 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001427 if not what:
1428 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001429 self.tk.call((self._w, 'xview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001430 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001431 if not what:
1432 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001433 self.tk.call((self._w, 'yview') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00001434
1435class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001436 def __init__(self, master=None, cnf={}, **kw):
1437 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001438 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001439 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001440 def tk_mbPost(self):
1441 self.tk.call('tk_mbPost', self._w)
1442 def tk_mbUnpost(self):
1443 self.tk.call('tk_mbUnpost')
1444 def tk_traverseToMenu(self, char):
1445 self.tk.call('tk_traverseToMenu', self._w, char)
1446 def tk_traverseWithinMenu(self, char):
1447 self.tk.call('tk_traverseWithinMenu', self._w, char)
1448 def tk_getMenuButtons(self):
1449 return self.tk.call('tk_getMenuButtons', self._w)
1450 def tk_nextMenu(self, count):
1451 self.tk.call('tk_nextMenu', count)
1452 def tk_nextMenuEntry(self, count):
1453 self.tk.call('tk_nextMenuEntry', count)
1454 def tk_invokeMenu(self):
1455 self.tk.call('tk_invokeMenu', self._w)
1456 def tk_firstMenu(self):
1457 self.tk.call('tk_firstMenu', self._w)
1458 def tk_mbButtonDown(self):
1459 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001460 def tk_popup(self, x, y, entry=""):
1461 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001462 def activate(self, index):
1463 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001464 def add(self, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001465 self.tk.call((self._w, 'add', itemType) +
1466 self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001467 def add_cascade(self, cnf={}, **kw):
1468 self.add('cascade', cnf or kw)
1469 def add_checkbutton(self, cnf={}, **kw):
1470 self.add('checkbutton', cnf or kw)
1471 def add_command(self, cnf={}, **kw):
1472 self.add('command', cnf or kw)
1473 def add_radiobutton(self, cnf={}, **kw):
1474 self.add('radiobutton', cnf or kw)
1475 def add_separator(self, cnf={}, **kw):
1476 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001477 def insert(self, index, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001478 self.tk.call((self._w, 'insert', index, itemType) +
1479 self._options(cnf, kw))
Guido van Rossum2caac731996-09-05 16:46:31 +00001480 def insert_cascade(self, index, cnf={}, **kw):
1481 self.insert(index, 'cascade', cnf or kw)
1482 def insert_checkbutton(self, index, cnf={}, **kw):
1483 self.insert(index, 'checkbutton', cnf or kw)
1484 def insert_command(self, index, cnf={}, **kw):
1485 self.insert(index, 'command', cnf or kw)
1486 def insert_radiobutton(self, index, cnf={}, **kw):
1487 self.insert(index, 'radiobutton', cnf or kw)
1488 def insert_separator(self, index, cnf={}, **kw):
1489 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001490 def delete(self, index1, index2=None):
1491 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001492 def entrycget(self, index, option):
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001493 return self.tk.call(self._w, 'entrycget', index, '-' + option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001494 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001495 if cnf is None and not kw:
1496 cnf = {}
Guido van Rossumf9756991998-04-29 21:57:08 +00001497 for x in self.tk.split(self.tk.call(
1498 (self._w, 'entryconfigure', index))):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001499 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1500 return cnf
1501 if type(cnf) == StringType and not kw:
Guido van Rossumf9756991998-04-29 21:57:08 +00001502 x = self.tk.split(self.tk.call(
1503 (self._w, 'entryconfigure', index, '-'+cnf)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001504 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001505 self.tk.call((self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001506 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001507 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001508 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001509 i = self.tk.call(self._w, 'index', index)
1510 if i == 'none': return None
Guido van Rossum0132f691998-04-30 17:50:36 +00001511 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001512 def invoke(self, index):
1513 return self.tk.call(self._w, 'invoke', index)
1514 def post(self, x, y):
1515 self.tk.call(self._w, 'post', x, y)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001516 def type(self, index):
1517 return self.tk.call(self._w, 'type', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001518 def unpost(self):
1519 self.tk.call(self._w, 'unpost')
1520 def yposition(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001521 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001522 self._w, 'yposition', index))
1523
1524class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001525 def __init__(self, master=None, cnf={}, **kw):
1526 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001527
1528class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001529 def __init__(self, master=None, cnf={}, **kw):
1530 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001531
1532class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001533 def __init__(self, master=None, cnf={}, **kw):
1534 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001535 def deselect(self):
1536 self.tk.call(self._w, 'deselect')
1537 def flash(self):
1538 self.tk.call(self._w, 'flash')
1539 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001540 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001541 def select(self):
1542 self.tk.call(self._w, 'select')
1543
1544class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001545 def __init__(self, master=None, cnf={}, **kw):
1546 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001547 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001548 value = self.tk.call(self._w, 'get')
1549 try:
Guido van Rossum0132f691998-04-30 17:50:36 +00001550 return getint(value)
Guido van Rossumfe02efd1998-06-09 02:37:45 +00001551 except ValueError:
Guido van Rossum0132f691998-04-30 17:50:36 +00001552 return getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001553 def set(self, value):
1554 self.tk.call(self._w, 'set', value)
1555
1556class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001557 def __init__(self, master=None, cnf={}, **kw):
1558 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001559 def activate(self, index):
1560 self.tk.call(self._w, 'activate', index)
1561 def delta(self, deltax, deltay):
Guido van Rossum0132f691998-04-30 17:50:36 +00001562 return getdouble(
1563 self.tk.call(self._w, 'delta', deltax, deltay))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001564 def fraction(self, x, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001565 return getdouble(self.tk.call(self._w, 'fraction', x, y))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001566 def identify(self, x, y):
1567 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001568 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001569 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001570 def set(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001571 self.tk.call((self._w, 'set') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001572
1573class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001574 def __init__(self, master=None, cnf={}, **kw):
1575 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001576 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001577 return self._getints(
1578 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001579 def tk_textSelectTo(self, index):
1580 self.tk.call('tk_textSelectTo', self._w, index)
1581 def tk_textBackspace(self):
1582 self.tk.call('tk_textBackspace', self._w)
1583 def tk_textIndexCloser(self, a, b, c):
1584 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1585 def tk_textResetAnchor(self, index):
1586 self.tk.call('tk_textResetAnchor', self._w, index)
1587 def compare(self, index1, op, index2):
1588 return self.tk.getboolean(self.tk.call(
1589 self._w, 'compare', index1, op, index2))
1590 def debug(self, boolean=None):
1591 return self.tk.getboolean(self.tk.call(
1592 self._w, 'debug', boolean))
1593 def delete(self, index1, index2=None):
1594 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001595 def dlineinfo(self, index):
1596 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001597 def get(self, index1, index2=None):
1598 return self.tk.call(self._w, 'get', index1, index2)
1599 def index(self, index):
1600 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001601 def insert(self, index, chars, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001602 self.tk.call((self._w, 'insert', index, chars) + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001603 def mark_gravity(self, markName, direction=None):
Guido van Rossumf9756991998-04-29 21:57:08 +00001604 return self.tk.call(
1605 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001606 def mark_names(self):
1607 return self.tk.splitlist(self.tk.call(
1608 self._w, 'mark', 'names'))
1609 def mark_set(self, markName, index):
1610 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001611 def mark_unset(self, *markNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001612 self.tk.call((self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001613 def scan_mark(self, x, y):
1614 self.tk.call(self._w, 'scan', 'mark', x, y)
1615 def scan_dragto(self, x, y):
1616 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001617 def search(self, pattern, index, stopindex=None,
1618 forwards=None, backwards=None, exact=None,
1619 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001620 args = [self._w, 'search']
1621 if forwards: args.append('-forwards')
1622 if backwards: args.append('-backwards')
1623 if exact: args.append('-exact')
1624 if regexp: args.append('-regexp')
1625 if nocase: args.append('-nocase')
1626 if count: args.append('-count'); args.append(count)
1627 if pattern[0] == '-': args.append('--')
1628 args.append(pattern)
1629 args.append(index)
1630 if stopindex: args.append(stopindex)
Guido van Rossumf9756991998-04-29 21:57:08 +00001631 return self.tk.call(tuple(args))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001632 def see(self, index):
1633 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001634 def tag_add(self, tagName, index1, index2=None):
1635 self.tk.call(
1636 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossum117a5a81998-03-27 21:26:51 +00001637 def tag_unbind(self, tagName, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001638 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001639 if funcid:
1640 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001641 def tag_bind(self, tagName, sequence, func, add=None):
1642 return self._bind((self._w, 'tag', 'bind', tagName),
1643 sequence, func, add)
1644 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001645 if option[:1] != '-':
1646 option = '-' + option
1647 if option[-1:] == '_':
1648 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001649 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001650 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001651 if type(cnf) == StringType:
1652 x = self.tk.split(self.tk.call(
1653 self._w, 'tag', 'configure', tagName, '-'+cnf))
1654 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001655 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001656 (self._w, 'tag', 'configure', tagName)
1657 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001658 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001659 def tag_delete(self, *tagNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001660 self.tk.call((self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001661 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001662 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001663 def tag_names(self, index=None):
1664 return self.tk.splitlist(
1665 self.tk.call(self._w, 'tag', 'names', index))
1666 def tag_nextrange(self, tagName, index1, index2=None):
1667 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001668 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossumf0413d41997-12-15 17:31:52 +00001669 def tag_prevrange(self, tagName, index1, index2=None):
1670 return self.tk.splitlist(self.tk.call(
1671 self._w, 'tag', 'prevrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001672 def tag_raise(self, tagName, aboveThis=None):
1673 self.tk.call(
1674 self._w, 'tag', 'raise', tagName, aboveThis)
1675 def tag_ranges(self, tagName):
1676 return self.tk.splitlist(self.tk.call(
1677 self._w, 'tag', 'ranges', tagName))
1678 def tag_remove(self, tagName, index1, index2=None):
1679 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001680 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001681 def window_cget(self, index, option):
Guido van Rossum7814ea61997-12-11 17:08:52 +00001682 if option[:1] != '-':
1683 option = '-' + option
1684 if option[-1:] == '_':
1685 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001686 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001687 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001688 if type(cnf) == StringType:
1689 x = self.tk.split(self.tk.call(
1690 self._w, 'window', 'configure',
1691 index, '-'+cnf))
1692 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001693 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001694 (self._w, 'window', 'configure', index)
1695 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001696 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001697 def window_create(self, index, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001698 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001699 (self._w, 'window', 'create', index)
1700 + self._options(cnf, kw))
1701 def window_names(self):
1702 return self.tk.splitlist(
1703 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001704 def xview(self, *what):
1705 if not what:
1706 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001707 self.tk.call((self._w, 'xview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001708 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001709 if not what:
1710 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001711 self.tk.call((self._w, 'yview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001712 def yview_pickplace(self, *what):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001713 self.tk.call((self._w, 'yview', '-pickplace') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00001714
Guido van Rossum28574b51996-10-21 15:16:51 +00001715class _setit:
1716 def __init__(self, var, value):
1717 self.__value = value
1718 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001719 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001720 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001721
1722class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001723 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001724 kw = {"borderwidth": 2, "textvariable": variable,
1725 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1726 "highlightthickness": 2}
1727 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001728 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001729 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1730 self.menuname = menu._w
1731 menu.add_command(label=value, command=_setit(variable, value))
1732 for v in values:
1733 menu.add_command(label=v, command=_setit(variable, v))
1734 self["menu"] = menu
1735
1736 def __getitem__(self, name):
1737 if name == 'menu':
1738 return self.__menu
1739 return Widget.__getitem__(self, name)
1740
1741 def destroy(self):
1742 Menubutton.destroy(self)
1743 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001744
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001745class Image:
Guido van Rossumc4570481998-03-20 20:45:49 +00001746 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001747 self.name = None
Guido van Rossumc4570481998-03-20 20:45:49 +00001748 if not master:
1749 master = _default_root
1750 if not master:
1751 raise RuntimeError, 'Too early to create image'
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001752 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001753 if not name:
1754 name = `id(self)`
1755 # The following is needed for systems where id(x)
1756 # can return a negative number, such as Linux/m68k:
1757 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001758 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1759 elif kw: cnf = kw
1760 options = ()
1761 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001762 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001763 v = self._register(v)
1764 options = options + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001765 self.tk.call(('image', 'create', imgtype, name,) + options)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001766 self.name = name
1767 def __str__(self): return self.name
1768 def __del__(self):
1769 if self.name:
1770 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001771 def __setitem__(self, key, value):
1772 self.tk.call(self.name, 'configure', '-'+key, value)
1773 def __getitem__(self, key):
1774 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001775 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001776 res = ()
1777 for k, v in _cnfmerge(kw).items():
1778 if v is not None:
1779 if k[-1] == '_': k = k[:-1]
1780 if callable(v):
1781 v = self._register(v)
1782 res = res + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001783 self.tk.call((self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001784 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001785 def height(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001786 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001787 self.tk.call('image', 'height', self.name))
1788 def type(self):
1789 return self.tk.call('image', 'type', self.name)
1790 def width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001791 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001792 self.tk.call('image', 'width', self.name))
1793
1794class PhotoImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001795 def __init__(self, name=None, cnf={}, master=None, **kw):
1796 apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001797 def blank(self):
1798 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001799 def cget(self, option):
1800 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001801 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001802 def __getitem__(self, key):
1803 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001804 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001805 def copy(self):
1806 destImage = PhotoImage()
1807 self.tk.call(destImage, 'copy', self.name)
1808 return destImage
1809 def zoom(self,x,y=''):
1810 destImage = PhotoImage()
1811 if y=='': y=x
1812 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1813 return destImage
1814 def subsample(self,x,y=''):
1815 destImage = PhotoImage()
1816 if y=='': y=x
1817 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1818 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001819 def get(self, x, y):
1820 return self.tk.call(self.name, 'get', x, y)
1821 def put(self, data, to=None):
1822 args = (self.name, 'put', data)
1823 if to:
Fred Drakeb5323991997-12-16 15:03:43 +00001824 if to[0] == '-to':
1825 to = to[1:]
1826 args = args + ('-to',) + tuple(to)
Guido van Rossumf9756991998-04-29 21:57:08 +00001827 self.tk.call(args)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001828 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001829 def write(self, filename, format=None, from_coords=None):
1830 args = (self.name, 'write', filename)
1831 if format:
1832 args = args + ('-format', format)
1833 if from_coords:
1834 args = args + ('-from',) + tuple(from_coords)
Guido van Rossumf9756991998-04-29 21:57:08 +00001835 self.tk.call(args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001836
1837class BitmapImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001838 def __init__(self, name=None, cnf={}, master=None, **kw):
1839 apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001840
1841def image_names(): return _default_root.tk.call('image', 'names')
1842def image_types(): return _default_root.tk.call('image', 'types')
1843
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001844######################################################################
1845# Extensions:
1846
1847class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001848 def __init__(self, master=None, cnf={}, **kw):
1849 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001850 self.bind('<Any-Enter>', self.tkButtonEnter)
1851 self.bind('<Any-Leave>', self.tkButtonLeave)
1852 self.bind('<1>', self.tkButtonDown)
1853 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001854
1855class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001856 def __init__(self, master=None, cnf={}, **kw):
1857 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001858 self.bind('<Any-Enter>', self.tkButtonEnter)
1859 self.bind('<Any-Leave>', self.tkButtonLeave)
1860 self.bind('<1>', self.tkButtonDown)
1861 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1862 self['fg'] = self['bg']
1863 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001864
Guido van Rossumc417ef81996-08-21 23:38:59 +00001865######################################################################
1866# Test:
1867
1868def _test():
1869 root = Tk()
1870 label = Label(root, text="Proof-of-existence test for Tk")
1871 label.pack()
1872 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001873 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001874 text="[%s]" % root.test['text']))
1875 test.pack()
1876 root.test = test
1877 quit = Button(root, text="QUIT", command=root.destroy)
1878 quit.pack()
Guido van Rossum268824e1998-06-19 04:34:19 +00001879 # The following three commands are needed so the window pops
1880 # up on top on Windows...
1881 root.iconify()
1882 root.update()
1883 root.deiconify()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001884 root.mainloop()
1885
1886if __name__ == '__main__':
1887 _test()