blob: ee2415da72a01e1602a3c034a2fccf4fb3f76538 [file] [log] [blame]
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001"""Wrapper functions for Tcl/Tk.
2
3Tkinter provides classes which allow the display, positioning and
4control of widgets. Toplevel widgets are Tk and Toplevel. Other
5widgets are Frame, Label, Entry, Text, Canvas, Button, Radiobutton,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00006Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox
Raymond Hettingerff41c482003-04-06 09:01:11 +00007LabelFrame and PanedWindow.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00008
Raymond Hettingerff41c482003-04-06 09:01:11 +00009Properties of the widgets are specified with keyword arguments.
10Keyword arguments have the same name as the corresponding resource
Martin v. Löwis2ec36272002-10-13 10:22:08 +000011under Tk.
Guido van Rossum5917ecb2000-06-29 16:30:50 +000012
13Widgets are positioned with one of the geometry managers Place, Pack
14or Grid. These managers can be called with methods place, pack, grid
15available in every Widget.
16
Guido van Rossuma0adb922001-09-01 18:29:55 +000017Actions are bound to events by resources (e.g. keyword argument
18command) or with the method bind.
Guido van Rossum5917ecb2000-06-29 16:30:50 +000019
20Example (Hello, World):
Georg Brandl14fc4272008-05-17 18:39:55 +000021import tkinter
22from tkinter.constants import *
23tk = tkinter.Tk()
24frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
Guido van Rossum5917ecb2000-06-29 16:30:50 +000025frame.pack(fill=BOTH,expand=1)
Georg Brandl14fc4272008-05-17 18:39:55 +000026label = tkinter.Label(frame, text="Hello, World")
Guido van Rossum5917ecb2000-06-29 16:30:50 +000027label.pack(fill=X, expand=1)
Georg Brandl14fc4272008-05-17 18:39:55 +000028button = tkinter.Button(frame,text="Exit",command=tk.destroy)
Guido van Rossum5917ecb2000-06-29 16:30:50 +000029button.pack(side=BOTTOM)
30tk.mainloop()
31"""
Guido van Rossum2dcf5291994-07-06 09:23:20 +000032
Serhiy Storchakad611f4c2016-06-12 00:19:44 +030033import enum
Guido van Rossumf8d579c1999-01-04 18:06:45 +000034import sys
Andrew Svetlova966c6f2012-03-21 23:52:59 +020035
Guido van Rossumf8d579c1999-01-04 18:06:45 +000036import _tkinter # If this fails your Python may not be configured for Tk
Guido van Rossum95806091997-02-15 18:33:24 +000037TclError = _tkinter.TclError
Georg Brandl14fc4272008-05-17 18:39:55 +000038from tkinter.constants import *
Serhiy Storchakab1396522013-01-15 17:56:08 +020039import re
Guido van Rossum18468821994-06-20 07:49:28 +000040
Andrew Svetlova966c6f2012-03-21 23:52:59 +020041
Martin v. Löwis8c8aa5d2002-11-26 21:39:48 +000042wantobjects = 1
Martin v. Löwisffad6332002-11-26 09:28:05 +000043
Eric S. Raymondfc170b12001-02-09 11:51:27 +000044TkVersion = float(_tkinter.TK_VERSION)
45TclVersion = float(_tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000046
Guido van Rossumd6615ab1997-08-05 02:35:01 +000047READABLE = _tkinter.READABLE
48WRITABLE = _tkinter.WRITABLE
49EXCEPTION = _tkinter.EXCEPTION
Guido van Rossumf53c86c1997-08-14 14:15:54 +000050
Fredrik Lundh06d28152000-08-09 18:03:12 +000051
Serhiy Storchakab1396522013-01-15 17:56:08 +020052_magic_re = re.compile(r'([\\{}])')
53_space_re = re.compile(r'([\s])', re.ASCII)
54
55def _join(value):
56 """Internal function."""
57 return ' '.join(map(_stringify, value))
58
59def _stringify(value):
60 """Internal function."""
61 if isinstance(value, (list, tuple)):
62 if len(value) == 1:
63 value = _stringify(value[0])
64 if value[0] == '{':
65 value = '{%s}' % value
66 else:
67 value = '{%s}' % _join(value)
68 else:
69 value = str(value)
70 if not value:
71 value = '{}'
72 elif _magic_re.search(value):
73 # add '\' before special characters and spaces
74 value = _magic_re.sub(r'\\\1', value)
75 value = _space_re.sub(r'\\\1', value)
76 elif value[0] == '"' or _space_re.search(value):
77 value = '{%s}' % value
78 return value
79
Guido van Rossum13257902007-06-07 23:15:56 +000080def _flatten(seq):
Fredrik Lundh06d28152000-08-09 18:03:12 +000081 """Internal function."""
82 res = ()
Guido van Rossum13257902007-06-07 23:15:56 +000083 for item in seq:
84 if isinstance(item, (tuple, list)):
Fredrik Lundh06d28152000-08-09 18:03:12 +000085 res = res + _flatten(item)
86 elif item is not None:
87 res = res + (item,)
88 return res
Guido van Rossum2dcf5291994-07-06 09:23:20 +000089
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +000090try: _flatten = _tkinter._flatten
91except AttributeError: pass
92
Guido van Rossum2dcf5291994-07-06 09:23:20 +000093def _cnfmerge(cnfs):
Fredrik Lundh06d28152000-08-09 18:03:12 +000094 """Internal function."""
Guido van Rossum13257902007-06-07 23:15:56 +000095 if isinstance(cnfs, dict):
Fredrik Lundh06d28152000-08-09 18:03:12 +000096 return cnfs
Guido van Rossum13257902007-06-07 23:15:56 +000097 elif isinstance(cnfs, (type(None), str)):
Fredrik Lundh06d28152000-08-09 18:03:12 +000098 return cnfs
99 else:
100 cnf = {}
101 for c in _flatten(cnfs):
102 try:
103 cnf.update(c)
Guido van Rossumb940e112007-01-10 16:19:56 +0000104 except (AttributeError, TypeError) as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000105 print("_cnfmerge: fallback due to:", msg)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000106 for k, v in c.items():
107 cnf[k] = v
108 return cnf
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000109
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +0000110try: _cnfmerge = _tkinter._cnfmerge
111except AttributeError: pass
112
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +0300113def _splitdict(tk, v, cut_minus=True, conv=None):
114 """Return a properly formatted dict built from Tcl list pairs.
115
116 If cut_minus is True, the supposed '-' prefix will be removed from
117 keys. If conv is specified, it is used to convert values.
118
119 Tcl list is expected to contain an even number of elements.
120 """
121 t = tk.splitlist(v)
122 if len(t) % 2:
123 raise RuntimeError('Tcl list representing a dict is expected '
124 'to contain an even number of elements')
125 it = iter(t)
126 dict = {}
127 for key, value in zip(it, it):
128 key = str(key)
129 if cut_minus and key[0] == '-':
130 key = key[1:]
131 if conv:
132 value = conv(value)
133 dict[key] = value
134 return dict
135
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300136
137class EventType(str, enum.Enum):
138 KeyPress = '2'
139 Key = KeyPress,
140 KeyRelease = '3'
141 ButtonPress = '4'
142 Button = ButtonPress,
143 ButtonRelease = '5'
144 Motion = '6'
145 Enter = '7'
146 Leave = '8'
147 FocusIn = '9'
148 FocusOut = '10'
149 Keymap = '11' # undocumented
150 Expose = '12'
151 GraphicsExpose = '13' # undocumented
152 NoExpose = '14' # undocumented
153 Visibility = '15'
154 Create = '16'
155 Destroy = '17'
156 Unmap = '18'
157 Map = '19'
158 MapRequest = '20'
159 Reparent = '21'
160 Configure = '22'
161 ConfigureRequest = '23'
162 Gravity = '24'
163 ResizeRequest = '25'
164 Circulate = '26'
165 CirculateRequest = '27'
166 Property = '28'
167 SelectionClear = '29' # undocumented
168 SelectionRequest = '30' # undocumented
169 Selection = '31' # undocumented
170 Colormap = '32'
171 ClientMessage = '33' # undocumented
172 Mapping = '34' # undocumented
173 VirtualEvent = '35', # undocumented
174 Activate = '36',
175 Deactivate = '37',
176 MouseWheel = '38',
177 def __str__(self):
178 return self.name
179
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000180class Event:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000181 """Container for the properties of an event.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000182
Fredrik Lundh06d28152000-08-09 18:03:12 +0000183 Instances of this type are generated if one of the following events occurs:
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000184
Fredrik Lundh06d28152000-08-09 18:03:12 +0000185 KeyPress, KeyRelease - for keyboard events
186 ButtonPress, ButtonRelease, Motion, Enter, Leave, MouseWheel - for mouse events
187 Visibility, Unmap, Map, Expose, FocusIn, FocusOut, Circulate,
188 Colormap, Gravity, Reparent, Property, Destroy, Activate,
189 Deactivate - for window events.
190
191 If a callback function for one of these events is registered
192 using bind, bind_all, bind_class, or tag_bind, the callback is
193 called with an Event as first argument. It will have the
194 following attributes (in braces are the event types for which
195 the attribute is valid):
196
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000197 serial - serial number of event
Fredrik Lundh06d28152000-08-09 18:03:12 +0000198 num - mouse button pressed (ButtonPress, ButtonRelease)
199 focus - whether the window has the focus (Enter, Leave)
200 height - height of the exposed window (Configure, Expose)
201 width - width of the exposed window (Configure, Expose)
202 keycode - keycode of the pressed key (KeyPress, KeyRelease)
203 state - state of the event as a number (ButtonPress, ButtonRelease,
204 Enter, KeyPress, KeyRelease,
205 Leave, Motion)
206 state - state as a string (Visibility)
207 time - when the event occurred
208 x - x-position of the mouse
209 y - y-position of the mouse
210 x_root - x-position of the mouse on the screen
211 (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
212 y_root - y-position of the mouse on the screen
213 (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
214 char - pressed character (KeyPress, KeyRelease)
215 send_event - see X/Windows documentation
Walter Dörwald966c2642005-11-09 17:12:43 +0000216 keysym - keysym of the event as a string (KeyPress, KeyRelease)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000217 keysym_num - keysym of the event as a number (KeyPress, KeyRelease)
218 type - type of the event as a number
219 widget - widget in which the event occurred
220 delta - delta of wheel movement (MouseWheel)
221 """
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300222 def __repr__(self):
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300223 attrs = {k: v for k, v in self.__dict__.items() if v != '??'}
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300224 if not self.char:
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300225 del attrs['char']
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300226 elif self.char != '??':
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300227 attrs['char'] = repr(self.char)
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300228 if not getattr(self, 'send_event', True):
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300229 del attrs['send_event']
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300230 if self.state == 0:
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300231 del attrs['state']
232 elif isinstance(self.state, int):
233 state = self.state
234 mods = ('Shift', 'Lock', 'Control',
235 'Mod1', 'Mod2', 'Mod3', 'Mod4', 'Mod5',
236 'Button1', 'Button2', 'Button3', 'Button4', 'Button5')
237 s = []
238 for i, n in enumerate(mods):
239 if state & (1 << i):
240 s.append(n)
241 state = state & ~((1<< len(mods)) - 1)
242 if state or not s:
243 s.append(hex(state))
244 attrs['state'] = '|'.join(s)
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300245 if self.delta == 0:
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300246 del attrs['delta']
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300247 # widget usually is known
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700248 # serial and time are not very interesting
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300249 # keysym_num duplicates keysym
250 # x_root and y_root mostly duplicate x and y
251 keys = ('send_event',
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300252 'state', 'keysym', 'keycode', 'char',
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300253 'num', 'delta', 'focus',
254 'x', 'y', 'width', 'height')
255 return '<%s event%s>' % (
256 self.type,
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300257 ''.join(' %s=%s' % (k, attrs[k]) for k in keys if k in attrs)
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300258 )
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000259
Guido van Rossumc4570481998-03-20 20:45:49 +0000260_support_default_root = 1
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000261_default_root = None
262
Guido van Rossumc4570481998-03-20 20:45:49 +0000263def NoDefaultRoot():
Fredrik Lundh06d28152000-08-09 18:03:12 +0000264 """Inhibit setting of default root window.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000265
Fredrik Lundh06d28152000-08-09 18:03:12 +0000266 Call this function to inhibit that the first instance of
267 Tk is used for windows without an explicit parent window.
268 """
269 global _support_default_root
270 _support_default_root = 0
271 global _default_root
272 _default_root = None
273 del _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +0000274
Guido van Rossum45853db1994-06-20 12:19:19 +0000275def _tkerror(err):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000276 """Internal function."""
277 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000278
Andrew Svetlov806bfad2012-12-10 00:02:31 +0200279def _exit(code=0):
Andrew Svetlov737fb892012-12-18 21:14:22 +0200280 """Internal function. Calling it will raise the exception SystemExit."""
Andrew Svetlov806bfad2012-12-10 00:02:31 +0200281 try:
282 code = int(code)
283 except ValueError:
284 pass
Collin Winterce36ad82007-08-30 01:19:48 +0000285 raise SystemExit(code)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000286
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000287_varnum = 0
288class Variable:
Guido van Rossum2cd0a652003-04-16 20:10:03 +0000289 """Class to define value holders for e.g. buttons.
290
291 Subclasses StringVar, IntVar, DoubleVar, BooleanVar are specializations
292 that constrain the type of the value returned from get()."""
Fredrik Lundh06d28152000-08-09 18:03:12 +0000293 _default = ""
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700294 _tk = None
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300295 _tclCommands = None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296 def __init__(self, master=None, value=None, name=None):
297 """Construct a variable
298
299 MASTER can be given as master widget.
300 VALUE is an optional value (defaults to "")
301 NAME is an optional Tcl name (defaults to PY_VARnum).
302
303 If NAME matches an existing variable and VALUE is omitted
304 then the existing value is retained.
Fredrik Lundh06d28152000-08-09 18:03:12 +0000305 """
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700306 # check for type of NAME parameter to override weird error message
307 # raised from Modules/_tkinter.c:SetVar like:
308 # TypeError: setvar() takes exactly 3 arguments (2 given)
309 if name is not None and not isinstance(name, str):
310 raise TypeError("name must be a string")
Fredrik Lundh06d28152000-08-09 18:03:12 +0000311 global _varnum
312 if not master:
313 master = _default_root
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300314 self._root = master._root()
Fredrik Lundh06d28152000-08-09 18:03:12 +0000315 self._tk = master.tk
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000316 if name:
317 self._name = name
318 else:
319 self._name = 'PY_VAR' + repr(_varnum)
320 _varnum += 1
Benjamin Peterson2a691a82008-03-31 01:51:45 +0000321 if value is not None:
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700322 self.initialize(value)
Serhiy Storchakad97c01f2013-12-26 20:06:05 +0200323 elif not self._tk.getboolean(self._tk.call("info", "exists", self._name)):
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700324 self.initialize(self._default)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000325 def __del__(self):
326 """Unset the variable in Tcl."""
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300327 if self._tk is None:
328 return
329 if self._tk.getboolean(self._tk.call("info", "exists", self._name)):
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700330 self._tk.globalunsetvar(self._name)
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300331 if self._tclCommands is not None:
332 for name in self._tclCommands:
333 #print '- Tkinter: deleted command', name
334 self._tk.deletecommand(name)
335 self._tclCommands = None
Fredrik Lundh06d28152000-08-09 18:03:12 +0000336 def __str__(self):
337 """Return the name of the variable in Tcl."""
338 return self._name
339 def set(self, value):
340 """Set the variable to VALUE."""
341 return self._tk.globalsetvar(self._name, value)
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700342 initialize = set
Guido van Rossum2cd0a652003-04-16 20:10:03 +0000343 def get(self):
344 """Return value of variable."""
345 return self._tk.globalgetvar(self._name)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000346
Serhiy Storchaka81221742016-06-26 09:46:57 +0300347 def _register(self, callback):
348 f = CallWrapper(callback, None, self._root).__call__
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300349 cbname = repr(id(f))
350 try:
351 callback = callback.__func__
352 except AttributeError:
353 pass
354 try:
355 cbname = cbname + callback.__name__
356 except AttributeError:
357 pass
358 self._tk.createcommand(cbname, f)
359 if self._tclCommands is None:
360 self._tclCommands = []
361 self._tclCommands.append(cbname)
Serhiy Storchaka81221742016-06-26 09:46:57 +0300362 return cbname
363
364 def trace_add(self, mode, callback):
365 """Define a trace callback for the variable.
366
367 Mode is one of "read", "write", "unset", or a list or tuple of
368 such strings.
369 Callback must be a function which is called when the variable is
370 read, written or unset.
371
372 Return the name of the callback.
373 """
374 cbname = self._register(callback)
375 self._tk.call('trace', 'add', 'variable',
376 self._name, mode, (cbname,))
377 return cbname
378
379 def trace_remove(self, mode, cbname):
380 """Delete the trace callback for a variable.
381
382 Mode is one of "read", "write", "unset" or a list or tuple of
383 such strings. Must be same as were specified in trace_add().
384 cbname is the name of the callback returned from trace_add().
385 """
386 self._tk.call('trace', 'remove', 'variable',
387 self._name, mode, cbname)
388 for m, ca in self.trace_info():
389 if self._tk.splitlist(ca)[0] == cbname:
390 break
391 else:
392 self._tk.deletecommand(cbname)
393 try:
394 self._tclCommands.remove(cbname)
395 except ValueError:
396 pass
397
398 def trace_info(self):
399 """Return all trace callback information."""
400 splitlist = self._tk.splitlist
401 return [(splitlist(k), v) for k, v in map(splitlist,
402 splitlist(self._tk.call('trace', 'info', 'variable', self._name)))]
403
404 def trace_variable(self, mode, callback):
405 """Define a trace callback for the variable.
406
407 MODE is one of "r", "w", "u" for read, write, undefine.
408 CALLBACK must be a function which is called when
409 the variable is read, written or undefined.
410
411 Return the name of the callback.
412
413 This deprecated method wraps a deprecated Tcl method that will
414 likely be removed in the future. Use trace_add() instead.
415 """
416 # TODO: Add deprecation warning
417 cbname = self._register(callback)
Serhiy Storchaka26f9feb2014-07-31 07:46:08 +0300418 self._tk.call("trace", "variable", self._name, mode, cbname)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000419 return cbname
Serhiy Storchaka81221742016-06-26 09:46:57 +0300420
Fredrik Lundh06d28152000-08-09 18:03:12 +0000421 trace = trace_variable
Serhiy Storchaka81221742016-06-26 09:46:57 +0300422
Fredrik Lundh06d28152000-08-09 18:03:12 +0000423 def trace_vdelete(self, mode, cbname):
424 """Delete the trace callback for a variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000425
Fredrik Lundh06d28152000-08-09 18:03:12 +0000426 MODE is one of "r", "w", "u" for read, write, undefine.
427 CBNAME is the name of the callback returned from trace_variable or trace.
Serhiy Storchaka81221742016-06-26 09:46:57 +0300428
429 This deprecated method wraps a deprecated Tcl method that will
430 likely be removed in the future. Use trace_remove() instead.
Fredrik Lundh06d28152000-08-09 18:03:12 +0000431 """
Serhiy Storchaka81221742016-06-26 09:46:57 +0300432 # TODO: Add deprecation warning
Serhiy Storchaka26f9feb2014-07-31 07:46:08 +0300433 self._tk.call("trace", "vdelete", self._name, mode, cbname)
Serhiy Storchaka81221742016-06-26 09:46:57 +0300434 cbname = self._tk.splitlist(cbname)[0]
435 for m, ca in self.trace_info():
436 if self._tk.splitlist(ca)[0] == cbname:
437 break
438 else:
439 self._tk.deletecommand(cbname)
440 try:
441 self._tclCommands.remove(cbname)
442 except ValueError:
443 pass
444
Fredrik Lundh06d28152000-08-09 18:03:12 +0000445 def trace_vinfo(self):
Serhiy Storchaka81221742016-06-26 09:46:57 +0300446 """Return all trace callback information.
447
448 This deprecated method wraps a deprecated Tcl method that will
449 likely be removed in the future. Use trace_info() instead.
450 """
451 # TODO: Add deprecation warning
Serhiy Storchaka6bc87b42016-06-26 00:09:19 +0300452 return [self._tk.splitlist(x) for x in self._tk.splitlist(
Serhiy Storchaka26f9feb2014-07-31 07:46:08 +0300453 self._tk.call("trace", "vinfo", self._name))]
Serhiy Storchaka81221742016-06-26 09:46:57 +0300454
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000455 def __eq__(self, other):
456 """Comparison for equality (==).
457
458 Note: if the Variable's master matters to behavior
459 also compare self._master == other._master
460 """
461 return self.__class__.__name__ == other.__class__.__name__ \
462 and self._name == other._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000463
464class StringVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000465 """Value holder for strings variables."""
466 _default = ""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000467 def __init__(self, master=None, value=None, name=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000468 """Construct a string variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000469
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000470 MASTER can be given as master widget.
471 VALUE is an optional value (defaults to "")
472 NAME is an optional Tcl name (defaults to PY_VARnum).
473
474 If NAME matches an existing variable and VALUE is omitted
475 then the existing value is retained.
476 """
477 Variable.__init__(self, master, value, name)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000478
Fredrik Lundh06d28152000-08-09 18:03:12 +0000479 def get(self):
480 """Return value of variable as string."""
Martin v. Löwisbfe175c2003-04-16 19:42:51 +0000481 value = self._tk.globalgetvar(self._name)
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000482 if isinstance(value, str):
Martin v. Löwisbfe175c2003-04-16 19:42:51 +0000483 return value
484 return str(value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000485
486class IntVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000487 """Value holder for integer variables."""
488 _default = 0
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000489 def __init__(self, master=None, value=None, name=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000490 """Construct an integer variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000491
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000492 MASTER can be given as master widget.
493 VALUE is an optional value (defaults to 0)
494 NAME is an optional Tcl name (defaults to PY_VARnum).
495
496 If NAME matches an existing variable and VALUE is omitted
497 then the existing value is retained.
498 """
499 Variable.__init__(self, master, value, name)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000500
Fredrik Lundh06d28152000-08-09 18:03:12 +0000501 def get(self):
502 """Return the value of the variable as an integer."""
Serhiy Storchaka32c0d3a2016-10-30 18:49:52 +0200503 value = self._tk.globalgetvar(self._name)
504 try:
505 return self._tk.getint(value)
506 except (TypeError, TclError):
507 return int(self._tk.getdouble(value))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000508
509class DoubleVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000510 """Value holder for float variables."""
511 _default = 0.0
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512 def __init__(self, master=None, value=None, name=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000513 """Construct a float variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000514
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000515 MASTER can be given as master widget.
516 VALUE is an optional value (defaults to 0.0)
517 NAME is an optional Tcl name (defaults to PY_VARnum).
518
519 If NAME matches an existing variable and VALUE is omitted
520 then the existing value is retained.
521 """
522 Variable.__init__(self, master, value, name)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000523
524 def get(self):
525 """Return the value of the variable as a float."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300526 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000527
528class BooleanVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000529 """Value holder for boolean variables."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000530 _default = False
531 def __init__(self, master=None, value=None, name=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000532 """Construct a boolean variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000533
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000534 MASTER can be given as master widget.
535 VALUE is an optional value (defaults to False)
536 NAME is an optional Tcl name (defaults to PY_VARnum).
537
538 If NAME matches an existing variable and VALUE is omitted
539 then the existing value is retained.
540 """
541 Variable.__init__(self, master, value, name)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000542
Serhiy Storchaka9a6e2012015-04-04 12:43:01 +0300543 def set(self, value):
544 """Set the variable to VALUE."""
545 return self._tk.globalsetvar(self._name, self._tk.getboolean(value))
546 initialize = set
547
Fredrik Lundh06d28152000-08-09 18:03:12 +0000548 def get(self):
Martin v. Löwisbfe175c2003-04-16 19:42:51 +0000549 """Return the value of the variable as a bool."""
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700550 try:
551 return self._tk.getboolean(self._tk.globalgetvar(self._name))
552 except TclError:
553 raise ValueError("invalid literal for getboolean()")
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000554
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000555def mainloop(n=0):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000556 """Run the main loop of Tcl."""
557 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000558
Guido van Rossum0132f691998-04-30 17:50:36 +0000559getint = int
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000560
Guido van Rossum0132f691998-04-30 17:50:36 +0000561getdouble = float
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000562
563def getboolean(s):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000564 """Convert true and false to integer values 1 and 0."""
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700565 try:
566 return _default_root.tk.getboolean(s)
567 except TclError:
568 raise ValueError("invalid literal for getboolean()")
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000569
Guido van Rossum368e06b1997-11-07 20:38:49 +0000570# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000571class Misc:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000572 """Internal class.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000573
Fredrik Lundh06d28152000-08-09 18:03:12 +0000574 Base class which defines methods common for interior widgets."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000575
Serhiy Storchaka8acb5682016-06-13 09:24:11 +0300576 # used for generating child widget names
577 _last_child_ids = None
578
Fredrik Lundh06d28152000-08-09 18:03:12 +0000579 # XXX font command?
580 _tclCommands = None
581 def destroy(self):
582 """Internal function.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000583
Fredrik Lundh06d28152000-08-09 18:03:12 +0000584 Delete all Tcl commands created for
585 this widget in the Tcl interpreter."""
586 if self._tclCommands is not None:
587 for name in self._tclCommands:
588 #print '- Tkinter: deleted command', name
589 self.tk.deletecommand(name)
590 self._tclCommands = None
591 def deletecommand(self, name):
592 """Internal function.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000593
Fredrik Lundh06d28152000-08-09 18:03:12 +0000594 Delete the Tcl command provided in NAME."""
595 #print '- Tkinter: deleted command', name
596 self.tk.deletecommand(name)
597 try:
598 self._tclCommands.remove(name)
599 except ValueError:
600 pass
601 def tk_strictMotif(self, boolean=None):
602 """Set Tcl internal variable, whether the look and feel
603 should adhere to Motif.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000604
Fredrik Lundh06d28152000-08-09 18:03:12 +0000605 A parameter of 1 means adhere to Motif (e.g. no color
606 change if mouse passes over slider).
607 Returns the set value."""
608 return self.tk.getboolean(self.tk.call(
609 'set', 'tk_strictMotif', boolean))
610 def tk_bisque(self):
611 """Change the color scheme to light brown as used in Tk 3.6 and before."""
612 self.tk.call('tk_bisque')
613 def tk_setPalette(self, *args, **kw):
614 """Set a new color scheme for all widget elements.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000615
Fredrik Lundh06d28152000-08-09 18:03:12 +0000616 A single color as argument will cause that all colors of Tk
617 widget elements are derived from this.
618 Alternatively several keyword parameters and its associated
619 colors can be given. The following keywords are valid:
620 activeBackground, foreground, selectColor,
621 activeForeground, highlightBackground, selectBackground,
622 background, highlightColor, selectForeground,
623 disabledForeground, insertBackground, troughColor."""
624 self.tk.call(('tk_setPalette',)
Serhiy Storchaka4cf4f3a2013-01-02 00:03:58 +0200625 + _flatten(args) + _flatten(list(kw.items())))
Fredrik Lundh06d28152000-08-09 18:03:12 +0000626 def wait_variable(self, name='PY_VAR'):
627 """Wait until the variable is modified.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000628
Fredrik Lundh06d28152000-08-09 18:03:12 +0000629 A parameter of type IntVar, StringVar, DoubleVar or
630 BooleanVar must be given."""
631 self.tk.call('tkwait', 'variable', name)
632 waitvar = wait_variable # XXX b/w compat
633 def wait_window(self, window=None):
634 """Wait until a WIDGET is destroyed.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000635
Fredrik Lundh06d28152000-08-09 18:03:12 +0000636 If no parameter is given self is used."""
Fred Drake132dce22000-12-12 23:11:42 +0000637 if window is None:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000638 window = self
639 self.tk.call('tkwait', 'window', window._w)
640 def wait_visibility(self, window=None):
641 """Wait until the visibility of a WIDGET changes
642 (e.g. it appears).
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000643
Fredrik Lundh06d28152000-08-09 18:03:12 +0000644 If no parameter is given self is used."""
Fred Drake132dce22000-12-12 23:11:42 +0000645 if window is None:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000646 window = self
647 self.tk.call('tkwait', 'visibility', window._w)
648 def setvar(self, name='PY_VAR', value='1'):
649 """Set Tcl variable NAME to VALUE."""
650 self.tk.setvar(name, value)
651 def getvar(self, name='PY_VAR'):
652 """Return value of Tcl variable NAME."""
653 return self.tk.getvar(name)
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300654
655 def getint(self, s):
656 try:
657 return self.tk.getint(s)
658 except TclError as exc:
659 raise ValueError(str(exc))
660
661 def getdouble(self, s):
662 try:
663 return self.tk.getdouble(s)
664 except TclError as exc:
665 raise ValueError(str(exc))
666
Fredrik Lundh06d28152000-08-09 18:03:12 +0000667 def getboolean(self, s):
Neal Norwitz6e5be222003-04-17 13:13:55 +0000668 """Return a boolean value for Tcl boolean values true and false given as parameter."""
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700669 try:
670 return self.tk.getboolean(s)
671 except TclError:
672 raise ValueError("invalid literal for getboolean()")
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300673
Fredrik Lundh06d28152000-08-09 18:03:12 +0000674 def focus_set(self):
675 """Direct input focus to this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000676
Fredrik Lundh06d28152000-08-09 18:03:12 +0000677 If the application currently does not have the focus
678 this widget will get the focus if the application gets
679 the focus through the window manager."""
680 self.tk.call('focus', self._w)
681 focus = focus_set # XXX b/w compat?
682 def focus_force(self):
683 """Direct input focus to this widget even if the
684 application does not have the focus. Use with
685 caution!"""
686 self.tk.call('focus', '-force', self._w)
687 def focus_get(self):
688 """Return the widget which has currently the focus in the
689 application.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000690
Fredrik Lundh06d28152000-08-09 18:03:12 +0000691 Use focus_displayof to allow working with several
692 displays. Return None if application does not have
693 the focus."""
694 name = self.tk.call('focus')
695 if name == 'none' or not name: return None
696 return self._nametowidget(name)
697 def focus_displayof(self):
698 """Return the widget which has currently the focus on the
699 display where this widget is located.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000700
Fredrik Lundh06d28152000-08-09 18:03:12 +0000701 Return None if the application does not have the focus."""
702 name = self.tk.call('focus', '-displayof', self._w)
703 if name == 'none' or not name: return None
704 return self._nametowidget(name)
705 def focus_lastfor(self):
706 """Return the widget which would have the focus if top level
707 for this widget gets the focus from the window manager."""
708 name = self.tk.call('focus', '-lastfor', self._w)
709 if name == 'none' or not name: return None
710 return self._nametowidget(name)
711 def tk_focusFollowsMouse(self):
712 """The widget under mouse will get automatically focus. Can not
713 be disabled easily."""
714 self.tk.call('tk_focusFollowsMouse')
715 def tk_focusNext(self):
716 """Return the next widget in the focus order which follows
717 widget which has currently the focus.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000718
Fredrik Lundh06d28152000-08-09 18:03:12 +0000719 The focus order first goes to the next child, then to
720 the children of the child recursively and then to the
721 next sibling which is higher in the stacking order. A
722 widget is omitted if it has the takefocus resource set
723 to 0."""
724 name = self.tk.call('tk_focusNext', self._w)
725 if not name: return None
726 return self._nametowidget(name)
727 def tk_focusPrev(self):
728 """Return previous widget in the focus order. See tk_focusNext for details."""
729 name = self.tk.call('tk_focusPrev', self._w)
730 if not name: return None
731 return self._nametowidget(name)
732 def after(self, ms, func=None, *args):
733 """Call function once after given time.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000734
Fredrik Lundh06d28152000-08-09 18:03:12 +0000735 MS specifies the time in milliseconds. FUNC gives the
736 function which shall be called. Additional parameters
737 are given as parameters to the function call. Return
738 identifier to cancel scheduling with after_cancel."""
739 if not func:
740 # I'd rather use time.sleep(ms*0.001)
741 self.tk.call('after', ms)
742 else:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000743 def callit():
Fredrik Lundh06d28152000-08-09 18:03:12 +0000744 try:
Raymond Hettingerff41c482003-04-06 09:01:11 +0000745 func(*args)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000746 finally:
747 try:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000748 self.deletecommand(name)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000749 except TclError:
750 pass
Serhiy Storchakad00aff22014-08-24 09:07:47 +0300751 callit.__name__ = func.__name__
Fredrik Lundh06d28152000-08-09 18:03:12 +0000752 name = self._register(callit)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000753 return self.tk.call('after', ms, name)
754 def after_idle(self, func, *args):
755 """Call FUNC once if the Tcl main loop has no event to
756 process.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000757
Fredrik Lundh06d28152000-08-09 18:03:12 +0000758 Return an identifier to cancel the scheduling with
759 after_cancel."""
Raymond Hettingerff41c482003-04-06 09:01:11 +0000760 return self.after('idle', func, *args)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000761 def after_cancel(self, id):
762 """Cancel scheduling of function identified with ID.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000763
Fredrik Lundh06d28152000-08-09 18:03:12 +0000764 Identifier returned by after or after_idle must be
765 given as first parameter."""
Martin v. Löwis0f9e5252003-06-07 19:52:38 +0000766 try:
Neal Norwitz3c0f2c92003-07-01 21:12:47 +0000767 data = self.tk.call('after', 'info', id)
768 # In Tk 8.3, splitlist returns: (script, type)
769 # In Tk 8.4, splitlist may return (script, type) or (script,)
770 script = self.tk.splitlist(data)[0]
Martin v. Löwis0f9e5252003-06-07 19:52:38 +0000771 self.deletecommand(script)
772 except TclError:
773 pass
Fredrik Lundh06d28152000-08-09 18:03:12 +0000774 self.tk.call('after', 'cancel', id)
775 def bell(self, displayof=0):
776 """Ring a display's bell."""
777 self.tk.call(('bell',) + self._displayof(displayof))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000778
Fredrik Lundh06d28152000-08-09 18:03:12 +0000779 # Clipboard handling:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780 def clipboard_get(self, **kw):
781 """Retrieve data from the clipboard on window's display.
782
783 The window keyword defaults to the root window of the Tkinter
784 application.
785
786 The type keyword specifies the form in which the data is
787 to be returned and should be an atom name such as STRING
Ned Deily4d377d92012-05-15 18:08:11 -0700788 or FILE_NAME. Type defaults to STRING, except on X11, where the default
789 is to try UTF8_STRING and fall back to STRING.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790
791 This command is equivalent to:
792
793 selection_get(CLIPBOARD)
794 """
Ned Deily4d377d92012-05-15 18:08:11 -0700795 if 'type' not in kw and self._windowingsystem == 'x11':
796 try:
797 kw['type'] = 'UTF8_STRING'
798 return self.tk.call(('clipboard', 'get') + self._options(kw))
799 except TclError:
800 del kw['type']
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000801 return self.tk.call(('clipboard', 'get') + self._options(kw))
802
Fredrik Lundh06d28152000-08-09 18:03:12 +0000803 def clipboard_clear(self, **kw):
804 """Clear the data in the Tk clipboard.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000805
Fredrik Lundh06d28152000-08-09 18:03:12 +0000806 A widget specified for the optional displayof keyword
807 argument specifies the target display."""
Guido van Rossume014a132006-08-19 16:53:45 +0000808 if 'displayof' not in kw: kw['displayof'] = self._w
Fredrik Lundh06d28152000-08-09 18:03:12 +0000809 self.tk.call(('clipboard', 'clear') + self._options(kw))
810 def clipboard_append(self, string, **kw):
811 """Append STRING to the Tk clipboard.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000812
Fredrik Lundh06d28152000-08-09 18:03:12 +0000813 A widget specified at the optional displayof keyword
814 argument specifies the target display. The clipboard
815 can be retrieved with selection_get."""
Guido van Rossume014a132006-08-19 16:53:45 +0000816 if 'displayof' not in kw: kw['displayof'] = self._w
Fredrik Lundh06d28152000-08-09 18:03:12 +0000817 self.tk.call(('clipboard', 'append') + self._options(kw)
818 + ('--', string))
819 # XXX grab current w/o window argument
820 def grab_current(self):
821 """Return widget which has currently the grab in this application
822 or None."""
823 name = self.tk.call('grab', 'current', self._w)
824 if not name: return None
825 return self._nametowidget(name)
826 def grab_release(self):
827 """Release grab for this widget if currently set."""
828 self.tk.call('grab', 'release', self._w)
829 def grab_set(self):
830 """Set grab for this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000831
Fredrik Lundh06d28152000-08-09 18:03:12 +0000832 A grab directs all events to this and descendant
833 widgets in the application."""
834 self.tk.call('grab', 'set', self._w)
835 def grab_set_global(self):
836 """Set global grab for this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000837
Fredrik Lundh06d28152000-08-09 18:03:12 +0000838 A global grab directs all events to this and
839 descendant widgets on the display. Use with caution -
840 other applications do not get events anymore."""
841 self.tk.call('grab', 'set', '-global', self._w)
842 def grab_status(self):
843 """Return None, "local" or "global" if this widget has
844 no, a local or a global grab."""
845 status = self.tk.call('grab', 'status', self._w)
846 if status == 'none': status = None
847 return status
Fredrik Lundh06d28152000-08-09 18:03:12 +0000848 def option_add(self, pattern, value, priority = None):
849 """Set a VALUE (second parameter) for an option
850 PATTERN (first parameter).
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000851
Fredrik Lundh06d28152000-08-09 18:03:12 +0000852 An optional third parameter gives the numeric priority
853 (defaults to 80)."""
854 self.tk.call('option', 'add', pattern, value, priority)
855 def option_clear(self):
856 """Clear the option database.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000857
Fredrik Lundh06d28152000-08-09 18:03:12 +0000858 It will be reloaded if option_add is called."""
859 self.tk.call('option', 'clear')
860 def option_get(self, name, className):
861 """Return the value for an option NAME for this widget
862 with CLASSNAME.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000863
Fredrik Lundh06d28152000-08-09 18:03:12 +0000864 Values with higher priority override lower values."""
865 return self.tk.call('option', 'get', self._w, name, className)
866 def option_readfile(self, fileName, priority = None):
867 """Read file FILENAME into the option database.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000868
Fredrik Lundh06d28152000-08-09 18:03:12 +0000869 An optional second parameter gives the numeric
870 priority."""
871 self.tk.call('option', 'readfile', fileName, priority)
872 def selection_clear(self, **kw):
873 """Clear the current X selection."""
Guido van Rossume014a132006-08-19 16:53:45 +0000874 if 'displayof' not in kw: kw['displayof'] = self._w
Fredrik Lundh06d28152000-08-09 18:03:12 +0000875 self.tk.call(('selection', 'clear') + self._options(kw))
876 def selection_get(self, **kw):
877 """Return the contents of the current X selection.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000878
Fredrik Lundh06d28152000-08-09 18:03:12 +0000879 A keyword parameter selection specifies the name of
880 the selection and defaults to PRIMARY. A keyword
881 parameter displayof specifies a widget on the display
Ned Deily4d377d92012-05-15 18:08:11 -0700882 to use. A keyword parameter type specifies the form of data to be
883 fetched, defaulting to STRING except on X11, where UTF8_STRING is tried
884 before STRING."""
Guido van Rossume014a132006-08-19 16:53:45 +0000885 if 'displayof' not in kw: kw['displayof'] = self._w
Ned Deily4d377d92012-05-15 18:08:11 -0700886 if 'type' not in kw and self._windowingsystem == 'x11':
887 try:
888 kw['type'] = 'UTF8_STRING'
889 return self.tk.call(('selection', 'get') + self._options(kw))
890 except TclError:
891 del kw['type']
Fredrik Lundh06d28152000-08-09 18:03:12 +0000892 return self.tk.call(('selection', 'get') + self._options(kw))
893 def selection_handle(self, command, **kw):
894 """Specify a function COMMAND to call if the X
895 selection owned by this widget is queried by another
896 application.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000897
Fredrik Lundh06d28152000-08-09 18:03:12 +0000898 This function must return the contents of the
899 selection. The function will be called with the
900 arguments OFFSET and LENGTH which allows the chunking
901 of very long selections. The following keyword
902 parameters can be provided:
903 selection - name of the selection (default PRIMARY),
904 type - type of the selection (e.g. STRING, FILE_NAME)."""
905 name = self._register(command)
906 self.tk.call(('selection', 'handle') + self._options(kw)
907 + (self._w, name))
908 def selection_own(self, **kw):
909 """Become owner of X selection.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000910
Fredrik Lundh06d28152000-08-09 18:03:12 +0000911 A keyword parameter selection specifies the name of
912 the selection (default PRIMARY)."""
913 self.tk.call(('selection', 'own') +
914 self._options(kw) + (self._w,))
915 def selection_own_get(self, **kw):
916 """Return owner of X selection.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000917
Fredrik Lundh06d28152000-08-09 18:03:12 +0000918 The following keyword parameter can
919 be provided:
920 selection - name of the selection (default PRIMARY),
921 type - type of the selection (e.g. STRING, FILE_NAME)."""
Guido van Rossume014a132006-08-19 16:53:45 +0000922 if 'displayof' not in kw: kw['displayof'] = self._w
Fredrik Lundh06d28152000-08-09 18:03:12 +0000923 name = self.tk.call(('selection', 'own') + self._options(kw))
924 if not name: return None
925 return self._nametowidget(name)
926 def send(self, interp, cmd, *args):
927 """Send Tcl command CMD to different interpreter INTERP to be executed."""
928 return self.tk.call(('send', interp, cmd) + args)
929 def lower(self, belowThis=None):
930 """Lower this widget in the stacking order."""
931 self.tk.call('lower', self._w, belowThis)
932 def tkraise(self, aboveThis=None):
933 """Raise this widget in the stacking order."""
934 self.tk.call('raise', self._w, aboveThis)
935 lift = tkraise
Fredrik Lundh06d28152000-08-09 18:03:12 +0000936 def winfo_atom(self, name, displayof=0):
937 """Return integer which represents atom NAME."""
938 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300939 return self.tk.getint(self.tk.call(args))
Fredrik Lundh06d28152000-08-09 18:03:12 +0000940 def winfo_atomname(self, id, displayof=0):
941 """Return name of atom with identifier ID."""
942 args = ('winfo', 'atomname') \
943 + self._displayof(displayof) + (id,)
944 return self.tk.call(args)
945 def winfo_cells(self):
946 """Return number of cells in the colormap for this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300947 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +0000948 self.tk.call('winfo', 'cells', self._w))
949 def winfo_children(self):
950 """Return a list of all widgets which are children of this widget."""
Martin v. Löwisf2041b82002-03-27 17:15:57 +0000951 result = []
952 for child in self.tk.splitlist(
953 self.tk.call('winfo', 'children', self._w)):
954 try:
955 # Tcl sometimes returns extra windows, e.g. for
956 # menus; those need to be skipped
957 result.append(self._nametowidget(child))
958 except KeyError:
959 pass
960 return result
961
Fredrik Lundh06d28152000-08-09 18:03:12 +0000962 def winfo_class(self):
963 """Return window class name of this widget."""
964 return self.tk.call('winfo', 'class', self._w)
965 def winfo_colormapfull(self):
966 """Return true if at the last color request the colormap was full."""
967 return self.tk.getboolean(
968 self.tk.call('winfo', 'colormapfull', self._w))
969 def winfo_containing(self, rootX, rootY, displayof=0):
970 """Return the widget which is at the root coordinates ROOTX, ROOTY."""
971 args = ('winfo', 'containing') \
972 + self._displayof(displayof) + (rootX, rootY)
973 name = self.tk.call(args)
974 if not name: return None
975 return self._nametowidget(name)
976 def winfo_depth(self):
977 """Return the number of bits per pixel."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300978 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
Fredrik Lundh06d28152000-08-09 18:03:12 +0000979 def winfo_exists(self):
980 """Return true if this widget exists."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300981 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +0000982 self.tk.call('winfo', 'exists', self._w))
983 def winfo_fpixels(self, number):
984 """Return the number of pixels for the given distance NUMBER
985 (e.g. "3c") as float."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300986 return self.tk.getdouble(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +0000987 'winfo', 'fpixels', self._w, number))
988 def winfo_geometry(self):
989 """Return geometry string for this widget in the form "widthxheight+X+Y"."""
990 return self.tk.call('winfo', 'geometry', self._w)
991 def winfo_height(self):
992 """Return height of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300993 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +0000994 self.tk.call('winfo', 'height', self._w))
995 def winfo_id(self):
996 """Return identifier ID for this widget."""
Serhiy Storchakab9d67242015-11-09 14:43:31 +0200997 return int(self.tk.call('winfo', 'id', self._w), 0)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000998 def winfo_interps(self, displayof=0):
999 """Return the name of all Tcl interpreters for this display."""
1000 args = ('winfo', 'interps') + self._displayof(displayof)
1001 return self.tk.splitlist(self.tk.call(args))
1002 def winfo_ismapped(self):
1003 """Return true if this widget is mapped."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001004 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001005 self.tk.call('winfo', 'ismapped', self._w))
1006 def winfo_manager(self):
1007 """Return the window mananger name for this widget."""
1008 return self.tk.call('winfo', 'manager', self._w)
1009 def winfo_name(self):
1010 """Return the name of this widget."""
1011 return self.tk.call('winfo', 'name', self._w)
1012 def winfo_parent(self):
1013 """Return the name of the parent of this widget."""
1014 return self.tk.call('winfo', 'parent', self._w)
1015 def winfo_pathname(self, id, displayof=0):
1016 """Return the pathname of the widget given by ID."""
1017 args = ('winfo', 'pathname') \
1018 + self._displayof(displayof) + (id,)
1019 return self.tk.call(args)
1020 def winfo_pixels(self, number):
1021 """Rounded integer value of winfo_fpixels."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001022 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001023 self.tk.call('winfo', 'pixels', self._w, number))
1024 def winfo_pointerx(self):
1025 """Return the x coordinate of the pointer on the root window."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001026 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001027 self.tk.call('winfo', 'pointerx', self._w))
1028 def winfo_pointerxy(self):
1029 """Return a tuple of x and y coordinates of the pointer on the root window."""
1030 return self._getints(
1031 self.tk.call('winfo', 'pointerxy', self._w))
1032 def winfo_pointery(self):
1033 """Return the y coordinate of the pointer on the root window."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001034 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001035 self.tk.call('winfo', 'pointery', self._w))
1036 def winfo_reqheight(self):
1037 """Return requested height of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001038 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001039 self.tk.call('winfo', 'reqheight', self._w))
1040 def winfo_reqwidth(self):
1041 """Return requested width of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001042 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001043 self.tk.call('winfo', 'reqwidth', self._w))
1044 def winfo_rgb(self, color):
1045 """Return tuple of decimal values for red, green, blue for
1046 COLOR in this widget."""
1047 return self._getints(
1048 self.tk.call('winfo', 'rgb', self._w, color))
1049 def winfo_rootx(self):
1050 """Return x coordinate of upper left corner of this widget on the
1051 root window."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001052 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001053 self.tk.call('winfo', 'rootx', self._w))
1054 def winfo_rooty(self):
1055 """Return y coordinate of upper left corner of this widget on the
1056 root window."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001057 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001058 self.tk.call('winfo', 'rooty', self._w))
1059 def winfo_screen(self):
1060 """Return the screen name of this widget."""
1061 return self.tk.call('winfo', 'screen', self._w)
1062 def winfo_screencells(self):
1063 """Return the number of the cells in the colormap of the screen
1064 of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001065 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001066 self.tk.call('winfo', 'screencells', self._w))
1067 def winfo_screendepth(self):
1068 """Return the number of bits per pixel of the root window of the
1069 screen of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001070 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001071 self.tk.call('winfo', 'screendepth', self._w))
1072 def winfo_screenheight(self):
1073 """Return the number of pixels of the height of the screen of this widget
1074 in pixel."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001075 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001076 self.tk.call('winfo', 'screenheight', self._w))
1077 def winfo_screenmmheight(self):
1078 """Return the number of pixels of the height of the screen of
1079 this widget in mm."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001080 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001081 self.tk.call('winfo', 'screenmmheight', self._w))
1082 def winfo_screenmmwidth(self):
1083 """Return the number of pixels of the width of the screen of
1084 this widget in mm."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001085 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001086 self.tk.call('winfo', 'screenmmwidth', self._w))
1087 def winfo_screenvisual(self):
1088 """Return one of the strings directcolor, grayscale, pseudocolor,
1089 staticcolor, staticgray, or truecolor for the default
1090 colormodel of this screen."""
1091 return self.tk.call('winfo', 'screenvisual', self._w)
1092 def winfo_screenwidth(self):
1093 """Return the number of pixels of the width of the screen of
1094 this widget in pixel."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001095 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001096 self.tk.call('winfo', 'screenwidth', self._w))
1097 def winfo_server(self):
1098 """Return information of the X-Server of the screen of this widget in
1099 the form "XmajorRminor vendor vendorVersion"."""
1100 return self.tk.call('winfo', 'server', self._w)
1101 def winfo_toplevel(self):
1102 """Return the toplevel widget of this widget."""
1103 return self._nametowidget(self.tk.call(
1104 'winfo', 'toplevel', self._w))
1105 def winfo_viewable(self):
1106 """Return true if the widget and all its higher ancestors are mapped."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001107 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001108 self.tk.call('winfo', 'viewable', self._w))
1109 def winfo_visual(self):
1110 """Return one of the strings directcolor, grayscale, pseudocolor,
1111 staticcolor, staticgray, or truecolor for the
1112 colormodel of this widget."""
1113 return self.tk.call('winfo', 'visual', self._w)
1114 def winfo_visualid(self):
1115 """Return the X identifier for the visual for this widget."""
1116 return self.tk.call('winfo', 'visualid', self._w)
Serhiy Storchaka6bc87b42016-06-26 00:09:19 +03001117 def winfo_visualsavailable(self, includeids=False):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001118 """Return a list of all visuals available for the screen
1119 of this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001120
Fredrik Lundh06d28152000-08-09 18:03:12 +00001121 Each item in the list consists of a visual name (see winfo_visual), a
Serhiy Storchaka6bc87b42016-06-26 00:09:19 +03001122 depth and if includeids is true is given also the X identifier."""
1123 data = self.tk.call('winfo', 'visualsavailable', self._w,
1124 'includeids' if includeids else None)
1125 data = [self.tk.splitlist(x) for x in self.tk.splitlist(data)]
1126 return [self.__winfo_parseitem(x) for x in data]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001127 def __winfo_parseitem(self, t):
1128 """Internal function."""
1129 return t[:1] + tuple(map(self.__winfo_getint, t[1:]))
1130 def __winfo_getint(self, x):
1131 """Internal function."""
Eric S. Raymondfc170b12001-02-09 11:51:27 +00001132 return int(x, 0)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001133 def winfo_vrootheight(self):
1134 """Return the height of the virtual root window associated with this
1135 widget in pixels. If there is no virtual root window return the
1136 height of the screen."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001137 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001138 self.tk.call('winfo', 'vrootheight', self._w))
1139 def winfo_vrootwidth(self):
1140 """Return the width of the virtual root window associated with this
1141 widget in pixel. If there is no virtual root window return the
1142 width of the screen."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001143 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001144 self.tk.call('winfo', 'vrootwidth', self._w))
1145 def winfo_vrootx(self):
1146 """Return the x offset of the virtual root relative to the root
1147 window of the screen of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001148 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001149 self.tk.call('winfo', 'vrootx', self._w))
1150 def winfo_vrooty(self):
1151 """Return the y offset of the virtual root relative to the root
1152 window of the screen of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001153 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001154 self.tk.call('winfo', 'vrooty', self._w))
1155 def winfo_width(self):
1156 """Return the width of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001157 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001158 self.tk.call('winfo', 'width', self._w))
1159 def winfo_x(self):
1160 """Return the x coordinate of the upper left corner of this widget
1161 in the parent."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001162 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001163 self.tk.call('winfo', 'x', self._w))
1164 def winfo_y(self):
1165 """Return the y coordinate of the upper left corner of this widget
1166 in the parent."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001167 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001168 self.tk.call('winfo', 'y', self._w))
1169 def update(self):
1170 """Enter event loop until all pending events have been processed by Tcl."""
1171 self.tk.call('update')
1172 def update_idletasks(self):
1173 """Enter event loop until all idle callbacks have been called. This
1174 will update the display of windows but not process events caused by
1175 the user."""
1176 self.tk.call('update', 'idletasks')
1177 def bindtags(self, tagList=None):
1178 """Set or get the list of bindtags for this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001179
Fredrik Lundh06d28152000-08-09 18:03:12 +00001180 With no argument return the list of all bindtags associated with
1181 this widget. With a list of strings as argument the bindtags are
1182 set to this list. The bindtags determine in which order events are
1183 processed (see bind)."""
1184 if tagList is None:
1185 return self.tk.splitlist(
1186 self.tk.call('bindtags', self._w))
1187 else:
1188 self.tk.call('bindtags', self._w, tagList)
1189 def _bind(self, what, sequence, func, add, needcleanup=1):
1190 """Internal function."""
Guido van Rossum13257902007-06-07 23:15:56 +00001191 if isinstance(func, str):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001192 self.tk.call(what + (sequence, func))
1193 elif func:
1194 funcid = self._register(func, self._substitute,
1195 needcleanup)
1196 cmd = ('%sif {"[%s %s]" == "break"} break\n'
1197 %
1198 (add and '+' or '',
Martin v. Löwisc8718c12001-08-09 16:57:33 +00001199 funcid, self._subst_format_str))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001200 self.tk.call(what + (sequence, cmd))
1201 return funcid
1202 elif sequence:
1203 return self.tk.call(what + (sequence,))
1204 else:
1205 return self.tk.splitlist(self.tk.call(what))
1206 def bind(self, sequence=None, func=None, add=None):
1207 """Bind to this widget at event SEQUENCE a call to function FUNC.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001208
Fredrik Lundh06d28152000-08-09 18:03:12 +00001209 SEQUENCE is a string of concatenated event
1210 patterns. An event pattern is of the form
1211 <MODIFIER-MODIFIER-TYPE-DETAIL> where MODIFIER is one
1212 of Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4,
1213 Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3,
1214 B3, Alt, Button4, B4, Double, Button5, B5 Triple,
1215 Mod1, M1. TYPE is one of Activate, Enter, Map,
1216 ButtonPress, Button, Expose, Motion, ButtonRelease
1217 FocusIn, MouseWheel, Circulate, FocusOut, Property,
1218 Colormap, Gravity Reparent, Configure, KeyPress, Key,
1219 Unmap, Deactivate, KeyRelease Visibility, Destroy,
1220 Leave and DETAIL is the button number for ButtonPress,
1221 ButtonRelease and DETAIL is the Keysym for KeyPress and
1222 KeyRelease. Examples are
1223 <Control-Button-1> for pressing Control and mouse button 1 or
1224 <Alt-A> for pressing A and the Alt key (KeyPress can be omitted).
1225 An event pattern can also be a virtual event of the form
1226 <<AString>> where AString can be arbitrary. This
1227 event can be generated by event_generate.
1228 If events are concatenated they must appear shortly
1229 after each other.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001230
Fredrik Lundh06d28152000-08-09 18:03:12 +00001231 FUNC will be called if the event sequence occurs with an
1232 instance of Event as argument. If the return value of FUNC is
1233 "break" no further bound function is invoked.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001234
Fredrik Lundh06d28152000-08-09 18:03:12 +00001235 An additional boolean parameter ADD specifies whether FUNC will
1236 be called additionally to the other bound function or whether
1237 it will replace the previous function.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001238
Fredrik Lundh06d28152000-08-09 18:03:12 +00001239 Bind will return an identifier to allow deletion of the bound function with
1240 unbind without memory leak.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001241
Fredrik Lundh06d28152000-08-09 18:03:12 +00001242 If FUNC or SEQUENCE is omitted the bound function or list
1243 of bound events are returned."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001244
Fredrik Lundh06d28152000-08-09 18:03:12 +00001245 return self._bind(('bind', self._w), sequence, func, add)
1246 def unbind(self, sequence, funcid=None):
1247 """Unbind for this widget for event SEQUENCE the
1248 function identified with FUNCID."""
1249 self.tk.call('bind', self._w, sequence, '')
1250 if funcid:
1251 self.deletecommand(funcid)
1252 def bind_all(self, sequence=None, func=None, add=None):
1253 """Bind to all widgets at an event SEQUENCE a call to function FUNC.
1254 An additional boolean parameter ADD specifies whether FUNC will
1255 be called additionally to the other bound function or whether
1256 it will replace the previous function. See bind for the return value."""
1257 return self._bind(('bind', 'all'), sequence, func, add, 0)
1258 def unbind_all(self, sequence):
1259 """Unbind for all widgets for event SEQUENCE all functions."""
1260 self.tk.call('bind', 'all' , sequence, '')
1261 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001262
Fredrik Lundh06d28152000-08-09 18:03:12 +00001263 """Bind to widgets with bindtag CLASSNAME at event
1264 SEQUENCE a call of function FUNC. An additional
1265 boolean parameter ADD specifies whether FUNC will be
1266 called additionally to the other bound function or
1267 whether it will replace the previous function. See bind for
1268 the return value."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001269
Fredrik Lundh06d28152000-08-09 18:03:12 +00001270 return self._bind(('bind', className), sequence, func, add, 0)
1271 def unbind_class(self, className, sequence):
Serhiy Storchakad65c9492015-11-02 14:10:23 +02001272 """Unbind for all widgets with bindtag CLASSNAME for event SEQUENCE
Fredrik Lundh06d28152000-08-09 18:03:12 +00001273 all functions."""
1274 self.tk.call('bind', className , sequence, '')
1275 def mainloop(self, n=0):
1276 """Call the mainloop of Tk."""
1277 self.tk.mainloop(n)
1278 def quit(self):
1279 """Quit the Tcl interpreter. All widgets will be destroyed."""
1280 self.tk.quit()
1281 def _getints(self, string):
1282 """Internal function."""
1283 if string:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001284 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001285 def _getdoubles(self, string):
1286 """Internal function."""
1287 if string:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001288 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001289 def _getboolean(self, string):
1290 """Internal function."""
1291 if string:
1292 return self.tk.getboolean(string)
1293 def _displayof(self, displayof):
1294 """Internal function."""
1295 if displayof:
1296 return ('-displayof', displayof)
1297 if displayof is None:
1298 return ('-displayof', self._w)
1299 return ()
Ned Deily4d377d92012-05-15 18:08:11 -07001300 @property
1301 def _windowingsystem(self):
1302 """Internal function."""
1303 try:
1304 return self._root()._windowingsystem_cached
1305 except AttributeError:
1306 ws = self._root()._windowingsystem_cached = \
1307 self.tk.call('tk', 'windowingsystem')
1308 return ws
Fredrik Lundh06d28152000-08-09 18:03:12 +00001309 def _options(self, cnf, kw = None):
1310 """Internal function."""
1311 if kw:
1312 cnf = _cnfmerge((cnf, kw))
1313 else:
1314 cnf = _cnfmerge(cnf)
1315 res = ()
1316 for k, v in cnf.items():
1317 if v is not None:
1318 if k[-1] == '_': k = k[:-1]
Florent Xicluna5d1155c2011-10-28 14:45:05 +02001319 if callable(v):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001320 v = self._register(v)
Georg Brandlbf1eb632008-05-29 07:19:00 +00001321 elif isinstance(v, (tuple, list)):
Georg Brandl3b550032008-06-03 10:25:47 +00001322 nv = []
Georg Brandlbf1eb632008-05-29 07:19:00 +00001323 for item in v:
Georg Brandl3b550032008-06-03 10:25:47 +00001324 if isinstance(item, int):
1325 nv.append(str(item))
1326 elif isinstance(item, str):
Serhiy Storchakab1396522013-01-15 17:56:08 +02001327 nv.append(_stringify(item))
Georg Brandl3b550032008-06-03 10:25:47 +00001328 else:
Georg Brandlbf1eb632008-05-29 07:19:00 +00001329 break
1330 else:
Georg Brandl3b550032008-06-03 10:25:47 +00001331 v = ' '.join(nv)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001332 res = res + ('-'+k, v)
1333 return res
1334 def nametowidget(self, name):
1335 """Return the Tkinter instance of a widget identified by
1336 its Tcl name NAME."""
Martin v. Löwiscdfae162008-08-02 07:23:15 +00001337 name = str(name).split('.')
Fredrik Lundh06d28152000-08-09 18:03:12 +00001338 w = self
Martin v. Löwiscdfae162008-08-02 07:23:15 +00001339
1340 if not name[0]:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001341 w = w._root()
1342 name = name[1:]
Martin v. Löwiscdfae162008-08-02 07:23:15 +00001343
1344 for n in name:
1345 if not n:
1346 break
1347 w = w.children[n]
1348
Fredrik Lundh06d28152000-08-09 18:03:12 +00001349 return w
1350 _nametowidget = nametowidget
1351 def _register(self, func, subst=None, needcleanup=1):
1352 """Return a newly created Tcl function. If this
1353 function is called, the Python function FUNC will
1354 be executed. An optional function SUBST can
1355 be given which will be executed before FUNC."""
1356 f = CallWrapper(func, subst, self).__call__
Walter Dörwald70a6b492004-02-12 17:35:32 +00001357 name = repr(id(f))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001358 try:
Christian Heimesff737952007-11-27 10:40:20 +00001359 func = func.__func__
Fredrik Lundh06d28152000-08-09 18:03:12 +00001360 except AttributeError:
1361 pass
1362 try:
1363 name = name + func.__name__
1364 except AttributeError:
1365 pass
1366 self.tk.createcommand(name, f)
1367 if needcleanup:
1368 if self._tclCommands is None:
1369 self._tclCommands = []
1370 self._tclCommands.append(name)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001371 return name
1372 register = _register
1373 def _root(self):
1374 """Internal function."""
1375 w = self
1376 while w.master: w = w.master
1377 return w
1378 _subst_format = ('%#', '%b', '%f', '%h', '%k',
1379 '%s', '%t', '%w', '%x', '%y',
1380 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y', '%D')
Martin v. Löwisc8718c12001-08-09 16:57:33 +00001381 _subst_format_str = " ".join(_subst_format)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001382 def _substitute(self, *args):
1383 """Internal function."""
1384 if len(args) != len(self._subst_format): return args
1385 getboolean = self.tk.getboolean
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001386
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001387 getint = self.tk.getint
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001388 def getint_event(s):
1389 """Tk changed behavior in 8.4.2, returning "??" rather more often."""
1390 try:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001391 return getint(s)
1392 except (ValueError, TclError):
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001393 return s
1394
Fredrik Lundh06d28152000-08-09 18:03:12 +00001395 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y, D = args
1396 # Missing: (a, c, d, m, o, v, B, R)
1397 e = Event()
Martin Panterf05641642016-05-08 13:48:10 +00001398 # serial field: valid for all events
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001399 # number of button: ButtonPress and ButtonRelease events only
1400 # height field: Configure, ConfigureRequest, Create,
1401 # ResizeRequest, and Expose events only
1402 # keycode field: KeyPress and KeyRelease events only
1403 # time field: "valid for events that contain a time field"
1404 # width field: Configure, ConfigureRequest, Create, ResizeRequest,
1405 # and Expose events only
Martin Panter4c359642016-05-08 13:53:41 +00001406 # x field: "valid for events that contain an x field"
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001407 # y field: "valid for events that contain a y field"
1408 # keysym as decimal: KeyPress and KeyRelease events only
1409 # x_root, y_root fields: ButtonPress, ButtonRelease, KeyPress,
Martin Panterf05641642016-05-08 13:48:10 +00001410 # KeyRelease, and Motion events
Fredrik Lundh06d28152000-08-09 18:03:12 +00001411 e.serial = getint(nsign)
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001412 e.num = getint_event(b)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001413 try: e.focus = getboolean(f)
1414 except TclError: pass
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001415 e.height = getint_event(h)
1416 e.keycode = getint_event(k)
1417 e.state = getint_event(s)
1418 e.time = getint_event(t)
1419 e.width = getint_event(w)
1420 e.x = getint_event(x)
1421 e.y = getint_event(y)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001422 e.char = A
1423 try: e.send_event = getboolean(E)
1424 except TclError: pass
1425 e.keysym = K
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001426 e.keysym_num = getint_event(N)
Serhiy Storchakad611f4c2016-06-12 00:19:44 +03001427 try:
1428 e.type = EventType(T)
1429 except ValueError:
1430 e.type = T
Fredrik Lundh06d28152000-08-09 18:03:12 +00001431 try:
1432 e.widget = self._nametowidget(W)
1433 except KeyError:
1434 e.widget = W
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001435 e.x_root = getint_event(X)
1436 e.y_root = getint_event(Y)
Fredrik Lundha249f162000-09-07 15:05:09 +00001437 try:
1438 e.delta = getint(D)
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001439 except (ValueError, TclError):
Fredrik Lundha249f162000-09-07 15:05:09 +00001440 e.delta = 0
Fredrik Lundh06d28152000-08-09 18:03:12 +00001441 return (e,)
1442 def _report_exception(self):
1443 """Internal function."""
Neal Norwitzac3625f2006-03-17 05:49:33 +00001444 exc, val, tb = sys.exc_info()
Fredrik Lundh06d28152000-08-09 18:03:12 +00001445 root = self._root()
1446 root.report_callback_exception(exc, val, tb)
Serhiy Storchaka848972c2013-12-25 16:35:38 +02001447
1448 def _getconfigure(self, *args):
1449 """Call Tcl configure command and return the result as a dict."""
1450 cnf = {}
1451 for x in self.tk.splitlist(self.tk.call(*args)):
1452 x = self.tk.splitlist(x)
1453 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1454 return cnf
1455
1456 def _getconfigure1(self, *args):
1457 x = self.tk.splitlist(self.tk.call(*args))
1458 return (x[0][1:],) + x[1:]
1459
Martin v. Löwis6ce13152002-10-10 14:36:13 +00001460 def _configure(self, cmd, cnf, kw):
1461 """Internal function."""
1462 if kw:
1463 cnf = _cnfmerge((cnf, kw))
1464 elif cnf:
1465 cnf = _cnfmerge(cnf)
1466 if cnf is None:
Serhiy Storchaka848972c2013-12-25 16:35:38 +02001467 return self._getconfigure(_flatten((self._w, cmd)))
Guido van Rossum13257902007-06-07 23:15:56 +00001468 if isinstance(cnf, str):
Serhiy Storchaka848972c2013-12-25 16:35:38 +02001469 return self._getconfigure1(_flatten((self._w, cmd, '-'+cnf)))
Martin v. Löwis6ce13152002-10-10 14:36:13 +00001470 self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001471 # These used to be defined in Widget:
1472 def configure(self, cnf=None, **kw):
1473 """Configure resources of a widget.
Barry Warsaw107e6231998-12-15 00:44:15 +00001474
Fredrik Lundh06d28152000-08-09 18:03:12 +00001475 The values for resources are specified as keyword
1476 arguments. To get an overview about
1477 the allowed keyword arguments call the method keys.
1478 """
Martin v. Löwis6ce13152002-10-10 14:36:13 +00001479 return self._configure('configure', cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001480 config = configure
1481 def cget(self, key):
1482 """Return the resource value for a KEY given as string."""
1483 return self.tk.call(self._w, 'cget', '-' + key)
1484 __getitem__ = cget
1485 def __setitem__(self, key, value):
1486 self.configure({key: value})
1487 def keys(self):
1488 """Return a list of all resource names of this widget."""
Serhiy Storchaka42a43662016-03-09 10:33:51 +02001489 splitlist = self.tk.splitlist
1490 return [splitlist(x)[0][1:] for x in
1491 splitlist(self.tk.call(self._w, 'configure'))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001492 def __str__(self):
1493 """Return the window path name of this widget."""
1494 return self._w
Serhiy Storchakabcc17462014-04-04 15:45:02 +03001495
1496 def __repr__(self):
1497 return '<%s.%s object %s>' % (
1498 self.__class__.__module__, self.__class__.__qualname__, self._w)
1499
Fredrik Lundh06d28152000-08-09 18:03:12 +00001500 # Pack methods that apply to the master
1501 _noarg_ = ['_noarg_']
1502 def pack_propagate(self, flag=_noarg_):
1503 """Set or get the status for propagation of geometry information.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001504
Fredrik Lundh06d28152000-08-09 18:03:12 +00001505 A boolean argument specifies whether the geometry information
1506 of the slaves will determine the size of this widget. If no argument
1507 is given the current setting will be returned.
1508 """
1509 if flag is Misc._noarg_:
1510 return self._getboolean(self.tk.call(
1511 'pack', 'propagate', self._w))
1512 else:
1513 self.tk.call('pack', 'propagate', self._w, flag)
1514 propagate = pack_propagate
1515 def pack_slaves(self):
1516 """Return a list of all slaves of this widget
1517 in its packing order."""
Alexander Belopolsky022f0492010-11-22 19:40:51 +00001518 return [self._nametowidget(x) for x in
1519 self.tk.splitlist(
1520 self.tk.call('pack', 'slaves', self._w))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001521 slaves = pack_slaves
1522 # Place method that applies to the master
1523 def place_slaves(self):
1524 """Return a list of all slaves of this widget
1525 in its packing order."""
Alexander Belopolsky022f0492010-11-22 19:40:51 +00001526 return [self._nametowidget(x) for x in
1527 self.tk.splitlist(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001528 self.tk.call(
Alexander Belopolsky022f0492010-11-22 19:40:51 +00001529 'place', 'slaves', self._w))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001530 # Grid methods that apply to the master
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001531 def grid_anchor(self, anchor=None): # new in Tk 8.5
1532 """The anchor value controls how to place the grid within the
1533 master when no row/column has any weight.
1534
1535 The default anchor is nw."""
1536 self.tk.call('grid', 'anchor', self._w, anchor)
1537 anchor = grid_anchor
Fredrik Lundh06d28152000-08-09 18:03:12 +00001538 def grid_bbox(self, column=None, row=None, col2=None, row2=None):
1539 """Return a tuple of integer coordinates for the bounding
1540 box of this widget controlled by the geometry manager grid.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001541
Fredrik Lundh06d28152000-08-09 18:03:12 +00001542 If COLUMN, ROW is given the bounding box applies from
1543 the cell with row and column 0 to the specified
1544 cell. If COL2 and ROW2 are given the bounding box
1545 starts at that cell.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001546
Fredrik Lundh06d28152000-08-09 18:03:12 +00001547 The returned integers specify the offset of the upper left
1548 corner in the master widget and the width and height.
1549 """
1550 args = ('grid', 'bbox', self._w)
1551 if column is not None and row is not None:
1552 args = args + (column, row)
1553 if col2 is not None and row2 is not None:
1554 args = args + (col2, row2)
Raymond Hettingerff41c482003-04-06 09:01:11 +00001555 return self._getints(self.tk.call(*args)) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00001556 bbox = grid_bbox
Serhiy Storchakae9597762014-02-19 18:34:05 +02001557
1558 def _gridconvvalue(self, value):
1559 if isinstance(value, (str, _tkinter.Tcl_Obj)):
1560 try:
1561 svalue = str(value)
1562 if not svalue:
1563 return None
1564 elif '.' in svalue:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001565 return self.tk.getdouble(svalue)
Serhiy Storchakae9597762014-02-19 18:34:05 +02001566 else:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001567 return self.tk.getint(svalue)
1568 except (ValueError, TclError):
Serhiy Storchakae9597762014-02-19 18:34:05 +02001569 pass
1570 return value
1571
Fredrik Lundh06d28152000-08-09 18:03:12 +00001572 def _grid_configure(self, command, index, cnf, kw):
1573 """Internal function."""
Guido van Rossum13257902007-06-07 23:15:56 +00001574 if isinstance(cnf, str) and not kw:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001575 if cnf[-1:] == '_':
1576 cnf = cnf[:-1]
1577 if cnf[:1] != '-':
1578 cnf = '-'+cnf
1579 options = (cnf,)
1580 else:
1581 options = self._options(cnf, kw)
1582 if not options:
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +03001583 return _splitdict(
1584 self.tk,
1585 self.tk.call('grid', command, self._w, index),
1586 conv=self._gridconvvalue)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001587 res = self.tk.call(
1588 ('grid', command, self._w, index)
1589 + options)
1590 if len(options) == 1:
Serhiy Storchakae9597762014-02-19 18:34:05 +02001591 return self._gridconvvalue(res)
1592
Fredrik Lundh06d28152000-08-09 18:03:12 +00001593 def grid_columnconfigure(self, index, cnf={}, **kw):
1594 """Configure column INDEX of a grid.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001595
Fredrik Lundh06d28152000-08-09 18:03:12 +00001596 Valid resources are minsize (minimum size of the column),
1597 weight (how much does additional space propagate to this column)
1598 and pad (how much space to let additionally)."""
1599 return self._grid_configure('columnconfigure', index, cnf, kw)
1600 columnconfigure = grid_columnconfigure
Martin v. Löwisdc579092001-10-13 09:33:51 +00001601 def grid_location(self, x, y):
1602 """Return a tuple of column and row which identify the cell
1603 at which the pixel at position X and Y inside the master
1604 widget is located."""
1605 return self._getints(
1606 self.tk.call(
1607 'grid', 'location', self._w, x, y)) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00001608 def grid_propagate(self, flag=_noarg_):
1609 """Set or get the status for propagation of geometry information.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001610
Fredrik Lundh06d28152000-08-09 18:03:12 +00001611 A boolean argument specifies whether the geometry information
1612 of the slaves will determine the size of this widget. If no argument
1613 is given, the current setting will be returned.
1614 """
1615 if flag is Misc._noarg_:
1616 return self._getboolean(self.tk.call(
1617 'grid', 'propagate', self._w))
1618 else:
1619 self.tk.call('grid', 'propagate', self._w, flag)
1620 def grid_rowconfigure(self, index, cnf={}, **kw):
1621 """Configure row INDEX of a grid.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001622
Fredrik Lundh06d28152000-08-09 18:03:12 +00001623 Valid resources are minsize (minimum size of the row),
1624 weight (how much does additional space propagate to this row)
1625 and pad (how much space to let additionally)."""
1626 return self._grid_configure('rowconfigure', index, cnf, kw)
1627 rowconfigure = grid_rowconfigure
1628 def grid_size(self):
1629 """Return a tuple of the number of column and rows in the grid."""
1630 return self._getints(
1631 self.tk.call('grid', 'size', self._w)) or None
1632 size = grid_size
1633 def grid_slaves(self, row=None, column=None):
1634 """Return a list of all slaves of this widget
1635 in its packing order."""
1636 args = ()
1637 if row is not None:
1638 args = args + ('-row', row)
1639 if column is not None:
1640 args = args + ('-column', column)
Alexander Belopolsky022f0492010-11-22 19:40:51 +00001641 return [self._nametowidget(x) for x in
1642 self.tk.splitlist(self.tk.call(
1643 ('grid', 'slaves', self._w) + args))]
Guido van Rossum80f8be81997-12-02 19:51:39 +00001644
Fredrik Lundh06d28152000-08-09 18:03:12 +00001645 # Support for the "event" command, new in Tk 4.2.
1646 # By Case Roole.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001647
Fredrik Lundh06d28152000-08-09 18:03:12 +00001648 def event_add(self, virtual, *sequences):
1649 """Bind a virtual event VIRTUAL (of the form <<Name>>)
1650 to an event SEQUENCE such that the virtual event is triggered
1651 whenever SEQUENCE occurs."""
1652 args = ('event', 'add', virtual) + sequences
1653 self.tk.call(args)
Guido van Rossumc2966511998-04-10 19:16:10 +00001654
Fredrik Lundh06d28152000-08-09 18:03:12 +00001655 def event_delete(self, virtual, *sequences):
1656 """Unbind a virtual event VIRTUAL from SEQUENCE."""
1657 args = ('event', 'delete', virtual) + sequences
1658 self.tk.call(args)
Guido van Rossumc2966511998-04-10 19:16:10 +00001659
Fredrik Lundh06d28152000-08-09 18:03:12 +00001660 def event_generate(self, sequence, **kw):
1661 """Generate an event SEQUENCE. Additional
1662 keyword arguments specify parameter of the event
1663 (e.g. x, y, rootx, rooty)."""
1664 args = ('event', 'generate', self._w, sequence)
1665 for k, v in kw.items():
1666 args = args + ('-%s' % k, str(v))
1667 self.tk.call(args)
1668
1669 def event_info(self, virtual=None):
1670 """Return a list of all virtual events or the information
1671 about the SEQUENCE bound to the virtual event VIRTUAL."""
1672 return self.tk.splitlist(
1673 self.tk.call('event', 'info', virtual))
1674
1675 # Image related commands
1676
1677 def image_names(self):
1678 """Return a list of all existing image names."""
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02001679 return self.tk.splitlist(self.tk.call('image', 'names'))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001680
1681 def image_types(self):
1682 """Return a list of all available image types (e.g. phote bitmap)."""
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02001683 return self.tk.splitlist(self.tk.call('image', 'types'))
Guido van Rossumc2966511998-04-10 19:16:10 +00001684
Guido van Rossum80f8be81997-12-02 19:51:39 +00001685
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001686class CallWrapper:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001687 """Internal class. Stores function to call when some user
1688 defined Tcl function is called e.g. after an event occurred."""
1689 def __init__(self, func, subst, widget):
1690 """Store FUNC, SUBST and WIDGET as members."""
1691 self.func = func
1692 self.subst = subst
1693 self.widget = widget
1694 def __call__(self, *args):
1695 """Apply first function SUBST to arguments, than FUNC."""
1696 try:
1697 if self.subst:
Raymond Hettingerff41c482003-04-06 09:01:11 +00001698 args = self.subst(*args)
1699 return self.func(*args)
Andrew Svetloveb0abce2012-12-03 16:13:07 +02001700 except SystemExit:
1701 raise
Fredrik Lundh06d28152000-08-09 18:03:12 +00001702 except:
1703 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +00001704
Guido van Rossume365a591998-05-01 19:48:20 +00001705
Guilherme Polo1fff0082009-08-14 15:05:30 +00001706class XView:
1707 """Mix-in class for querying and changing the horizontal position
1708 of a widget's window."""
1709
1710 def xview(self, *args):
1711 """Query and change the horizontal position of the view."""
1712 res = self.tk.call(self._w, 'xview', *args)
1713 if not args:
1714 return self._getdoubles(res)
1715
1716 def xview_moveto(self, fraction):
1717 """Adjusts the view in the window so that FRACTION of the
1718 total width of the canvas is off-screen to the left."""
1719 self.tk.call(self._w, 'xview', 'moveto', fraction)
1720
1721 def xview_scroll(self, number, what):
1722 """Shift the x-view according to NUMBER which is measured in "units"
1723 or "pages" (WHAT)."""
1724 self.tk.call(self._w, 'xview', 'scroll', number, what)
1725
1726
1727class YView:
1728 """Mix-in class for querying and changing the vertical position
1729 of a widget's window."""
1730
1731 def yview(self, *args):
1732 """Query and change the vertical position of the view."""
1733 res = self.tk.call(self._w, 'yview', *args)
1734 if not args:
1735 return self._getdoubles(res)
1736
1737 def yview_moveto(self, fraction):
1738 """Adjusts the view in the window so that FRACTION of the
1739 total height of the canvas is off-screen to the top."""
1740 self.tk.call(self._w, 'yview', 'moveto', fraction)
1741
1742 def yview_scroll(self, number, what):
1743 """Shift the y-view according to NUMBER which is measured in
1744 "units" or "pages" (WHAT)."""
1745 self.tk.call(self._w, 'yview', 'scroll', number, what)
1746
1747
Guido van Rossum18468821994-06-20 07:49:28 +00001748class Wm:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001749 """Provides functions for the communication with the window manager."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00001750
Fredrik Lundh06d28152000-08-09 18:03:12 +00001751 def wm_aspect(self,
1752 minNumer=None, minDenom=None,
1753 maxNumer=None, maxDenom=None):
1754 """Instruct the window manager to set the aspect ratio (width/height)
1755 of this widget to be between MINNUMER/MINDENOM and MAXNUMER/MAXDENOM. Return a tuple
1756 of the actual values if no argument is given."""
1757 return self._getints(
1758 self.tk.call('wm', 'aspect', self._w,
1759 minNumer, minDenom,
1760 maxNumer, maxDenom))
1761 aspect = wm_aspect
Raymond Hettingerff41c482003-04-06 09:01:11 +00001762
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001763 def wm_attributes(self, *args):
1764 """This subcommand returns or sets platform specific attributes
Raymond Hettingerff41c482003-04-06 09:01:11 +00001765
1766 The first form returns a list of the platform specific flags and
1767 their values. The second form returns the value for the specific
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001768 option. The third form sets one or more of the values. The values
1769 are as follows:
Raymond Hettingerff41c482003-04-06 09:01:11 +00001770
1771 On Windows, -disabled gets or sets whether the window is in a
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001772 disabled state. -toolwindow gets or sets the style of the window
Raymond Hettingerff41c482003-04-06 09:01:11 +00001773 to toolwindow (as defined in the MSDN). -topmost gets or sets
1774 whether this is a topmost window (displays above all other
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001775 windows).
Raymond Hettingerff41c482003-04-06 09:01:11 +00001776
1777 On Macintosh, XXXXX
1778
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001779 On Unix, there are currently no special attribute values.
1780 """
1781 args = ('wm', 'attributes', self._w) + args
1782 return self.tk.call(args)
1783 attributes=wm_attributes
Raymond Hettingerff41c482003-04-06 09:01:11 +00001784
Fredrik Lundh06d28152000-08-09 18:03:12 +00001785 def wm_client(self, name=None):
1786 """Store NAME in WM_CLIENT_MACHINE property of this widget. Return
1787 current value."""
1788 return self.tk.call('wm', 'client', self._w, name)
1789 client = wm_client
1790 def wm_colormapwindows(self, *wlist):
1791 """Store list of window names (WLIST) into WM_COLORMAPWINDOWS property
1792 of this widget. This list contains windows whose colormaps differ from their
1793 parents. Return current list of widgets if WLIST is empty."""
1794 if len(wlist) > 1:
1795 wlist = (wlist,) # Tk needs a list of windows here
1796 args = ('wm', 'colormapwindows', self._w) + wlist
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02001797 if wlist:
1798 self.tk.call(args)
1799 else:
1800 return [self._nametowidget(x)
1801 for x in self.tk.splitlist(self.tk.call(args))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001802 colormapwindows = wm_colormapwindows
1803 def wm_command(self, value=None):
1804 """Store VALUE in WM_COMMAND property. It is the command
1805 which shall be used to invoke the application. Return current
1806 command if VALUE is None."""
1807 return self.tk.call('wm', 'command', self._w, value)
1808 command = wm_command
1809 def wm_deiconify(self):
1810 """Deiconify this widget. If it was never mapped it will not be mapped.
1811 On Windows it will raise this widget and give it the focus."""
1812 return self.tk.call('wm', 'deiconify', self._w)
1813 deiconify = wm_deiconify
1814 def wm_focusmodel(self, model=None):
1815 """Set focus model to MODEL. "active" means that this widget will claim
1816 the focus itself, "passive" means that the window manager shall give
1817 the focus. Return current focus model if MODEL is None."""
1818 return self.tk.call('wm', 'focusmodel', self._w, model)
1819 focusmodel = wm_focusmodel
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001820 def wm_forget(self, window): # new in Tk 8.5
1821 """The window will be unmappend from the screen and will no longer
1822 be managed by wm. toplevel windows will be treated like frame
1823 windows once they are no longer managed by wm, however, the menu
1824 option configuration will be remembered and the menus will return
1825 once the widget is managed again."""
1826 self.tk.call('wm', 'forget', window)
1827 forget = wm_forget
Fredrik Lundh06d28152000-08-09 18:03:12 +00001828 def wm_frame(self):
1829 """Return identifier for decorative frame of this widget if present."""
1830 return self.tk.call('wm', 'frame', self._w)
1831 frame = wm_frame
1832 def wm_geometry(self, newGeometry=None):
1833 """Set geometry to NEWGEOMETRY of the form =widthxheight+x+y. Return
1834 current value if None is given."""
1835 return self.tk.call('wm', 'geometry', self._w, newGeometry)
1836 geometry = wm_geometry
1837 def wm_grid(self,
1838 baseWidth=None, baseHeight=None,
1839 widthInc=None, heightInc=None):
1840 """Instruct the window manager that this widget shall only be
1841 resized on grid boundaries. WIDTHINC and HEIGHTINC are the width and
1842 height of a grid unit in pixels. BASEWIDTH and BASEHEIGHT are the
1843 number of grid units requested in Tk_GeometryRequest."""
1844 return self._getints(self.tk.call(
1845 'wm', 'grid', self._w,
1846 baseWidth, baseHeight, widthInc, heightInc))
1847 grid = wm_grid
1848 def wm_group(self, pathName=None):
1849 """Set the group leader widgets for related widgets to PATHNAME. Return
1850 the group leader of this widget if None is given."""
1851 return self.tk.call('wm', 'group', self._w, pathName)
1852 group = wm_group
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001853 def wm_iconbitmap(self, bitmap=None, default=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001854 """Set bitmap for the iconified widget to BITMAP. Return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001855 the bitmap if None is given.
1856
1857 Under Windows, the DEFAULT parameter can be used to set the icon
1858 for the widget and any descendents that don't have an icon set
1859 explicitly. DEFAULT can be the relative path to a .ico file
1860 (example: root.iconbitmap(default='myicon.ico') ). See Tk
1861 documentation for more information."""
1862 if default:
1863 return self.tk.call('wm', 'iconbitmap', self._w, '-default', default)
1864 else:
1865 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001866 iconbitmap = wm_iconbitmap
1867 def wm_iconify(self):
1868 """Display widget as icon."""
1869 return self.tk.call('wm', 'iconify', self._w)
1870 iconify = wm_iconify
1871 def wm_iconmask(self, bitmap=None):
1872 """Set mask for the icon bitmap of this widget. Return the
1873 mask if None is given."""
1874 return self.tk.call('wm', 'iconmask', self._w, bitmap)
1875 iconmask = wm_iconmask
1876 def wm_iconname(self, newName=None):
1877 """Set the name of the icon for this widget. Return the name if
1878 None is given."""
1879 return self.tk.call('wm', 'iconname', self._w, newName)
1880 iconname = wm_iconname
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001881 def wm_iconphoto(self, default=False, *args): # new in Tk 8.5
1882 """Sets the titlebar icon for this window based on the named photo
1883 images passed through args. If default is True, this is applied to
1884 all future created toplevels as well.
1885
1886 The data in the images is taken as a snapshot at the time of
1887 invocation. If the images are later changed, this is not reflected
1888 to the titlebar icons. Multiple images are accepted to allow
1889 different images sizes to be provided. The window manager may scale
1890 provided icons to an appropriate size.
1891
1892 On Windows, the images are packed into a Windows icon structure.
1893 This will override an icon specified to wm_iconbitmap, and vice
1894 versa.
1895
1896 On X, the images are arranged into the _NET_WM_ICON X property,
1897 which most modern window managers support. An icon specified by
Berker Peksag4882cac2015-04-14 09:30:01 +03001898 wm_iconbitmap may exist simultaneously.
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001899
1900 On Macintosh, this currently does nothing."""
1901 if default:
1902 self.tk.call('wm', 'iconphoto', self._w, "-default", *args)
1903 else:
1904 self.tk.call('wm', 'iconphoto', self._w, *args)
1905 iconphoto = wm_iconphoto
Fredrik Lundh06d28152000-08-09 18:03:12 +00001906 def wm_iconposition(self, x=None, y=None):
1907 """Set the position of the icon of this widget to X and Y. Return
1908 a tuple of the current values of X and X if None is given."""
1909 return self._getints(self.tk.call(
1910 'wm', 'iconposition', self._w, x, y))
1911 iconposition = wm_iconposition
1912 def wm_iconwindow(self, pathName=None):
1913 """Set widget PATHNAME to be displayed instead of icon. Return the current
1914 value if None is given."""
1915 return self.tk.call('wm', 'iconwindow', self._w, pathName)
1916 iconwindow = wm_iconwindow
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001917 def wm_manage(self, widget): # new in Tk 8.5
1918 """The widget specified will become a stand alone top-level window.
1919 The window will be decorated with the window managers title bar,
1920 etc."""
1921 self.tk.call('wm', 'manage', widget)
1922 manage = wm_manage
Fredrik Lundh06d28152000-08-09 18:03:12 +00001923 def wm_maxsize(self, width=None, height=None):
1924 """Set max WIDTH and HEIGHT for this widget. If the window is gridded
1925 the values are given in grid units. Return the current values if None
1926 is given."""
1927 return self._getints(self.tk.call(
1928 'wm', 'maxsize', self._w, width, height))
1929 maxsize = wm_maxsize
1930 def wm_minsize(self, width=None, height=None):
1931 """Set min WIDTH and HEIGHT for this widget. If the window is gridded
1932 the values are given in grid units. Return the current values if None
1933 is given."""
1934 return self._getints(self.tk.call(
1935 'wm', 'minsize', self._w, width, height))
1936 minsize = wm_minsize
1937 def wm_overrideredirect(self, boolean=None):
1938 """Instruct the window manager to ignore this widget
1939 if BOOLEAN is given with 1. Return the current value if None
1940 is given."""
1941 return self._getboolean(self.tk.call(
1942 'wm', 'overrideredirect', self._w, boolean))
1943 overrideredirect = wm_overrideredirect
1944 def wm_positionfrom(self, who=None):
1945 """Instruct the window manager that the position of this widget shall
1946 be defined by the user if WHO is "user", and by its own policy if WHO is
1947 "program"."""
1948 return self.tk.call('wm', 'positionfrom', self._w, who)
1949 positionfrom = wm_positionfrom
1950 def wm_protocol(self, name=None, func=None):
1951 """Bind function FUNC to command NAME for this widget.
1952 Return the function bound to NAME if None is given. NAME could be
1953 e.g. "WM_SAVE_YOURSELF" or "WM_DELETE_WINDOW"."""
Florent Xicluna5d1155c2011-10-28 14:45:05 +02001954 if callable(func):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001955 command = self._register(func)
1956 else:
1957 command = func
1958 return self.tk.call(
1959 'wm', 'protocol', self._w, name, command)
1960 protocol = wm_protocol
1961 def wm_resizable(self, width=None, height=None):
1962 """Instruct the window manager whether this width can be resized
1963 in WIDTH or HEIGHT. Both values are boolean values."""
1964 return self.tk.call('wm', 'resizable', self._w, width, height)
1965 resizable = wm_resizable
1966 def wm_sizefrom(self, who=None):
1967 """Instruct the window manager that the size of this widget shall
1968 be defined by the user if WHO is "user", and by its own policy if WHO is
1969 "program"."""
1970 return self.tk.call('wm', 'sizefrom', self._w, who)
1971 sizefrom = wm_sizefrom
Fredrik Lundh289ad8f2000-08-09 19:11:59 +00001972 def wm_state(self, newstate=None):
1973 """Query or set the state of this widget as one of normal, icon,
1974 iconic (see wm_iconwindow), withdrawn, or zoomed (Windows only)."""
1975 return self.tk.call('wm', 'state', self._w, newstate)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001976 state = wm_state
1977 def wm_title(self, string=None):
1978 """Set the title of this widget."""
1979 return self.tk.call('wm', 'title', self._w, string)
1980 title = wm_title
1981 def wm_transient(self, master=None):
1982 """Instruct the window manager that this widget is transient
1983 with regard to widget MASTER."""
1984 return self.tk.call('wm', 'transient', self._w, master)
1985 transient = wm_transient
1986 def wm_withdraw(self):
1987 """Withdraw this widget from the screen such that it is unmapped
1988 and forgotten by the window manager. Re-draw it with wm_deiconify."""
1989 return self.tk.call('wm', 'withdraw', self._w)
1990 withdraw = wm_withdraw
Guido van Rossume365a591998-05-01 19:48:20 +00001991
Guido van Rossum18468821994-06-20 07:49:28 +00001992
1993class Tk(Misc, Wm):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001994 """Toplevel widget of Tk which represents mostly the main window
Ezio Melotti42da6632011-03-15 05:18:48 +02001995 of an application. It has an associated Tcl interpreter."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00001996 _w = '.'
Martin v. Löwis9441c072004-08-03 18:36:25 +00001997 def __init__(self, screenName=None, baseName=None, className='Tk',
1998 useTk=1, sync=0, use=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001999 """Return a new Toplevel widget on screen SCREENNAME. A new Tcl interpreter will
2000 be created. BASENAME will be used for the identification of the profile file (see
2001 readprofile).
2002 It is constructed from sys.argv[0] without extensions if None is given. CLASSNAME
2003 is the name of the widget class."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002004 self.master = None
2005 self.children = {}
David Aschere2b4b322004-02-18 05:59:53 +00002006 self._tkloaded = 0
2007 # to avoid recursions in the getattr code in case of failure, we
2008 # ensure that self.tk is always _something_.
Tim Peters182b5ac2004-07-18 06:16:08 +00002009 self.tk = None
Fredrik Lundh06d28152000-08-09 18:03:12 +00002010 if baseName is None:
Florent Xicluna54540ec2011-11-04 08:29:17 +01002011 import os
Fredrik Lundh06d28152000-08-09 18:03:12 +00002012 baseName = os.path.basename(sys.argv[0])
2013 baseName, ext = os.path.splitext(baseName)
Brett Cannonf299abd2015-04-13 14:21:02 -04002014 if ext not in ('.py', '.pyc'):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002015 baseName = baseName + ext
David Aschere2b4b322004-02-18 05:59:53 +00002016 interactive = 0
Martin v. Löwis9441c072004-08-03 18:36:25 +00002017 self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
David Aschere2b4b322004-02-18 05:59:53 +00002018 if useTk:
2019 self._loadtk()
Antoine Pitrou7ec3a322012-12-09 14:46:18 +01002020 if not sys.flags.ignore_environment:
2021 # Issue #16248: Honor the -E flag to avoid code injection.
2022 self.readprofile(baseName, className)
David Aschere2b4b322004-02-18 05:59:53 +00002023 def loadtk(self):
2024 if not self._tkloaded:
2025 self.tk.loadtk()
2026 self._loadtk()
2027 def _loadtk(self):
2028 self._tkloaded = 1
2029 global _default_root
Fredrik Lundh06d28152000-08-09 18:03:12 +00002030 # Version sanity checks
2031 tk_version = self.tk.getvar('tk_version')
2032 if tk_version != _tkinter.TK_VERSION:
Collin Winterce36ad82007-08-30 01:19:48 +00002033 raise RuntimeError("tk.h version (%s) doesn't match libtk.a version (%s)"
2034 % (_tkinter.TK_VERSION, tk_version))
Martin v. Löwis54895972003-05-24 11:37:15 +00002035 # Under unknown circumstances, tcl_version gets coerced to float
2036 tcl_version = str(self.tk.getvar('tcl_version'))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002037 if tcl_version != _tkinter.TCL_VERSION:
Collin Winterce36ad82007-08-30 01:19:48 +00002038 raise RuntimeError("tcl.h version (%s) doesn't match libtcl.a version (%s)" \
2039 % (_tkinter.TCL_VERSION, tcl_version))
Martin v. Löwis4afe1542005-03-01 08:09:28 +00002040 # Create and register the tkerror and exit commands
2041 # We need to inline parts of _register here, _ register
2042 # would register differently-named commands.
2043 if self._tclCommands is None:
2044 self._tclCommands = []
Fredrik Lundh06d28152000-08-09 18:03:12 +00002045 self.tk.createcommand('tkerror', _tkerror)
2046 self.tk.createcommand('exit', _exit)
Martin v. Löwis4afe1542005-03-01 08:09:28 +00002047 self._tclCommands.append('tkerror')
2048 self._tclCommands.append('exit')
Fredrik Lundh06d28152000-08-09 18:03:12 +00002049 if _support_default_root and not _default_root:
2050 _default_root = self
2051 self.protocol("WM_DELETE_WINDOW", self.destroy)
2052 def destroy(self):
2053 """Destroy this and all descendants widgets. This will
2054 end the application of this Tcl interpreter."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002055 for c in list(self.children.values()): c.destroy()
Fredrik Lundh06d28152000-08-09 18:03:12 +00002056 self.tk.call('destroy', self._w)
2057 Misc.destroy(self)
2058 global _default_root
2059 if _support_default_root and _default_root is self:
2060 _default_root = None
2061 def readprofile(self, baseName, className):
2062 """Internal function. It reads BASENAME.tcl and CLASSNAME.tcl into
Neal Norwitz01688022007-08-12 00:43:29 +00002063 the Tcl Interpreter and calls exec on the contents of BASENAME.py and
2064 CLASSNAME.py if such a file exists in the home directory."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002065 import os
Guido van Rossume014a132006-08-19 16:53:45 +00002066 if 'HOME' in os.environ: home = os.environ['HOME']
Fredrik Lundh06d28152000-08-09 18:03:12 +00002067 else: home = os.curdir
2068 class_tcl = os.path.join(home, '.%s.tcl' % className)
2069 class_py = os.path.join(home, '.%s.py' % className)
2070 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
2071 base_py = os.path.join(home, '.%s.py' % baseName)
2072 dir = {'self': self}
Georg Brandl14fc4272008-05-17 18:39:55 +00002073 exec('from tkinter import *', dir)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002074 if os.path.isfile(class_tcl):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002075 self.tk.call('source', class_tcl)
2076 if os.path.isfile(class_py):
Neal Norwitz01688022007-08-12 00:43:29 +00002077 exec(open(class_py).read(), dir)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002078 if os.path.isfile(base_tcl):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002079 self.tk.call('source', base_tcl)
2080 if os.path.isfile(base_py):
Neal Norwitz01688022007-08-12 00:43:29 +00002081 exec(open(base_py).read(), dir)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002082 def report_callback_exception(self, exc, val, tb):
Serhiy Storchaka95024872014-09-14 21:17:32 +03002083 """Report callback exception on sys.stderr.
2084
2085 Applications may want to override this internal function, and
2086 should when sys.stderr is None."""
Florent Xicluna54540ec2011-11-04 08:29:17 +01002087 import traceback
Serhiy Storchaka95024872014-09-14 21:17:32 +03002088 print("Exception in Tkinter callback", file=sys.stderr)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002089 sys.last_type = exc
2090 sys.last_value = val
2091 sys.last_traceback = tb
2092 traceback.print_exception(exc, val, tb)
David Aschere2b4b322004-02-18 05:59:53 +00002093 def __getattr__(self, attr):
2094 "Delegate attribute access to the interpreter object"
2095 return getattr(self.tk, attr)
Guido van Rossum18468821994-06-20 07:49:28 +00002096
Guido van Rossum368e06b1997-11-07 20:38:49 +00002097# Ideally, the classes Pack, Place and Grid disappear, the
2098# pack/place/grid methods are defined on the Widget class, and
2099# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
2100# ...), with pack(), place() and grid() being short for
2101# pack_configure(), place_configure() and grid_columnconfigure(), and
2102# forget() being short for pack_forget(). As a practical matter, I'm
2103# afraid that there is too much code out there that may be using the
2104# Pack, Place or Grid class, so I leave them intact -- but only as
2105# backwards compatibility features. Also note that those methods that
2106# take a master as argument (e.g. pack_propagate) have been moved to
2107# the Misc class (which now incorporates all methods common between
2108# toplevel and interior widgets). Again, for compatibility, these are
2109# copied into the Pack, Place or Grid class.
2110
David Aschere2b4b322004-02-18 05:59:53 +00002111
2112def Tcl(screenName=None, baseName=None, className='Tk', useTk=0):
2113 return Tk(screenName, baseName, className, useTk)
2114
Guido van Rossum18468821994-06-20 07:49:28 +00002115class Pack:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002116 """Geometry manager Pack.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002117
Fredrik Lundh06d28152000-08-09 18:03:12 +00002118 Base class to use the methods pack_* in every widget."""
2119 def pack_configure(self, cnf={}, **kw):
2120 """Pack a widget in the parent widget. Use as options:
2121 after=widget - pack it after you have packed widget
2122 anchor=NSEW (or subset) - position widget according to
2123 given direction
Georg Brandlbf1eb632008-05-29 07:19:00 +00002124 before=widget - pack it before you will pack widget
Martin v. Löwisbfe175c2003-04-16 19:42:51 +00002125 expand=bool - expand widget if parent size grows
Fredrik Lundh06d28152000-08-09 18:03:12 +00002126 fill=NONE or X or Y or BOTH - fill widget if widget grows
2127 in=master - use master to contain this widget
Georg Brandlbf1eb632008-05-29 07:19:00 +00002128 in_=master - see 'in' option description
Fredrik Lundh06d28152000-08-09 18:03:12 +00002129 ipadx=amount - add internal padding in x direction
2130 ipady=amount - add internal padding in y direction
2131 padx=amount - add padding in x direction
2132 pady=amount - add padding in y direction
2133 side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget.
2134 """
2135 self.tk.call(
2136 ('pack', 'configure', self._w)
2137 + self._options(cnf, kw))
2138 pack = configure = config = pack_configure
2139 def pack_forget(self):
2140 """Unmap this widget and do not use it for the packing order."""
2141 self.tk.call('pack', 'forget', self._w)
2142 forget = pack_forget
2143 def pack_info(self):
2144 """Return information about the packing options
2145 for this widget."""
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +03002146 d = _splitdict(self.tk, self.tk.call('pack', 'info', self._w))
2147 if 'in' in d:
2148 d['in'] = self.nametowidget(d['in'])
2149 return d
Fredrik Lundh06d28152000-08-09 18:03:12 +00002150 info = pack_info
2151 propagate = pack_propagate = Misc.pack_propagate
2152 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00002153
2154class Place:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002155 """Geometry manager Place.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002156
Fredrik Lundh06d28152000-08-09 18:03:12 +00002157 Base class to use the methods place_* in every widget."""
2158 def place_configure(self, cnf={}, **kw):
2159 """Place a widget in the parent widget. Use as options:
Georg Brandlbf1eb632008-05-29 07:19:00 +00002160 in=master - master relative to which the widget is placed
2161 in_=master - see 'in' option description
Fredrik Lundh06d28152000-08-09 18:03:12 +00002162 x=amount - locate anchor of this widget at position x of master
2163 y=amount - locate anchor of this widget at position y of master
2164 relx=amount - locate anchor of this widget between 0.0 and 1.0
2165 relative to width of master (1.0 is right edge)
Georg Brandlbf1eb632008-05-29 07:19:00 +00002166 rely=amount - locate anchor of this widget between 0.0 and 1.0
Fredrik Lundh06d28152000-08-09 18:03:12 +00002167 relative to height of master (1.0 is bottom edge)
Georg Brandlbf1eb632008-05-29 07:19:00 +00002168 anchor=NSEW (or subset) - position anchor according to given direction
Fredrik Lundh06d28152000-08-09 18:03:12 +00002169 width=amount - width of this widget in pixel
2170 height=amount - height of this widget in pixel
2171 relwidth=amount - width of this widget between 0.0 and 1.0
2172 relative to width of master (1.0 is the same width
Georg Brandlbf1eb632008-05-29 07:19:00 +00002173 as the master)
2174 relheight=amount - height of this widget between 0.0 and 1.0
Fredrik Lundh06d28152000-08-09 18:03:12 +00002175 relative to height of master (1.0 is the same
Georg Brandlbf1eb632008-05-29 07:19:00 +00002176 height as the master)
2177 bordermode="inside" or "outside" - whether to take border width of
2178 master widget into account
2179 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002180 self.tk.call(
2181 ('place', 'configure', self._w)
2182 + self._options(cnf, kw))
2183 place = configure = config = place_configure
2184 def place_forget(self):
2185 """Unmap this widget."""
2186 self.tk.call('place', 'forget', self._w)
2187 forget = place_forget
2188 def place_info(self):
2189 """Return information about the placing options
2190 for this widget."""
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +03002191 d = _splitdict(self.tk, self.tk.call('place', 'info', self._w))
2192 if 'in' in d:
2193 d['in'] = self.nametowidget(d['in'])
2194 return d
Fredrik Lundh06d28152000-08-09 18:03:12 +00002195 info = place_info
2196 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00002197
Guido van Rossum37dcab11996-05-16 16:00:19 +00002198class Grid:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002199 """Geometry manager Grid.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002200
Fredrik Lundh06d28152000-08-09 18:03:12 +00002201 Base class to use the methods grid_* in every widget."""
2202 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
2203 def grid_configure(self, cnf={}, **kw):
2204 """Position a widget in the parent widget in a grid. Use as options:
2205 column=number - use cell identified with given column (starting with 0)
2206 columnspan=number - this widget will span several columns
2207 in=master - use master to contain this widget
Georg Brandlbf1eb632008-05-29 07:19:00 +00002208 in_=master - see 'in' option description
Fredrik Lundh06d28152000-08-09 18:03:12 +00002209 ipadx=amount - add internal padding in x direction
2210 ipady=amount - add internal padding in y direction
2211 padx=amount - add padding in x direction
2212 pady=amount - add padding in y direction
2213 row=number - use cell identified with given row (starting with 0)
2214 rowspan=number - this widget will span several rows
2215 sticky=NSEW - if cell is larger on which sides will this
2216 widget stick to the cell boundary
2217 """
2218 self.tk.call(
2219 ('grid', 'configure', self._w)
2220 + self._options(cnf, kw))
2221 grid = configure = config = grid_configure
2222 bbox = grid_bbox = Misc.grid_bbox
2223 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
2224 def grid_forget(self):
2225 """Unmap this widget."""
2226 self.tk.call('grid', 'forget', self._w)
2227 forget = grid_forget
2228 def grid_remove(self):
2229 """Unmap this widget but remember the grid options."""
2230 self.tk.call('grid', 'remove', self._w)
2231 def grid_info(self):
2232 """Return information about the options
2233 for positioning this widget in a grid."""
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +03002234 d = _splitdict(self.tk, self.tk.call('grid', 'info', self._w))
2235 if 'in' in d:
2236 d['in'] = self.nametowidget(d['in'])
2237 return d
Fredrik Lundh06d28152000-08-09 18:03:12 +00002238 info = grid_info
Martin v. Löwisdc579092001-10-13 09:33:51 +00002239 location = grid_location = Misc.grid_location
Fredrik Lundh06d28152000-08-09 18:03:12 +00002240 propagate = grid_propagate = Misc.grid_propagate
2241 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
2242 size = grid_size = Misc.grid_size
2243 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00002244
Guido van Rossum368e06b1997-11-07 20:38:49 +00002245class BaseWidget(Misc):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002246 """Internal class."""
2247 def _setup(self, master, cnf):
2248 """Internal function. Sets up information about children."""
2249 if _support_default_root:
2250 global _default_root
2251 if not master:
2252 if not _default_root:
2253 _default_root = Tk()
2254 master = _default_root
2255 self.master = master
2256 self.tk = master.tk
2257 name = None
Guido van Rossume014a132006-08-19 16:53:45 +00002258 if 'name' in cnf:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002259 name = cnf['name']
2260 del cnf['name']
2261 if not name:
Serhiy Storchaka8acb5682016-06-13 09:24:11 +03002262 name = self.__class__.__name__.lower()
2263 if master._last_child_ids is None:
2264 master._last_child_ids = {}
2265 count = master._last_child_ids.get(name, 0) + 1
2266 master._last_child_ids[name] = count
2267 if count == 1:
Serhiy Storchakab1f52872016-10-25 09:43:48 +03002268 name = '!%s' % (name,)
Serhiy Storchaka8acb5682016-06-13 09:24:11 +03002269 else:
Serhiy Storchakab1f52872016-10-25 09:43:48 +03002270 name = '!%s%d' % (name, count)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002271 self._name = name
2272 if master._w=='.':
2273 self._w = '.' + name
2274 else:
2275 self._w = master._w + '.' + name
2276 self.children = {}
Guido van Rossume014a132006-08-19 16:53:45 +00002277 if self._name in self.master.children:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002278 self.master.children[self._name].destroy()
2279 self.master.children[self._name] = self
2280 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
2281 """Construct a widget with the parent widget MASTER, a name WIDGETNAME
2282 and appropriate options."""
2283 if kw:
2284 cnf = _cnfmerge((cnf, kw))
2285 self.widgetName = widgetName
2286 BaseWidget._setup(self, master, cnf)
Hirokazu Yamamotoa18424c2008-11-04 06:26:27 +00002287 if self._tclCommands is None:
2288 self._tclCommands = []
Guilherme Polob212b752008-09-04 11:21:31 +00002289 classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)]
2290 for k, v in classes:
2291 del cnf[k]
Fredrik Lundh06d28152000-08-09 18:03:12 +00002292 self.tk.call(
2293 (widgetName, self._w) + extra + self._options(cnf))
2294 for k, v in classes:
2295 k.configure(self, v)
2296 def destroy(self):
2297 """Destroy this and all descendants widgets."""
Guido van Rossum992d4a32007-07-11 13:09:30 +00002298 for c in list(self.children.values()): c.destroy()
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002299 self.tk.call('destroy', self._w)
Guido van Rossume014a132006-08-19 16:53:45 +00002300 if self._name in self.master.children:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002301 del self.master.children[self._name]
Fredrik Lundh06d28152000-08-09 18:03:12 +00002302 Misc.destroy(self)
2303 def _do(self, name, args=()):
2304 # XXX Obsolete -- better use self.tk.call directly!
2305 return self.tk.call((self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00002306
Guido van Rossum368e06b1997-11-07 20:38:49 +00002307class Widget(BaseWidget, Pack, Place, Grid):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002308 """Internal class.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002309
Fredrik Lundh06d28152000-08-09 18:03:12 +00002310 Base class for a widget which can be positioned with the geometry managers
2311 Pack, Place or Grid."""
2312 pass
Guido van Rossum368e06b1997-11-07 20:38:49 +00002313
2314class Toplevel(BaseWidget, Wm):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002315 """Toplevel widget, e.g. for dialogs."""
2316 def __init__(self, master=None, cnf={}, **kw):
2317 """Construct a toplevel widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002318
Fredrik Lundh06d28152000-08-09 18:03:12 +00002319 Valid resource names: background, bd, bg, borderwidth, class,
2320 colormap, container, cursor, height, highlightbackground,
2321 highlightcolor, highlightthickness, menu, relief, screen, takefocus,
2322 use, visual, width."""
2323 if kw:
2324 cnf = _cnfmerge((cnf, kw))
2325 extra = ()
2326 for wmkey in ['screen', 'class_', 'class', 'visual',
2327 'colormap']:
Guido van Rossume014a132006-08-19 16:53:45 +00002328 if wmkey in cnf:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002329 val = cnf[wmkey]
2330 # TBD: a hack needed because some keys
2331 # are not valid as keyword arguments
2332 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
2333 else: opt = '-'+wmkey
2334 extra = extra + (opt, val)
2335 del cnf[wmkey]
2336 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
2337 root = self._root()
2338 self.iconname(root.iconname())
2339 self.title(root.title())
2340 self.protocol("WM_DELETE_WINDOW", self.destroy)
Guido van Rossum18468821994-06-20 07:49:28 +00002341
2342class Button(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002343 """Button widget."""
2344 def __init__(self, master=None, cnf={}, **kw):
2345 """Construct a button widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00002346
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002347 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002348
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002349 activebackground, activeforeground, anchor,
2350 background, bitmap, borderwidth, cursor,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002351 disabledforeground, font, foreground
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002352 highlightbackground, highlightcolor,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002353 highlightthickness, image, justify,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002354 padx, pady, relief, repeatdelay,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002355 repeatinterval, takefocus, text,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002356 textvariable, underline, wraplength
Raymond Hettingerff41c482003-04-06 09:01:11 +00002357
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002358 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002359
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002360 command, compound, default, height,
2361 overrelief, state, width
2362 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002363 Widget.__init__(self, master, 'button', cnf, kw)
Raymond Hettingerff41c482003-04-06 09:01:11 +00002364
Fredrik Lundh06d28152000-08-09 18:03:12 +00002365 def flash(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00002366 """Flash the button.
2367
2368 This is accomplished by redisplaying
2369 the button several times, alternating between active and
2370 normal colors. At the end of the flash the button is left
2371 in the same normal/active state as when the command was
2372 invoked. This command is ignored if the button's state is
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002373 disabled.
2374 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002375 self.tk.call(self._w, 'flash')
Raymond Hettingerff41c482003-04-06 09:01:11 +00002376
Fredrik Lundh06d28152000-08-09 18:03:12 +00002377 def invoke(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00002378 """Invoke the command associated with the button.
2379
2380 The return value is the return value from the command,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002381 or an empty string if there is no command associated with
2382 the button. This command is ignored if the button's state
2383 is disabled.
2384 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002385 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00002386
Guilherme Polo1fff0082009-08-14 15:05:30 +00002387class Canvas(Widget, XView, YView):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002388 """Canvas widget to display graphical elements like lines or text."""
2389 def __init__(self, master=None, cnf={}, **kw):
2390 """Construct a canvas widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002391
Fredrik Lundh06d28152000-08-09 18:03:12 +00002392 Valid resource names: background, bd, bg, borderwidth, closeenough,
2393 confine, cursor, height, highlightbackground, highlightcolor,
2394 highlightthickness, insertbackground, insertborderwidth,
2395 insertofftime, insertontime, insertwidth, offset, relief,
2396 scrollregion, selectbackground, selectborderwidth, selectforeground,
2397 state, takefocus, width, xscrollcommand, xscrollincrement,
2398 yscrollcommand, yscrollincrement."""
2399 Widget.__init__(self, master, 'canvas', cnf, kw)
2400 def addtag(self, *args):
2401 """Internal function."""
2402 self.tk.call((self._w, 'addtag') + args)
2403 def addtag_above(self, newtag, tagOrId):
2404 """Add tag NEWTAG to all items above TAGORID."""
2405 self.addtag(newtag, 'above', tagOrId)
2406 def addtag_all(self, newtag):
2407 """Add tag NEWTAG to all items."""
2408 self.addtag(newtag, 'all')
2409 def addtag_below(self, newtag, tagOrId):
2410 """Add tag NEWTAG to all items below TAGORID."""
2411 self.addtag(newtag, 'below', tagOrId)
2412 def addtag_closest(self, newtag, x, y, halo=None, start=None):
2413 """Add tag NEWTAG to item which is closest to pixel at X, Y.
2414 If several match take the top-most.
2415 All items closer than HALO are considered overlapping (all are
2416 closests). If START is specified the next below this tag is taken."""
2417 self.addtag(newtag, 'closest', x, y, halo, start)
2418 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
2419 """Add tag NEWTAG to all items in the rectangle defined
2420 by X1,Y1,X2,Y2."""
2421 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
2422 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
2423 """Add tag NEWTAG to all items which overlap the rectangle
2424 defined by X1,Y1,X2,Y2."""
2425 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
2426 def addtag_withtag(self, newtag, tagOrId):
2427 """Add tag NEWTAG to all items with TAGORID."""
2428 self.addtag(newtag, 'withtag', tagOrId)
2429 def bbox(self, *args):
2430 """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
2431 which encloses all items with tags specified as arguments."""
2432 return self._getints(
2433 self.tk.call((self._w, 'bbox') + args)) or None
2434 def tag_unbind(self, tagOrId, sequence, funcid=None):
2435 """Unbind for all items with TAGORID for event SEQUENCE the
2436 function identified with FUNCID."""
2437 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
2438 if funcid:
2439 self.deletecommand(funcid)
2440 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
2441 """Bind to all items with TAGORID at event SEQUENCE a call to function FUNC.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002442
Fredrik Lundh06d28152000-08-09 18:03:12 +00002443 An additional boolean parameter ADD specifies whether FUNC will be
2444 called additionally to the other bound function or whether it will
2445 replace the previous function. See bind for the return value."""
2446 return self._bind((self._w, 'bind', tagOrId),
2447 sequence, func, add)
2448 def canvasx(self, screenx, gridspacing=None):
2449 """Return the canvas x coordinate of pixel position SCREENX rounded
2450 to nearest multiple of GRIDSPACING units."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002451 return self.tk.getdouble(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002452 self._w, 'canvasx', screenx, gridspacing))
2453 def canvasy(self, screeny, gridspacing=None):
2454 """Return the canvas y coordinate of pixel position SCREENY rounded
2455 to nearest multiple of GRIDSPACING units."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002456 return self.tk.getdouble(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002457 self._w, 'canvasy', screeny, gridspacing))
2458 def coords(self, *args):
2459 """Return a list of coordinates for the item given in ARGS."""
2460 # XXX Should use _flatten on args
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002461 return [self.tk.getdouble(x) for x in
Guido van Rossum0bd54331998-05-19 21:18:13 +00002462 self.tk.splitlist(
Alexander Belopolsky022f0492010-11-22 19:40:51 +00002463 self.tk.call((self._w, 'coords') + args))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00002464 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
2465 """Internal function."""
2466 args = _flatten(args)
2467 cnf = args[-1]
Guido van Rossum13257902007-06-07 23:15:56 +00002468 if isinstance(cnf, (dict, tuple)):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002469 args = args[:-1]
2470 else:
2471 cnf = {}
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002472 return self.tk.getint(self.tk.call(
Raymond Hettingerff41c482003-04-06 09:01:11 +00002473 self._w, 'create', itemType,
2474 *(args + self._options(cnf, kw))))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002475 def create_arc(self, *args, **kw):
2476 """Create arc shaped region with coordinates x1,y1,x2,y2."""
2477 return self._create('arc', args, kw)
2478 def create_bitmap(self, *args, **kw):
2479 """Create bitmap with coordinates x1,y1."""
2480 return self._create('bitmap', args, kw)
2481 def create_image(self, *args, **kw):
2482 """Create image item with coordinates x1,y1."""
2483 return self._create('image', args, kw)
2484 def create_line(self, *args, **kw):
2485 """Create line with coordinates x1,y1,...,xn,yn."""
2486 return self._create('line', args, kw)
2487 def create_oval(self, *args, **kw):
2488 """Create oval with coordinates x1,y1,x2,y2."""
2489 return self._create('oval', args, kw)
2490 def create_polygon(self, *args, **kw):
2491 """Create polygon with coordinates x1,y1,...,xn,yn."""
2492 return self._create('polygon', args, kw)
2493 def create_rectangle(self, *args, **kw):
2494 """Create rectangle with coordinates x1,y1,x2,y2."""
2495 return self._create('rectangle', args, kw)
2496 def create_text(self, *args, **kw):
2497 """Create text with coordinates x1,y1."""
2498 return self._create('text', args, kw)
2499 def create_window(self, *args, **kw):
2500 """Create window with coordinates x1,y1,x2,y2."""
2501 return self._create('window', args, kw)
2502 def dchars(self, *args):
2503 """Delete characters of text items identified by tag or id in ARGS (possibly
2504 several times) from FIRST to LAST character (including)."""
2505 self.tk.call((self._w, 'dchars') + args)
2506 def delete(self, *args):
2507 """Delete items identified by all tag or ids contained in ARGS."""
2508 self.tk.call((self._w, 'delete') + args)
2509 def dtag(self, *args):
2510 """Delete tag or id given as last arguments in ARGS from items
2511 identified by first argument in ARGS."""
2512 self.tk.call((self._w, 'dtag') + args)
2513 def find(self, *args):
2514 """Internal function."""
2515 return self._getints(
2516 self.tk.call((self._w, 'find') + args)) or ()
2517 def find_above(self, tagOrId):
2518 """Return items above TAGORID."""
2519 return self.find('above', tagOrId)
2520 def find_all(self):
2521 """Return all items."""
2522 return self.find('all')
2523 def find_below(self, tagOrId):
2524 """Return all items below TAGORID."""
2525 return self.find('below', tagOrId)
2526 def find_closest(self, x, y, halo=None, start=None):
2527 """Return item which is closest to pixel at X, Y.
2528 If several match take the top-most.
2529 All items closer than HALO are considered overlapping (all are
2530 closests). If START is specified the next below this tag is taken."""
2531 return self.find('closest', x, y, halo, start)
2532 def find_enclosed(self, x1, y1, x2, y2):
2533 """Return all items in rectangle defined
2534 by X1,Y1,X2,Y2."""
2535 return self.find('enclosed', x1, y1, x2, y2)
2536 def find_overlapping(self, x1, y1, x2, y2):
2537 """Return all items which overlap the rectangle
2538 defined by X1,Y1,X2,Y2."""
2539 return self.find('overlapping', x1, y1, x2, y2)
2540 def find_withtag(self, tagOrId):
2541 """Return all items with TAGORID."""
2542 return self.find('withtag', tagOrId)
2543 def focus(self, *args):
2544 """Set focus to the first item specified in ARGS."""
2545 return self.tk.call((self._w, 'focus') + args)
2546 def gettags(self, *args):
2547 """Return tags associated with the first item specified in ARGS."""
2548 return self.tk.splitlist(
2549 self.tk.call((self._w, 'gettags') + args))
2550 def icursor(self, *args):
2551 """Set cursor at position POS in the item identified by TAGORID.
2552 In ARGS TAGORID must be first."""
2553 self.tk.call((self._w, 'icursor') + args)
2554 def index(self, *args):
2555 """Return position of cursor as integer in item specified in ARGS."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002556 return self.tk.getint(self.tk.call((self._w, 'index') + args))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002557 def insert(self, *args):
2558 """Insert TEXT in item TAGORID at position POS. ARGS must
2559 be TAGORID POS TEXT."""
2560 self.tk.call((self._w, 'insert') + args)
2561 def itemcget(self, tagOrId, option):
2562 """Return the resource value for an OPTION for item TAGORID."""
2563 return self.tk.call(
2564 (self._w, 'itemcget') + (tagOrId, '-'+option))
2565 def itemconfigure(self, tagOrId, cnf=None, **kw):
2566 """Configure resources of an item TAGORID.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002567
Fredrik Lundh06d28152000-08-09 18:03:12 +00002568 The values for resources are specified as keyword
2569 arguments. To get an overview about
2570 the allowed keyword arguments call the method without arguments.
2571 """
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002572 return self._configure(('itemconfigure', tagOrId), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002573 itemconfig = itemconfigure
2574 # lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
2575 # so the preferred name for them is tag_lower, tag_raise
2576 # (similar to tag_bind, and similar to the Text widget);
2577 # unfortunately can't delete the old ones yet (maybe in 1.6)
2578 def tag_lower(self, *args):
2579 """Lower an item TAGORID given in ARGS
2580 (optional below another item)."""
2581 self.tk.call((self._w, 'lower') + args)
2582 lower = tag_lower
2583 def move(self, *args):
2584 """Move an item TAGORID given in ARGS."""
2585 self.tk.call((self._w, 'move') + args)
2586 def postscript(self, cnf={}, **kw):
2587 """Print the contents of the canvas to a postscript
2588 file. Valid options: colormap, colormode, file, fontmap,
2589 height, pageanchor, pageheight, pagewidth, pagex, pagey,
2590 rotate, witdh, x, y."""
2591 return self.tk.call((self._w, 'postscript') +
2592 self._options(cnf, kw))
2593 def tag_raise(self, *args):
2594 """Raise an item TAGORID given in ARGS
2595 (optional above another item)."""
2596 self.tk.call((self._w, 'raise') + args)
2597 lift = tkraise = tag_raise
2598 def scale(self, *args):
2599 """Scale item TAGORID with XORIGIN, YORIGIN, XSCALE, YSCALE."""
2600 self.tk.call((self._w, 'scale') + args)
2601 def scan_mark(self, x, y):
2602 """Remember the current X, Y coordinates."""
2603 self.tk.call(self._w, 'scan', 'mark', x, y)
Neal Norwitze931ed52003-01-10 23:24:32 +00002604 def scan_dragto(self, x, y, gain=10):
2605 """Adjust the view of the canvas to GAIN times the
Fredrik Lundh06d28152000-08-09 18:03:12 +00002606 difference between X and Y and the coordinates given in
2607 scan_mark."""
Neal Norwitze931ed52003-01-10 23:24:32 +00002608 self.tk.call(self._w, 'scan', 'dragto', x, y, gain)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002609 def select_adjust(self, tagOrId, index):
2610 """Adjust the end of the selection near the cursor of an item TAGORID to index."""
2611 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
2612 def select_clear(self):
2613 """Clear the selection if it is in this widget."""
2614 self.tk.call(self._w, 'select', 'clear')
2615 def select_from(self, tagOrId, index):
2616 """Set the fixed end of a selection in item TAGORID to INDEX."""
2617 self.tk.call(self._w, 'select', 'from', tagOrId, index)
2618 def select_item(self):
2619 """Return the item which has the selection."""
Neal Norwitz58b63bf2002-07-23 02:52:58 +00002620 return self.tk.call(self._w, 'select', 'item') or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00002621 def select_to(self, tagOrId, index):
2622 """Set the variable end of a selection in item TAGORID to INDEX."""
2623 self.tk.call(self._w, 'select', 'to', tagOrId, index)
2624 def type(self, tagOrId):
2625 """Return the type of the item TAGORID."""
2626 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +00002627
2628class Checkbutton(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002629 """Checkbutton widget which is either in on- or off-state."""
2630 def __init__(self, master=None, cnf={}, **kw):
2631 """Construct a checkbutton widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002632
Fredrik Lundh06d28152000-08-09 18:03:12 +00002633 Valid resource names: activebackground, activeforeground, anchor,
2634 background, bd, bg, bitmap, borderwidth, command, cursor,
2635 disabledforeground, fg, font, foreground, height,
2636 highlightbackground, highlightcolor, highlightthickness, image,
2637 indicatoron, justify, offvalue, onvalue, padx, pady, relief,
2638 selectcolor, selectimage, state, takefocus, text, textvariable,
2639 underline, variable, width, wraplength."""
2640 Widget.__init__(self, master, 'checkbutton', cnf, kw)
2641 def deselect(self):
2642 """Put the button in off-state."""
2643 self.tk.call(self._w, 'deselect')
2644 def flash(self):
2645 """Flash the button."""
2646 self.tk.call(self._w, 'flash')
2647 def invoke(self):
2648 """Toggle the button and invoke a command if given as resource."""
2649 return self.tk.call(self._w, 'invoke')
2650 def select(self):
2651 """Put the button in on-state."""
2652 self.tk.call(self._w, 'select')
2653 def toggle(self):
2654 """Toggle the button."""
2655 self.tk.call(self._w, 'toggle')
Guido van Rossum18468821994-06-20 07:49:28 +00002656
Guilherme Polo1fff0082009-08-14 15:05:30 +00002657class Entry(Widget, XView):
Martin Panterc04fb562016-02-10 05:44:01 +00002658 """Entry widget which allows displaying simple text."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002659 def __init__(self, master=None, cnf={}, **kw):
2660 """Construct an entry widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002661
Fredrik Lundh06d28152000-08-09 18:03:12 +00002662 Valid resource names: background, bd, bg, borderwidth, cursor,
2663 exportselection, fg, font, foreground, highlightbackground,
2664 highlightcolor, highlightthickness, insertbackground,
2665 insertborderwidth, insertofftime, insertontime, insertwidth,
2666 invalidcommand, invcmd, justify, relief, selectbackground,
2667 selectborderwidth, selectforeground, show, state, takefocus,
2668 textvariable, validate, validatecommand, vcmd, width,
2669 xscrollcommand."""
2670 Widget.__init__(self, master, 'entry', cnf, kw)
2671 def delete(self, first, last=None):
2672 """Delete text from FIRST to LAST (not included)."""
2673 self.tk.call(self._w, 'delete', first, last)
2674 def get(self):
2675 """Return the text."""
2676 return self.tk.call(self._w, 'get')
2677 def icursor(self, index):
2678 """Insert cursor at INDEX."""
2679 self.tk.call(self._w, 'icursor', index)
2680 def index(self, index):
2681 """Return position of cursor."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002682 return self.tk.getint(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002683 self._w, 'index', index))
2684 def insert(self, index, string):
2685 """Insert STRING at INDEX."""
2686 self.tk.call(self._w, 'insert', index, string)
2687 def scan_mark(self, x):
2688 """Remember the current X, Y coordinates."""
2689 self.tk.call(self._w, 'scan', 'mark', x)
2690 def scan_dragto(self, x):
2691 """Adjust the view of the canvas to 10 times the
2692 difference between X and Y and the coordinates given in
2693 scan_mark."""
2694 self.tk.call(self._w, 'scan', 'dragto', x)
2695 def selection_adjust(self, index):
2696 """Adjust the end of the selection near the cursor to INDEX."""
2697 self.tk.call(self._w, 'selection', 'adjust', index)
2698 select_adjust = selection_adjust
2699 def selection_clear(self):
2700 """Clear the selection if it is in this widget."""
2701 self.tk.call(self._w, 'selection', 'clear')
2702 select_clear = selection_clear
2703 def selection_from(self, index):
2704 """Set the fixed end of a selection to INDEX."""
2705 self.tk.call(self._w, 'selection', 'from', index)
2706 select_from = selection_from
2707 def selection_present(self):
Guilherme Polo1fff0082009-08-14 15:05:30 +00002708 """Return True if there are characters selected in the entry, False
2709 otherwise."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002710 return self.tk.getboolean(
2711 self.tk.call(self._w, 'selection', 'present'))
2712 select_present = selection_present
2713 def selection_range(self, start, end):
2714 """Set the selection from START to END (not included)."""
2715 self.tk.call(self._w, 'selection', 'range', start, end)
2716 select_range = selection_range
2717 def selection_to(self, index):
2718 """Set the variable end of a selection to INDEX."""
2719 self.tk.call(self._w, 'selection', 'to', index)
2720 select_to = selection_to
Guido van Rossum18468821994-06-20 07:49:28 +00002721
2722class Frame(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002723 """Frame widget which may contain other widgets and can have a 3D border."""
2724 def __init__(self, master=None, cnf={}, **kw):
2725 """Construct a frame widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002726
Fredrik Lundh06d28152000-08-09 18:03:12 +00002727 Valid resource names: background, bd, bg, borderwidth, class,
2728 colormap, container, cursor, height, highlightbackground,
2729 highlightcolor, highlightthickness, relief, takefocus, visual, width."""
2730 cnf = _cnfmerge((cnf, kw))
2731 extra = ()
Guido van Rossume014a132006-08-19 16:53:45 +00002732 if 'class_' in cnf:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002733 extra = ('-class', cnf['class_'])
2734 del cnf['class_']
Guido van Rossume014a132006-08-19 16:53:45 +00002735 elif 'class' in cnf:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002736 extra = ('-class', cnf['class'])
2737 del cnf['class']
2738 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00002739
2740class Label(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002741 """Label widget which can display text and bitmaps."""
2742 def __init__(self, master=None, cnf={}, **kw):
2743 """Construct a label widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00002744
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002745 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002746
2747 activebackground, activeforeground, anchor,
2748 background, bitmap, borderwidth, cursor,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002749 disabledforeground, font, foreground,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002750 highlightbackground, highlightcolor,
2751 highlightthickness, image, justify,
2752 padx, pady, relief, takefocus, text,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002753 textvariable, underline, wraplength
Raymond Hettingerff41c482003-04-06 09:01:11 +00002754
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002755 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002756
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002757 height, state, width
Raymond Hettingerff41c482003-04-06 09:01:11 +00002758
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002759 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002760 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002761
Guilherme Polo1fff0082009-08-14 15:05:30 +00002762class Listbox(Widget, XView, YView):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002763 """Listbox widget which can display a list of strings."""
2764 def __init__(self, master=None, cnf={}, **kw):
2765 """Construct a listbox widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002766
Fredrik Lundh06d28152000-08-09 18:03:12 +00002767 Valid resource names: background, bd, bg, borderwidth, cursor,
2768 exportselection, fg, font, foreground, height, highlightbackground,
2769 highlightcolor, highlightthickness, relief, selectbackground,
2770 selectborderwidth, selectforeground, selectmode, setgrid, takefocus,
2771 width, xscrollcommand, yscrollcommand, listvariable."""
2772 Widget.__init__(self, master, 'listbox', cnf, kw)
2773 def activate(self, index):
2774 """Activate item identified by INDEX."""
2775 self.tk.call(self._w, 'activate', index)
Serhiy Storchakafc14ad92014-06-02 21:31:07 +03002776 def bbox(self, index):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002777 """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
Serhiy Storchakafc14ad92014-06-02 21:31:07 +03002778 which encloses the item identified by the given index."""
2779 return self._getints(self.tk.call(self._w, 'bbox', index)) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00002780 def curselection(self):
Serhiy Storchakafc14ad92014-06-02 21:31:07 +03002781 """Return the indices of currently selected item."""
2782 return self._getints(self.tk.call(self._w, 'curselection')) or ()
Fredrik Lundh06d28152000-08-09 18:03:12 +00002783 def delete(self, first, last=None):
Serhiy Storchakaca3cd002014-06-02 16:51:44 +03002784 """Delete items from FIRST to LAST (included)."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002785 self.tk.call(self._w, 'delete', first, last)
2786 def get(self, first, last=None):
Serhiy Storchakaca3cd002014-06-02 16:51:44 +03002787 """Get list of items from FIRST to LAST (included)."""
Serhiy Storchakafc14ad92014-06-02 21:31:07 +03002788 if last is not None:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002789 return self.tk.splitlist(self.tk.call(
2790 self._w, 'get', first, last))
2791 else:
2792 return self.tk.call(self._w, 'get', first)
2793 def index(self, index):
2794 """Return index of item identified with INDEX."""
2795 i = self.tk.call(self._w, 'index', index)
2796 if i == 'none': return None
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002797 return self.tk.getint(i)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002798 def insert(self, index, *elements):
2799 """Insert ELEMENTS at INDEX."""
2800 self.tk.call((self._w, 'insert', index) + elements)
2801 def nearest(self, y):
2802 """Get index of item which is nearest to y coordinate Y."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002803 return self.tk.getint(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002804 self._w, 'nearest', y))
2805 def scan_mark(self, x, y):
2806 """Remember the current X, Y coordinates."""
2807 self.tk.call(self._w, 'scan', 'mark', x, y)
2808 def scan_dragto(self, x, y):
2809 """Adjust the view of the listbox to 10 times the
2810 difference between X and Y and the coordinates given in
2811 scan_mark."""
2812 self.tk.call(self._w, 'scan', 'dragto', x, y)
2813 def see(self, index):
2814 """Scroll such that INDEX is visible."""
2815 self.tk.call(self._w, 'see', index)
2816 def selection_anchor(self, index):
2817 """Set the fixed end oft the selection to INDEX."""
2818 self.tk.call(self._w, 'selection', 'anchor', index)
2819 select_anchor = selection_anchor
2820 def selection_clear(self, first, last=None):
Serhiy Storchakaca3cd002014-06-02 16:51:44 +03002821 """Clear the selection from FIRST to LAST (included)."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002822 self.tk.call(self._w,
2823 'selection', 'clear', first, last)
2824 select_clear = selection_clear
2825 def selection_includes(self, index):
2826 """Return 1 if INDEX is part of the selection."""
2827 return self.tk.getboolean(self.tk.call(
2828 self._w, 'selection', 'includes', index))
2829 select_includes = selection_includes
2830 def selection_set(self, first, last=None):
Serhiy Storchakaca3cd002014-06-02 16:51:44 +03002831 """Set the selection from FIRST to LAST (included) without
Fredrik Lundh06d28152000-08-09 18:03:12 +00002832 changing the currently selected elements."""
2833 self.tk.call(self._w, 'selection', 'set', first, last)
2834 select_set = selection_set
2835 def size(self):
2836 """Return the number of elements in the listbox."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002837 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum09f1ad82001-09-05 19:29:56 +00002838 def itemcget(self, index, option):
2839 """Return the resource value for an ITEM and an OPTION."""
2840 return self.tk.call(
2841 (self._w, 'itemcget') + (index, '-'+option))
Guido van Rossuma0adb922001-09-01 18:29:55 +00002842 def itemconfigure(self, index, cnf=None, **kw):
Guido van Rossum09f1ad82001-09-05 19:29:56 +00002843 """Configure resources of an ITEM.
Guido van Rossuma0adb922001-09-01 18:29:55 +00002844
2845 The values for resources are specified as keyword arguments.
2846 To get an overview about the allowed keyword arguments
2847 call the method without arguments.
2848 Valid resource names: background, bg, foreground, fg,
2849 selectbackground, selectforeground."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002850 return self._configure(('itemconfigure', index), cnf, kw)
Guido van Rossuma0adb922001-09-01 18:29:55 +00002851 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00002852
2853class Menu(Widget):
Martin Panterc04fb562016-02-10 05:44:01 +00002854 """Menu widget which allows displaying menu bars, pull-down menus and pop-up menus."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002855 def __init__(self, master=None, cnf={}, **kw):
2856 """Construct menu widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002857
Fredrik Lundh06d28152000-08-09 18:03:12 +00002858 Valid resource names: activebackground, activeborderwidth,
2859 activeforeground, background, bd, bg, borderwidth, cursor,
2860 disabledforeground, fg, font, foreground, postcommand, relief,
2861 selectcolor, takefocus, tearoff, tearoffcommand, title, type."""
2862 Widget.__init__(self, master, 'menu', cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002863 def tk_popup(self, x, y, entry=""):
2864 """Post the menu at position X,Y with entry ENTRY."""
2865 self.tk.call('tk_popup', self._w, x, y, entry)
2866 def activate(self, index):
2867 """Activate entry at INDEX."""
2868 self.tk.call(self._w, 'activate', index)
2869 def add(self, itemType, cnf={}, **kw):
2870 """Internal function."""
2871 self.tk.call((self._w, 'add', itemType) +
2872 self._options(cnf, kw))
2873 def add_cascade(self, cnf={}, **kw):
2874 """Add hierarchical menu item."""
2875 self.add('cascade', cnf or kw)
2876 def add_checkbutton(self, cnf={}, **kw):
2877 """Add checkbutton menu item."""
2878 self.add('checkbutton', cnf or kw)
2879 def add_command(self, cnf={}, **kw):
2880 """Add command menu item."""
2881 self.add('command', cnf or kw)
2882 def add_radiobutton(self, cnf={}, **kw):
2883 """Addd radio menu item."""
2884 self.add('radiobutton', cnf or kw)
2885 def add_separator(self, cnf={}, **kw):
2886 """Add separator."""
2887 self.add('separator', cnf or kw)
2888 def insert(self, index, itemType, cnf={}, **kw):
2889 """Internal function."""
2890 self.tk.call((self._w, 'insert', index, itemType) +
2891 self._options(cnf, kw))
2892 def insert_cascade(self, index, cnf={}, **kw):
2893 """Add hierarchical menu item at INDEX."""
2894 self.insert(index, 'cascade', cnf or kw)
2895 def insert_checkbutton(self, index, cnf={}, **kw):
2896 """Add checkbutton menu item at INDEX."""
2897 self.insert(index, 'checkbutton', cnf or kw)
2898 def insert_command(self, index, cnf={}, **kw):
2899 """Add command menu item at INDEX."""
2900 self.insert(index, 'command', cnf or kw)
2901 def insert_radiobutton(self, index, cnf={}, **kw):
2902 """Addd radio menu item at INDEX."""
2903 self.insert(index, 'radiobutton', cnf or kw)
2904 def insert_separator(self, index, cnf={}, **kw):
2905 """Add separator at INDEX."""
2906 self.insert(index, 'separator', cnf or kw)
2907 def delete(self, index1, index2=None):
Hirokazu Yamamotoa18424c2008-11-04 06:26:27 +00002908 """Delete menu items between INDEX1 and INDEX2 (included)."""
Robert Schuppenies3d1c7de2008-08-10 11:28:17 +00002909 if index2 is None:
2910 index2 = index1
Robert Schuppenies3d1c7de2008-08-10 11:28:17 +00002911
Hirokazu Yamamotoa18424c2008-11-04 06:26:27 +00002912 num_index1, num_index2 = self.index(index1), self.index(index2)
2913 if (num_index1 is None) or (num_index2 is None):
2914 num_index1, num_index2 = 0, -1
2915
2916 for i in range(num_index1, num_index2 + 1):
2917 if 'command' in self.entryconfig(i):
2918 c = str(self.entrycget(i, 'command'))
2919 if c:
2920 self.deletecommand(c)
2921 self.tk.call(self._w, 'delete', index1, index2)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002922 def entrycget(self, index, option):
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002923 """Return the resource value of a menu item for OPTION at INDEX."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002924 return self.tk.call(self._w, 'entrycget', index, '-' + option)
2925 def entryconfigure(self, index, cnf=None, **kw):
2926 """Configure a menu item at INDEX."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002927 return self._configure(('entryconfigure', index), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002928 entryconfig = entryconfigure
2929 def index(self, index):
2930 """Return the index of a menu item identified by INDEX."""
2931 i = self.tk.call(self._w, 'index', index)
2932 if i == 'none': return None
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002933 return self.tk.getint(i)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002934 def invoke(self, index):
2935 """Invoke a menu item identified by INDEX and execute
2936 the associated command."""
2937 return self.tk.call(self._w, 'invoke', index)
2938 def post(self, x, y):
2939 """Display a menu at position X,Y."""
2940 self.tk.call(self._w, 'post', x, y)
2941 def type(self, index):
2942 """Return the type of the menu item at INDEX."""
2943 return self.tk.call(self._w, 'type', index)
2944 def unpost(self):
2945 """Unmap a menu."""
2946 self.tk.call(self._w, 'unpost')
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07002947 def xposition(self, index): # new in Tk 8.5
2948 """Return the x-position of the leftmost pixel of the menu item
2949 at INDEX."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002950 return self.tk.getint(self.tk.call(self._w, 'xposition', index))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002951 def yposition(self, index):
2952 """Return the y-position of the topmost pixel of the menu item at INDEX."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002953 return self.tk.getint(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002954 self._w, 'yposition', index))
Guido van Rossum18468821994-06-20 07:49:28 +00002955
2956class Menubutton(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002957 """Menubutton widget, obsolete since Tk8.0."""
2958 def __init__(self, master=None, cnf={}, **kw):
2959 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002960
2961class Message(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002962 """Message widget to display multiline text. Obsolete since Label does it too."""
2963 def __init__(self, master=None, cnf={}, **kw):
2964 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002965
2966class Radiobutton(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002967 """Radiobutton widget which shows only one of several buttons in on-state."""
2968 def __init__(self, master=None, cnf={}, **kw):
2969 """Construct a radiobutton widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002970
Fredrik Lundh06d28152000-08-09 18:03:12 +00002971 Valid resource names: activebackground, activeforeground, anchor,
2972 background, bd, bg, bitmap, borderwidth, command, cursor,
2973 disabledforeground, fg, font, foreground, height,
2974 highlightbackground, highlightcolor, highlightthickness, image,
2975 indicatoron, justify, padx, pady, relief, selectcolor, selectimage,
2976 state, takefocus, text, textvariable, underline, value, variable,
2977 width, wraplength."""
2978 Widget.__init__(self, master, 'radiobutton', cnf, kw)
2979 def deselect(self):
2980 """Put the button in off-state."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002981
Fredrik Lundh06d28152000-08-09 18:03:12 +00002982 self.tk.call(self._w, 'deselect')
2983 def flash(self):
2984 """Flash the button."""
2985 self.tk.call(self._w, 'flash')
2986 def invoke(self):
2987 """Toggle the button and invoke a command if given as resource."""
2988 return self.tk.call(self._w, 'invoke')
2989 def select(self):
2990 """Put the button in on-state."""
2991 self.tk.call(self._w, 'select')
Guido van Rossum18468821994-06-20 07:49:28 +00002992
2993class Scale(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002994 """Scale widget which can display a numerical scale."""
2995 def __init__(self, master=None, cnf={}, **kw):
2996 """Construct a scale widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002997
Fredrik Lundh06d28152000-08-09 18:03:12 +00002998 Valid resource names: activebackground, background, bigincrement, bd,
2999 bg, borderwidth, command, cursor, digits, fg, font, foreground, from,
3000 highlightbackground, highlightcolor, highlightthickness, label,
3001 length, orient, relief, repeatdelay, repeatinterval, resolution,
3002 showvalue, sliderlength, sliderrelief, state, takefocus,
3003 tickinterval, to, troughcolor, variable, width."""
3004 Widget.__init__(self, master, 'scale', cnf, kw)
3005 def get(self):
3006 """Get the current value as integer or float."""
3007 value = self.tk.call(self._w, 'get')
3008 try:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003009 return self.tk.getint(value)
Serhiy Storchaka32c0d3a2016-10-30 18:49:52 +02003010 except (ValueError, TypeError, TclError):
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003011 return self.tk.getdouble(value)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003012 def set(self, value):
3013 """Set the value to VALUE."""
3014 self.tk.call(self._w, 'set', value)
3015 def coords(self, value=None):
3016 """Return a tuple (X,Y) of the point along the centerline of the
3017 trough that corresponds to VALUE or the current value if None is
3018 given."""
3019
3020 return self._getints(self.tk.call(self._w, 'coords', value))
3021 def identify(self, x, y):
3022 """Return where the point X,Y lies. Valid return values are "slider",
3023 "though1" and "though2"."""
3024 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00003025
3026class Scrollbar(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003027 """Scrollbar widget which displays a slider at a certain position."""
3028 def __init__(self, master=None, cnf={}, **kw):
3029 """Construct a scrollbar widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003030
Fredrik Lundh06d28152000-08-09 18:03:12 +00003031 Valid resource names: activebackground, activerelief,
3032 background, bd, bg, borderwidth, command, cursor,
3033 elementborderwidth, highlightbackground,
3034 highlightcolor, highlightthickness, jump, orient,
3035 relief, repeatdelay, repeatinterval, takefocus,
3036 troughcolor, width."""
3037 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Serhiy Storchaka320f3392014-07-23 22:00:44 +03003038 def activate(self, index=None):
3039 """Marks the element indicated by index as active.
3040 The only index values understood by this method are "arrow1",
3041 "slider", or "arrow2". If any other value is specified then no
3042 element of the scrollbar will be active. If index is not specified,
3043 the method returns the name of the element that is currently active,
3044 or None if no element is active."""
3045 return self.tk.call(self._w, 'activate', index) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00003046 def delta(self, deltax, deltay):
3047 """Return the fractional change of the scrollbar setting if it
3048 would be moved by DELTAX or DELTAY pixels."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003049 return self.tk.getdouble(
Fredrik Lundh06d28152000-08-09 18:03:12 +00003050 self.tk.call(self._w, 'delta', deltax, deltay))
3051 def fraction(self, x, y):
3052 """Return the fractional value which corresponds to a slider
3053 position of X,Y."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003054 return self.tk.getdouble(self.tk.call(self._w, 'fraction', x, y))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003055 def identify(self, x, y):
3056 """Return the element under position X,Y as one of
3057 "arrow1","slider","arrow2" or ""."""
3058 return self.tk.call(self._w, 'identify', x, y)
3059 def get(self):
3060 """Return the current fractional values (upper and lower end)
3061 of the slider position."""
3062 return self._getdoubles(self.tk.call(self._w, 'get'))
Serhiy Storchaka320f3392014-07-23 22:00:44 +03003063 def set(self, first, last):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003064 """Set the fractional values of the slider position (upper and
3065 lower ends as value between 0 and 1)."""
Serhiy Storchaka320f3392014-07-23 22:00:44 +03003066 self.tk.call(self._w, 'set', first, last)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003067
3068
3069
Guilherme Polo1fff0082009-08-14 15:05:30 +00003070class Text(Widget, XView, YView):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003071 """Text widget which can display text in various forms."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00003072 def __init__(self, master=None, cnf={}, **kw):
3073 """Construct a text widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003074
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003075 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003076
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003077 background, borderwidth, cursor,
3078 exportselection, font, foreground,
3079 highlightbackground, highlightcolor,
3080 highlightthickness, insertbackground,
3081 insertborderwidth, insertofftime,
3082 insertontime, insertwidth, padx, pady,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003083 relief, selectbackground,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003084 selectborderwidth, selectforeground,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003085 setgrid, takefocus,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003086 xscrollcommand, yscrollcommand,
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003087
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003088 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003089
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003090 autoseparators, height, maxundo,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003091 spacing1, spacing2, spacing3,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003092 state, tabs, undo, width, wrap,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003093
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003094 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00003095 Widget.__init__(self, master, 'text', cnf, kw)
Serhiy Storchaka4babb912013-11-03 14:34:25 +02003096 def bbox(self, index):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003097 """Return a tuple of (x,y,width,height) which gives the bounding
Serhiy Storchaka4babb912013-11-03 14:34:25 +02003098 box of the visible part of the character at the given index."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00003099 return self._getints(
Serhiy Storchaka4babb912013-11-03 14:34:25 +02003100 self.tk.call(self._w, 'bbox', index)) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00003101 def compare(self, index1, op, index2):
3102 """Return whether between index INDEX1 and index INDEX2 the
3103 relation OP is satisfied. OP is one of <, <=, ==, >=, >, or !=."""
3104 return self.tk.getboolean(self.tk.call(
3105 self._w, 'compare', index1, op, index2))
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07003106 def count(self, index1, index2, *args): # new in Tk 8.5
3107 """Counts the number of relevant things between the two indices.
3108 If index1 is after index2, the result will be a negative number
3109 (and this holds for each of the possible options).
3110
3111 The actual items which are counted depends on the options given by
3112 args. The result is a list of integers, one for the result of each
3113 counting option given. Valid counting options are "chars",
3114 "displaychars", "displayindices", "displaylines", "indices",
3115 "lines", "xpixels" and "ypixels". There is an additional possible
3116 option "update", which if given then all subsequent options ensure
3117 that any possible out of date information is recalculated."""
3118 args = ['-%s' % arg for arg in args if not arg.startswith('-')]
3119 args += [index1, index2]
3120 res = self.tk.call(self._w, 'count', *args) or None
3121 if res is not None and len(args) <= 3:
3122 return (res, )
3123 else:
3124 return res
Fredrik Lundh06d28152000-08-09 18:03:12 +00003125 def debug(self, boolean=None):
3126 """Turn on the internal consistency checks of the B-Tree inside the text
3127 widget according to BOOLEAN."""
Serhiy Storchaka0b9e8152013-11-03 14:29:35 +02003128 if boolean is None:
Serhiy Storchaka2f26c222014-01-11 13:13:46 +02003129 return self.tk.getboolean(self.tk.call(self._w, 'debug'))
Serhiy Storchaka0b9e8152013-11-03 14:29:35 +02003130 self.tk.call(self._w, 'debug', boolean)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003131 def delete(self, index1, index2=None):
3132 """Delete the characters between INDEX1 and INDEX2 (not included)."""
3133 self.tk.call(self._w, 'delete', index1, index2)
3134 def dlineinfo(self, index):
3135 """Return tuple (x,y,width,height,baseline) giving the bounding box
3136 and baseline position of the visible part of the line containing
3137 the character at INDEX."""
3138 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum256705b2002-04-23 13:29:43 +00003139 def dump(self, index1, index2=None, command=None, **kw):
3140 """Return the contents of the widget between index1 and index2.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003141
Guido van Rossum256705b2002-04-23 13:29:43 +00003142 The type of contents returned in filtered based on the keyword
3143 parameters; if 'all', 'image', 'mark', 'tag', 'text', or 'window' are
3144 given and true, then the corresponding items are returned. The result
3145 is a list of triples of the form (key, value, index). If none of the
3146 keywords are true then 'all' is used by default.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003147
Guido van Rossum256705b2002-04-23 13:29:43 +00003148 If the 'command' argument is given, it is called once for each element
3149 of the list of triples, with the values of each triple serving as the
3150 arguments to the function. In this case the list is not returned."""
3151 args = []
3152 func_name = None
3153 result = None
3154 if not command:
3155 # Never call the dump command without the -command flag, since the
3156 # output could involve Tcl quoting and would be a pain to parse
3157 # right. Instead just set the command to build a list of triples
3158 # as if we had done the parsing.
3159 result = []
3160 def append_triple(key, value, index, result=result):
3161 result.append((key, value, index))
3162 command = append_triple
3163 try:
3164 if not isinstance(command, str):
3165 func_name = command = self._register(command)
3166 args += ["-command", command]
3167 for key in kw:
3168 if kw[key]: args.append("-" + key)
3169 args.append(index1)
3170 if index2:
3171 args.append(index2)
3172 self.tk.call(self._w, "dump", *args)
3173 return result
3174 finally:
3175 if func_name:
3176 self.deletecommand(func_name)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003177
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003178 ## new in tk8.4
3179 def edit(self, *args):
3180 """Internal method
Raymond Hettingerff41c482003-04-06 09:01:11 +00003181
3182 This method controls the undo mechanism and
3183 the modified flag. The exact behavior of the
3184 command depends on the option argument that
3185 follows the edit argument. The following forms
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003186 of the command are currently supported:
Raymond Hettingerff41c482003-04-06 09:01:11 +00003187
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003188 edit_modified, edit_redo, edit_reset, edit_separator
3189 and edit_undo
Raymond Hettingerff41c482003-04-06 09:01:11 +00003190
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003191 """
Georg Brandlb533e262008-05-25 18:19:30 +00003192 return self.tk.call(self._w, 'edit', *args)
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003193
3194 def edit_modified(self, arg=None):
3195 """Get or Set the modified flag
Raymond Hettingerff41c482003-04-06 09:01:11 +00003196
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003197 If arg is not specified, returns the modified
Raymond Hettingerff41c482003-04-06 09:01:11 +00003198 flag of the widget. The insert, delete, edit undo and
3199 edit redo commands or the user can set or clear the
3200 modified flag. If boolean is specified, sets the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003201 modified flag of the widget to arg.
3202 """
3203 return self.edit("modified", arg)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003204
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003205 def edit_redo(self):
3206 """Redo the last undone edit
Raymond Hettingerff41c482003-04-06 09:01:11 +00003207
3208 When the undo option is true, reapplies the last
3209 undone edits provided no other edits were done since
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003210 then. Generates an error when the redo stack is empty.
3211 Does nothing when the undo option is false.
3212 """
3213 return self.edit("redo")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003214
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003215 def edit_reset(self):
3216 """Clears the undo and redo stacks
3217 """
3218 return self.edit("reset")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003219
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003220 def edit_separator(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003221 """Inserts a separator (boundary) on the undo stack.
3222
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003223 Does nothing when the undo option is false
3224 """
3225 return self.edit("separator")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003226
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003227 def edit_undo(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003228 """Undoes the last edit action
3229
3230 If the undo option is true. An edit action is defined
3231 as all the insert and delete commands that are recorded
3232 on the undo stack in between two separators. Generates
3233 an error when the undo stack is empty. Does nothing
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003234 when the undo option is false
3235 """
3236 return self.edit("undo")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003237
Fredrik Lundh06d28152000-08-09 18:03:12 +00003238 def get(self, index1, index2=None):
3239 """Return the text from INDEX1 to INDEX2 (not included)."""
3240 return self.tk.call(self._w, 'get', index1, index2)
3241 # (Image commands are new in 8.0)
3242 def image_cget(self, index, option):
3243 """Return the value of OPTION of an embedded image at INDEX."""
3244 if option[:1] != "-":
3245 option = "-" + option
3246 if option[-1:] == "_":
3247 option = option[:-1]
3248 return self.tk.call(self._w, "image", "cget", index, option)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003249 def image_configure(self, index, cnf=None, **kw):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003250 """Configure an embedded image at INDEX."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003251 return self._configure(('image', 'configure', index), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003252 def image_create(self, index, cnf={}, **kw):
3253 """Create an embedded image at INDEX."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00003254 return self.tk.call(
3255 self._w, "image", "create", index,
3256 *self._options(cnf, kw))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003257 def image_names(self):
3258 """Return all names of embedded images in this widget."""
3259 return self.tk.call(self._w, "image", "names")
3260 def index(self, index):
3261 """Return the index in the form line.char for INDEX."""
Christian Heimes57dddfb2008-01-02 18:30:52 +00003262 return str(self.tk.call(self._w, 'index', index))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003263 def insert(self, index, chars, *args):
3264 """Insert CHARS before the characters at INDEX. An additional
3265 tag can be given in ARGS. Additional CHARS and tags can follow in ARGS."""
3266 self.tk.call((self._w, 'insert', index, chars) + args)
3267 def mark_gravity(self, markName, direction=None):
3268 """Change the gravity of a mark MARKNAME to DIRECTION (LEFT or RIGHT).
3269 Return the current value if None is given for DIRECTION."""
3270 return self.tk.call(
3271 (self._w, 'mark', 'gravity', markName, direction))
3272 def mark_names(self):
3273 """Return all mark names."""
3274 return self.tk.splitlist(self.tk.call(
3275 self._w, 'mark', 'names'))
3276 def mark_set(self, markName, index):
3277 """Set mark MARKNAME before the character at INDEX."""
3278 self.tk.call(self._w, 'mark', 'set', markName, index)
3279 def mark_unset(self, *markNames):
3280 """Delete all marks in MARKNAMES."""
3281 self.tk.call((self._w, 'mark', 'unset') + markNames)
3282 def mark_next(self, index):
3283 """Return the name of the next mark after INDEX."""
3284 return self.tk.call(self._w, 'mark', 'next', index) or None
3285 def mark_previous(self, index):
3286 """Return the name of the previous mark before INDEX."""
3287 return self.tk.call(self._w, 'mark', 'previous', index) or None
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07003288 def peer_create(self, newPathName, cnf={}, **kw): # new in Tk 8.5
3289 """Creates a peer text widget with the given newPathName, and any
3290 optional standard configuration options. By default the peer will
Terry Jan Reedy0f847642013-03-11 18:34:00 -04003291 have the same start and end line as the parent widget, but
Martin Pantere26da7c2016-06-02 10:07:09 +00003292 these can be overridden with the standard configuration options."""
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07003293 self.tk.call(self._w, 'peer', 'create', newPathName,
3294 *self._options(cnf, kw))
3295 def peer_names(self): # new in Tk 8.5
3296 """Returns a list of peers of this widget (this does not include
3297 the widget itself)."""
3298 return self.tk.splitlist(self.tk.call(self._w, 'peer', 'names'))
3299 def replace(self, index1, index2, chars, *args): # new in Tk 8.5
3300 """Replaces the range of characters between index1 and index2 with
3301 the given characters and tags specified by args.
3302
3303 See the method insert for some more information about args, and the
3304 method delete for information about the indices."""
3305 self.tk.call(self._w, 'replace', index1, index2, chars, *args)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003306 def scan_mark(self, x, y):
3307 """Remember the current X, Y coordinates."""
3308 self.tk.call(self._w, 'scan', 'mark', x, y)
3309 def scan_dragto(self, x, y):
3310 """Adjust the view of the text to 10 times the
3311 difference between X and Y and the coordinates given in
3312 scan_mark."""
3313 self.tk.call(self._w, 'scan', 'dragto', x, y)
3314 def search(self, pattern, index, stopindex=None,
3315 forwards=None, backwards=None, exact=None,
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 regexp=None, nocase=None, count=None, elide=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003317 """Search PATTERN beginning from INDEX until STOPINDEX.
Guilherme Poloae098992009-02-09 16:44:24 +00003318 Return the index of the first character of a match or an
3319 empty string."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00003320 args = [self._w, 'search']
3321 if forwards: args.append('-forwards')
3322 if backwards: args.append('-backwards')
3323 if exact: args.append('-exact')
3324 if regexp: args.append('-regexp')
3325 if nocase: args.append('-nocase')
Thomas Wouters89f507f2006-12-13 04:49:30 +00003326 if elide: args.append('-elide')
Fredrik Lundh06d28152000-08-09 18:03:12 +00003327 if count: args.append('-count'); args.append(count)
Guilherme Poloae098992009-02-09 16:44:24 +00003328 if pattern and pattern[0] == '-': args.append('--')
Fredrik Lundh06d28152000-08-09 18:03:12 +00003329 args.append(pattern)
3330 args.append(index)
3331 if stopindex: args.append(stopindex)
Guilherme Polo56f5be52009-03-07 01:54:57 +00003332 return str(self.tk.call(tuple(args)))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003333 def see(self, index):
3334 """Scroll such that the character at INDEX is visible."""
3335 self.tk.call(self._w, 'see', index)
3336 def tag_add(self, tagName, index1, *args):
3337 """Add tag TAGNAME to all characters between INDEX1 and index2 in ARGS.
3338 Additional pairs of indices may follow in ARGS."""
3339 self.tk.call(
3340 (self._w, 'tag', 'add', tagName, index1) + args)
3341 def tag_unbind(self, tagName, sequence, funcid=None):
3342 """Unbind for all characters with TAGNAME for event SEQUENCE the
3343 function identified with FUNCID."""
3344 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
3345 if funcid:
3346 self.deletecommand(funcid)
3347 def tag_bind(self, tagName, sequence, func, add=None):
3348 """Bind to all characters with TAGNAME at event SEQUENCE a call to function FUNC.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003349
Fredrik Lundh06d28152000-08-09 18:03:12 +00003350 An additional boolean parameter ADD specifies whether FUNC will be
3351 called additionally to the other bound function or whether it will
3352 replace the previous function. See bind for the return value."""
3353 return self._bind((self._w, 'tag', 'bind', tagName),
3354 sequence, func, add)
3355 def tag_cget(self, tagName, option):
3356 """Return the value of OPTION for tag TAGNAME."""
3357 if option[:1] != '-':
3358 option = '-' + option
3359 if option[-1:] == '_':
3360 option = option[:-1]
3361 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003362 def tag_configure(self, tagName, cnf=None, **kw):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003363 """Configure a tag TAGNAME."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003364 return self._configure(('tag', 'configure', tagName), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003365 tag_config = tag_configure
3366 def tag_delete(self, *tagNames):
3367 """Delete all tags in TAGNAMES."""
3368 self.tk.call((self._w, 'tag', 'delete') + tagNames)
3369 def tag_lower(self, tagName, belowThis=None):
3370 """Change the priority of tag TAGNAME such that it is lower
3371 than the priority of BELOWTHIS."""
3372 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
3373 def tag_names(self, index=None):
3374 """Return a list of all tag names."""
3375 return self.tk.splitlist(
3376 self.tk.call(self._w, 'tag', 'names', index))
3377 def tag_nextrange(self, tagName, index1, index2=None):
3378 """Return a list of start and end index for the first sequence of
3379 characters between INDEX1 and INDEX2 which all have tag TAGNAME.
3380 The text is searched forward from INDEX1."""
3381 return self.tk.splitlist(self.tk.call(
3382 self._w, 'tag', 'nextrange', tagName, index1, index2))
3383 def tag_prevrange(self, tagName, index1, index2=None):
3384 """Return a list of start and end index for the first sequence of
3385 characters between INDEX1 and INDEX2 which all have tag TAGNAME.
3386 The text is searched backwards from INDEX1."""
3387 return self.tk.splitlist(self.tk.call(
3388 self._w, 'tag', 'prevrange', tagName, index1, index2))
3389 def tag_raise(self, tagName, aboveThis=None):
3390 """Change the priority of tag TAGNAME such that it is higher
3391 than the priority of ABOVETHIS."""
3392 self.tk.call(
3393 self._w, 'tag', 'raise', tagName, aboveThis)
3394 def tag_ranges(self, tagName):
3395 """Return a list of ranges of text which have tag TAGNAME."""
3396 return self.tk.splitlist(self.tk.call(
3397 self._w, 'tag', 'ranges', tagName))
3398 def tag_remove(self, tagName, index1, index2=None):
3399 """Remove tag TAGNAME from all characters between INDEX1 and INDEX2."""
3400 self.tk.call(
3401 self._w, 'tag', 'remove', tagName, index1, index2)
3402 def window_cget(self, index, option):
3403 """Return the value of OPTION of an embedded window at INDEX."""
3404 if option[:1] != '-':
3405 option = '-' + option
3406 if option[-1:] == '_':
3407 option = option[:-1]
3408 return self.tk.call(self._w, 'window', 'cget', index, option)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003409 def window_configure(self, index, cnf=None, **kw):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003410 """Configure an embedded window at INDEX."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003411 return self._configure(('window', 'configure', index), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003412 window_config = window_configure
3413 def window_create(self, index, cnf={}, **kw):
3414 """Create a window at INDEX."""
3415 self.tk.call(
3416 (self._w, 'window', 'create', index)
3417 + self._options(cnf, kw))
3418 def window_names(self):
3419 """Return all names of embedded windows in this widget."""
3420 return self.tk.splitlist(
3421 self.tk.call(self._w, 'window', 'names'))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003422 def yview_pickplace(self, *what):
3423 """Obsolete function, use see."""
3424 self.tk.call((self._w, 'yview', '-pickplace') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00003425
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003426
Guido van Rossum28574b51996-10-21 15:16:51 +00003427class _setit:
Fredrik Lundh06d28152000-08-09 18:03:12 +00003428 """Internal class. It wraps the command in the widget OptionMenu."""
3429 def __init__(self, var, value, callback=None):
3430 self.__value = value
3431 self.__var = var
3432 self.__callback = callback
3433 def __call__(self, *args):
3434 self.__var.set(self.__value)
3435 if self.__callback:
Raymond Hettingerff41c482003-04-06 09:01:11 +00003436 self.__callback(self.__value, *args)
Guido van Rossum28574b51996-10-21 15:16:51 +00003437
3438class OptionMenu(Menubutton):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003439 """OptionMenu which allows the user to select a value from a menu."""
3440 def __init__(self, master, variable, value, *values, **kwargs):
3441 """Construct an optionmenu widget with the parent MASTER, with
3442 the resource textvariable set to VARIABLE, the initially selected
3443 value VALUE, the other menu values VALUES and an additional
3444 keyword argument command."""
3445 kw = {"borderwidth": 2, "textvariable": variable,
3446 "indicatoron": 1, "relief": RAISED, "anchor": "c",
3447 "highlightthickness": 2}
3448 Widget.__init__(self, master, "menubutton", kw)
3449 self.widgetName = 'tk_optionMenu'
3450 menu = self.__menu = Menu(self, name="menu", tearoff=0)
3451 self.menuname = menu._w
3452 # 'command' is the only supported keyword
3453 callback = kwargs.get('command')
Guido van Rossume014a132006-08-19 16:53:45 +00003454 if 'command' in kwargs:
Fredrik Lundh06d28152000-08-09 18:03:12 +00003455 del kwargs['command']
3456 if kwargs:
Collin Winterce36ad82007-08-30 01:19:48 +00003457 raise TclError('unknown option -'+kwargs.keys()[0])
Fredrik Lundh06d28152000-08-09 18:03:12 +00003458 menu.add_command(label=value,
3459 command=_setit(variable, value, callback))
3460 for v in values:
3461 menu.add_command(label=v,
3462 command=_setit(variable, v, callback))
3463 self["menu"] = menu
Guido van Rossum28574b51996-10-21 15:16:51 +00003464
Fredrik Lundh06d28152000-08-09 18:03:12 +00003465 def __getitem__(self, name):
3466 if name == 'menu':
3467 return self.__menu
3468 return Widget.__getitem__(self, name)
Guido van Rossum28574b51996-10-21 15:16:51 +00003469
Fredrik Lundh06d28152000-08-09 18:03:12 +00003470 def destroy(self):
3471 """Destroy this widget and the associated menu."""
3472 Menubutton.destroy(self)
3473 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00003474
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003475class Image:
Fredrik Lundh06d28152000-08-09 18:03:12 +00003476 """Base class for images."""
Martin v. Löwis0d8ce612000-09-08 16:28:30 +00003477 _last_id = 0
Fredrik Lundh06d28152000-08-09 18:03:12 +00003478 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
3479 self.name = None
3480 if not master:
3481 master = _default_root
3482 if not master:
Collin Winterce36ad82007-08-30 01:19:48 +00003483 raise RuntimeError('Too early to create image')
Serhiy Storchakad00aff22014-08-24 09:07:47 +03003484 self.tk = getattr(master, 'tk', master)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003485 if not name:
Martin v. Löwis0d8ce612000-09-08 16:28:30 +00003486 Image._last_id += 1
Walter Dörwald70a6b492004-02-12 17:35:32 +00003487 name = "pyimage%r" % (Image._last_id,) # tk itself would use image<x>
Fredrik Lundh06d28152000-08-09 18:03:12 +00003488 if kw and cnf: cnf = _cnfmerge((cnf, kw))
3489 elif kw: cnf = kw
3490 options = ()
3491 for k, v in cnf.items():
Florent Xicluna5d1155c2011-10-28 14:45:05 +02003492 if callable(v):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003493 v = self._register(v)
3494 options = options + ('-'+k, v)
3495 self.tk.call(('image', 'create', imgtype, name,) + options)
3496 self.name = name
3497 def __str__(self): return self.name
3498 def __del__(self):
3499 if self.name:
3500 try:
3501 self.tk.call('image', 'delete', self.name)
3502 except TclError:
3503 # May happen if the root was destroyed
3504 pass
3505 def __setitem__(self, key, value):
3506 self.tk.call(self.name, 'configure', '-'+key, value)
3507 def __getitem__(self, key):
3508 return self.tk.call(self.name, 'configure', '-'+key)
3509 def configure(self, **kw):
3510 """Configure the image."""
3511 res = ()
3512 for k, v in _cnfmerge(kw).items():
3513 if v is not None:
3514 if k[-1] == '_': k = k[:-1]
Florent Xicluna5d1155c2011-10-28 14:45:05 +02003515 if callable(v):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003516 v = self._register(v)
3517 res = res + ('-'+k, v)
3518 self.tk.call((self.name, 'config') + res)
3519 config = configure
3520 def height(self):
3521 """Return the height of the image."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003522 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00003523 self.tk.call('image', 'height', self.name))
3524 def type(self):
3525 """Return the type of the imgage, e.g. "photo" or "bitmap"."""
3526 return self.tk.call('image', 'type', self.name)
3527 def width(self):
3528 """Return the width of the image."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003529 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00003530 self.tk.call('image', 'width', self.name))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003531
3532class PhotoImage(Image):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003533 """Widget which can display colored images in GIF, PPM/PGM format."""
3534 def __init__(self, name=None, cnf={}, master=None, **kw):
3535 """Create an image with NAME.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003536
Fredrik Lundh06d28152000-08-09 18:03:12 +00003537 Valid resource names: data, format, file, gamma, height, palette,
3538 width."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00003539 Image.__init__(self, 'photo', name, cnf, master, **kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003540 def blank(self):
3541 """Display a transparent image."""
3542 self.tk.call(self.name, 'blank')
3543 def cget(self, option):
3544 """Return the value of OPTION."""
3545 return self.tk.call(self.name, 'cget', '-' + option)
3546 # XXX config
3547 def __getitem__(self, key):
3548 return self.tk.call(self.name, 'cget', '-' + key)
3549 # XXX copy -from, -to, ...?
3550 def copy(self):
3551 """Return a new PhotoImage with the same image as this widget."""
Serhiy Storchakad00aff22014-08-24 09:07:47 +03003552 destImage = PhotoImage(master=self.tk)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003553 self.tk.call(destImage, 'copy', self.name)
3554 return destImage
Terry Jan Reedyca0250a2016-03-11 15:30:35 -05003555 def zoom(self, x, y=''):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003556 """Return a new PhotoImage with the same image as this widget
Terry Jan Reedyca0250a2016-03-11 15:30:35 -05003557 but zoom it with a factor of x in the X direction and y in the Y
3558 direction. If y is not given, the default value is the same as x.
3559 """
Serhiy Storchakad00aff22014-08-24 09:07:47 +03003560 destImage = PhotoImage(master=self.tk)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003561 if y=='': y=x
3562 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
3563 return destImage
Terry Jan Reedyca0250a2016-03-11 15:30:35 -05003564 def subsample(self, x, y=''):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003565 """Return a new PhotoImage based on the same image as this widget
Terry Jan Reedyca0250a2016-03-11 15:30:35 -05003566 but use only every Xth or Yth pixel. If y is not given, the
3567 default value is the same as x.
3568 """
Serhiy Storchakad00aff22014-08-24 09:07:47 +03003569 destImage = PhotoImage(master=self.tk)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003570 if y=='': y=x
3571 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
3572 return destImage
3573 def get(self, x, y):
3574 """Return the color (red, green, blue) of the pixel at X,Y."""
3575 return self.tk.call(self.name, 'get', x, y)
3576 def put(self, data, to=None):
Mark Dickinson934896d2009-02-21 20:59:32 +00003577 """Put row formatted colors to image starting from
Fredrik Lundh06d28152000-08-09 18:03:12 +00003578 position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))"""
3579 args = (self.name, 'put', data)
3580 if to:
3581 if to[0] == '-to':
3582 to = to[1:]
3583 args = args + ('-to',) + tuple(to)
3584 self.tk.call(args)
3585 # XXX read
3586 def write(self, filename, format=None, from_coords=None):
3587 """Write image to file FILENAME in FORMAT starting from
3588 position FROM_COORDS."""
3589 args = (self.name, 'write', filename)
3590 if format:
3591 args = args + ('-format', format)
3592 if from_coords:
3593 args = args + ('-from',) + tuple(from_coords)
3594 self.tk.call(args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003595
3596class BitmapImage(Image):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003597 """Widget which can display a bitmap."""
3598 def __init__(self, name=None, cnf={}, master=None, **kw):
3599 """Create a bitmap with NAME.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003600
Fredrik Lundh06d28152000-08-09 18:03:12 +00003601 Valid resource names: background, data, file, foreground, maskdata, maskfile."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00003602 Image.__init__(self, 'bitmap', name, cnf, master, **kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003603
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02003604def image_names():
3605 return _default_root.tk.splitlist(_default_root.tk.call('image', 'names'))
3606
3607def image_types():
3608 return _default_root.tk.splitlist(_default_root.tk.call('image', 'types'))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003609
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003610
Guilherme Polo1fff0082009-08-14 15:05:30 +00003611class Spinbox(Widget, XView):
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003612 """spinbox widget."""
3613 def __init__(self, master=None, cnf={}, **kw):
3614 """Construct a spinbox widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003615
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003616 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003617
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003618 activebackground, background, borderwidth,
3619 cursor, exportselection, font, foreground,
3620 highlightbackground, highlightcolor,
3621 highlightthickness, insertbackground,
3622 insertborderwidth, insertofftime,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003623 insertontime, insertwidth, justify, relief,
3624 repeatdelay, repeatinterval,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003625 selectbackground, selectborderwidth
3626 selectforeground, takefocus, textvariable
3627 xscrollcommand.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003628
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003629 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003630
3631 buttonbackground, buttoncursor,
3632 buttondownrelief, buttonuprelief,
3633 command, disabledbackground,
3634 disabledforeground, format, from,
3635 invalidcommand, increment,
3636 readonlybackground, state, to,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003637 validate, validatecommand values,
3638 width, wrap,
3639 """
3640 Widget.__init__(self, master, 'spinbox', cnf, kw)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003641
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003642 def bbox(self, index):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003643 """Return a tuple of X1,Y1,X2,Y2 coordinates for a
3644 rectangle which encloses the character given by index.
3645
3646 The first two elements of the list give the x and y
3647 coordinates of the upper-left corner of the screen
3648 area covered by the character (in pixels relative
3649 to the widget) and the last two elements give the
3650 width and height of the character, in pixels. The
3651 bounding box may refer to a region outside the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003652 visible area of the window.
3653 """
Serhiy Storchaka2849e0d2013-11-03 14:13:34 +02003654 return self._getints(self.tk.call(self._w, 'bbox', index)) or None
Raymond Hettingerff41c482003-04-06 09:01:11 +00003655
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003656 def delete(self, first, last=None):
3657 """Delete one or more elements of the spinbox.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003658
3659 First is the index of the first character to delete,
3660 and last is the index of the character just after
3661 the last one to delete. If last isn't specified it
3662 defaults to first+1, i.e. a single character is
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003663 deleted. This command returns an empty string.
3664 """
3665 return self.tk.call(self._w, 'delete', first, last)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003666
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003667 def get(self):
3668 """Returns the spinbox's string"""
3669 return self.tk.call(self._w, 'get')
Raymond Hettingerff41c482003-04-06 09:01:11 +00003670
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003671 def icursor(self, index):
3672 """Alter the position of the insertion cursor.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003673
3674 The insertion cursor will be displayed just before
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003675 the character given by index. Returns an empty string
3676 """
3677 return self.tk.call(self._w, 'icursor', index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003678
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003679 def identify(self, x, y):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003680 """Returns the name of the widget at position x, y
3681
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003682 Return value is one of: none, buttondown, buttonup, entry
3683 """
3684 return self.tk.call(self._w, 'identify', x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003685
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003686 def index(self, index):
3687 """Returns the numerical index corresponding to index
3688 """
3689 return self.tk.call(self._w, 'index', index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003690
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003691 def insert(self, index, s):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003692 """Insert string s at index
3693
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003694 Returns an empty string.
3695 """
3696 return self.tk.call(self._w, 'insert', index, s)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003697
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003698 def invoke(self, element):
3699 """Causes the specified element to be invoked
Raymond Hettingerff41c482003-04-06 09:01:11 +00003700
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003701 The element could be buttondown or buttonup
3702 triggering the action associated with it.
3703 """
3704 return self.tk.call(self._w, 'invoke', element)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003705
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003706 def scan(self, *args):
3707 """Internal function."""
3708 return self._getints(
3709 self.tk.call((self._w, 'scan') + args)) or ()
Raymond Hettingerff41c482003-04-06 09:01:11 +00003710
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003711 def scan_mark(self, x):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003712 """Records x and the current view in the spinbox window;
3713
3714 used in conjunction with later scan dragto commands.
3715 Typically this command is associated with a mouse button
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003716 press in the widget. It returns an empty string.
3717 """
3718 return self.scan("mark", x)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003719
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003720 def scan_dragto(self, x):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003721 """Compute the difference between the given x argument
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003722 and the x argument to the last scan mark command
Raymond Hettingerff41c482003-04-06 09:01:11 +00003723
3724 It then adjusts the view left or right by 10 times the
3725 difference in x-coordinates. This command is typically
3726 associated with mouse motion events in the widget, to
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003727 produce the effect of dragging the spinbox at high speed
3728 through the window. The return value is an empty string.
3729 """
3730 return self.scan("dragto", x)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003731
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003732 def selection(self, *args):
3733 """Internal function."""
3734 return self._getints(
3735 self.tk.call((self._w, 'selection') + args)) or ()
Raymond Hettingerff41c482003-04-06 09:01:11 +00003736
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003737 def selection_adjust(self, index):
3738 """Locate the end of the selection nearest to the character
Raymond Hettingerff41c482003-04-06 09:01:11 +00003739 given by index,
3740
3741 Then adjust that end of the selection to be at index
3742 (i.e including but not going beyond index). The other
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003743 end of the selection is made the anchor point for future
Raymond Hettingerff41c482003-04-06 09:01:11 +00003744 select to commands. If the selection isn't currently in
3745 the spinbox, then a new selection is created to include
3746 the characters between index and the most recent selection
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003747 anchor point, inclusive. Returns an empty string.
3748 """
3749 return self.selection("adjust", index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003750
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003751 def selection_clear(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003752 """Clear the selection
3753
3754 If the selection isn't in this widget then the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003755 command has no effect. Returns an empty string.
3756 """
3757 return self.selection("clear")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003758
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003759 def selection_element(self, element=None):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003760 """Sets or gets the currently selected element.
3761
3762 If a spinbutton element is specified, it will be
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003763 displayed depressed
3764 """
3765 return self.selection("element", element)
3766
3767###########################################################################
3768
3769class LabelFrame(Widget):
3770 """labelframe widget."""
3771 def __init__(self, master=None, cnf={}, **kw):
3772 """Construct a labelframe widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003773
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003774 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003775
3776 borderwidth, cursor, font, foreground,
3777 highlightbackground, highlightcolor,
3778 highlightthickness, padx, pady, relief,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003779 takefocus, text
Raymond Hettingerff41c482003-04-06 09:01:11 +00003780
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003781 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003782
3783 background, class, colormap, container,
3784 height, labelanchor, labelwidget,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003785 visual, width
3786 """
3787 Widget.__init__(self, master, 'labelframe', cnf, kw)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003788
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003789########################################################################
3790
3791class PanedWindow(Widget):
3792 """panedwindow widget."""
3793 def __init__(self, master=None, cnf={}, **kw):
3794 """Construct a panedwindow widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003795
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003796 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003797
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003798 background, borderwidth, cursor, height,
3799 orient, relief, width
Raymond Hettingerff41c482003-04-06 09:01:11 +00003800
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003801 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003802
3803 handlepad, handlesize, opaqueresize,
3804 sashcursor, sashpad, sashrelief,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003805 sashwidth, showhandle,
3806 """
3807 Widget.__init__(self, master, 'panedwindow', cnf, kw)
3808
3809 def add(self, child, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003810 """Add a child widget to the panedwindow in a new pane.
3811
3812 The child argument is the name of the child widget
3813 followed by pairs of arguments that specify how to
Guilherme Polo86425562009-05-31 21:35:23 +00003814 manage the windows. The possible options and values
3815 are the ones accepted by the paneconfigure method.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003816 """
3817 self.tk.call((self._w, 'add', child) + self._options(kw))
Raymond Hettingerff41c482003-04-06 09:01:11 +00003818
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003819 def remove(self, child):
3820 """Remove the pane containing child from the panedwindow
Raymond Hettingerff41c482003-04-06 09:01:11 +00003821
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003822 All geometry management options for child will be forgotten.
3823 """
3824 self.tk.call(self._w, 'forget', child)
3825 forget=remove
Raymond Hettingerff41c482003-04-06 09:01:11 +00003826
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003827 def identify(self, x, y):
3828 """Identify the panedwindow component at point x, y
Raymond Hettingerff41c482003-04-06 09:01:11 +00003829
3830 If the point is over a sash or a sash handle, the result
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003831 is a two element list containing the index of the sash or
Raymond Hettingerff41c482003-04-06 09:01:11 +00003832 handle, and a word indicating whether it is over a sash
3833 or a handle, such as {0 sash} or {2 handle}. If the point
3834 is over any other part of the panedwindow, the result is
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003835 an empty list.
3836 """
3837 return self.tk.call(self._w, 'identify', x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003838
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003839 def proxy(self, *args):
3840 """Internal function."""
3841 return self._getints(
Raymond Hettingerff41c482003-04-06 09:01:11 +00003842 self.tk.call((self._w, 'proxy') + args)) or ()
3843
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003844 def proxy_coord(self):
3845 """Return the x and y pair of the most recent proxy location
3846 """
3847 return self.proxy("coord")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003848
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003849 def proxy_forget(self):
3850 """Remove the proxy from the display.
3851 """
3852 return self.proxy("forget")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003853
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003854 def proxy_place(self, x, y):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003855 """Place the proxy at the given x and y coordinates.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003856 """
3857 return self.proxy("place", x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003858
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003859 def sash(self, *args):
3860 """Internal function."""
3861 return self._getints(
3862 self.tk.call((self._w, 'sash') + args)) or ()
Raymond Hettingerff41c482003-04-06 09:01:11 +00003863
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003864 def sash_coord(self, index):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003865 """Return the current x and y pair for the sash given by index.
3866
3867 Index must be an integer between 0 and 1 less than the
3868 number of panes in the panedwindow. The coordinates given are
3869 those of the top left corner of the region containing the sash.
3870 pathName sash dragto index x y This command computes the
3871 difference between the given coordinates and the coordinates
3872 given to the last sash coord command for the given sash. It then
3873 moves that sash the computed difference. The return value is the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003874 empty string.
3875 """
3876 return self.sash("coord", index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003877
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003878 def sash_mark(self, index):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003879 """Records x and y for the sash given by index;
3880
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003881 Used in conjunction with later dragto commands to move the sash.
3882 """
3883 return self.sash("mark", index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003884
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003885 def sash_place(self, index, x, y):
3886 """Place the sash given by index at the given coordinates
3887 """
3888 return self.sash("place", index, x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003889
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003890 def panecget(self, child, option):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003891 """Query a management option for window.
3892
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003893 Option may be any value allowed by the paneconfigure subcommand
3894 """
3895 return self.tk.call(
3896 (self._w, 'panecget') + (child, '-'+option))
Raymond Hettingerff41c482003-04-06 09:01:11 +00003897
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003898 def paneconfigure(self, tagOrId, cnf=None, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003899 """Query or modify the management options for window.
3900
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003901 If no option is specified, returns a list describing all
Raymond Hettingerff41c482003-04-06 09:01:11 +00003902 of the available options for pathName. If option is
3903 specified with no value, then the command returns a list
3904 describing the one named option (this list will be identical
3905 to the corresponding sublist of the value returned if no
3906 option is specified). If one or more option-value pairs are
3907 specified, then the command modifies the given widget
3908 option(s) to have the given value(s); in this case the
3909 command returns an empty string. The following options
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003910 are supported:
Raymond Hettingerff41c482003-04-06 09:01:11 +00003911
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003912 after window
Raymond Hettingerff41c482003-04-06 09:01:11 +00003913 Insert the window after the window specified. window
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003914 should be the name of a window already managed by pathName.
3915 before window
Raymond Hettingerff41c482003-04-06 09:01:11 +00003916 Insert the window before the window specified. window
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003917 should be the name of a window already managed by pathName.
3918 height size
Raymond Hettingerff41c482003-04-06 09:01:11 +00003919 Specify a height for the window. The height will be the
3920 outer dimension of the window including its border, if
3921 any. If size is an empty string, or if -height is not
3922 specified, then the height requested internally by the
3923 window will be used initially; the height may later be
3924 adjusted by the movement of sashes in the panedwindow.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003925 Size may be any value accepted by Tk_GetPixels.
3926 minsize n
Raymond Hettingerff41c482003-04-06 09:01:11 +00003927 Specifies that the size of the window cannot be made
3928 less than n. This constraint only affects the size of
3929 the widget in the paned dimension -- the x dimension
3930 for horizontal panedwindows, the y dimension for
3931 vertical panedwindows. May be any value accepted by
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003932 Tk_GetPixels.
3933 padx n
Raymond Hettingerff41c482003-04-06 09:01:11 +00003934 Specifies a non-negative value indicating how much
3935 extra space to leave on each side of the window in
3936 the X-direction. The value may have any of the forms
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003937 accepted by Tk_GetPixels.
3938 pady n
3939 Specifies a non-negative value indicating how much
Raymond Hettingerff41c482003-04-06 09:01:11 +00003940 extra space to leave on each side of the window in
3941 the Y-direction. The value may have any of the forms
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003942 accepted by Tk_GetPixels.
3943 sticky style
Raymond Hettingerff41c482003-04-06 09:01:11 +00003944 If a window's pane is larger than the requested
3945 dimensions of the window, this option may be used
3946 to position (or stretch) the window within its pane.
3947 Style is a string that contains zero or more of the
3948 characters n, s, e or w. The string can optionally
3949 contains spaces or commas, but they are ignored. Each
3950 letter refers to a side (north, south, east, or west)
3951 that the window will "stick" to. If both n and s
3952 (or e and w) are specified, the window will be
3953 stretched to fill the entire height (or width) of
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003954 its cavity.
3955 width size
Raymond Hettingerff41c482003-04-06 09:01:11 +00003956 Specify a width for the window. The width will be
3957 the outer dimension of the window including its
3958 border, if any. If size is an empty string, or
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003959 if -width is not specified, then the width requested
Raymond Hettingerff41c482003-04-06 09:01:11 +00003960 internally by the window will be used initially; the
3961 width may later be adjusted by the movement of sashes
3962 in the panedwindow. Size may be any value accepted by
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003963 Tk_GetPixels.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003964
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003965 """
3966 if cnf is None and not kw:
Serhiy Storchaka848972c2013-12-25 16:35:38 +02003967 return self._getconfigure(self._w, 'paneconfigure', tagOrId)
Guido van Rossum13257902007-06-07 23:15:56 +00003968 if isinstance(cnf, str) and not kw:
Serhiy Storchaka848972c2013-12-25 16:35:38 +02003969 return self._getconfigure1(
3970 self._w, 'paneconfigure', tagOrId, '-'+cnf)
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003971 self.tk.call((self._w, 'paneconfigure', tagOrId) +
3972 self._options(cnf, kw))
3973 paneconfig = paneconfigure
3974
3975 def panes(self):
3976 """Returns an ordered list of the child panes."""
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02003977 return self.tk.splitlist(self.tk.call(self._w, 'panes'))
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003978
Guido van Rossumc417ef81996-08-21 23:38:59 +00003979# Test:
3980
3981def _test():
Fredrik Lundh06d28152000-08-09 18:03:12 +00003982 root = Tk()
3983 text = "This is Tcl/Tk version %s" % TclVersion
Serhiy Storchaka6716d602014-07-30 19:19:21 +03003984 text += "\nThis should be a cedilla: \xe7"
Fredrik Lundh06d28152000-08-09 18:03:12 +00003985 label = Label(root, text=text)
3986 label.pack()
3987 test = Button(root, text="Click me!",
3988 command=lambda root=root: root.test.configure(
3989 text="[%s]" % root.test['text']))
3990 test.pack()
3991 root.test = test
3992 quit = Button(root, text="QUIT", command=root.destroy)
3993 quit.pack()
3994 # The following three commands are needed so the window pops
3995 # up on top on Windows...
3996 root.iconify()
3997 root.update()
3998 root.deiconify()
3999 root.mainloop()
Guido van Rossumc417ef81996-08-21 23:38:59 +00004000
4001if __name__ == '__main__':
Fredrik Lundh06d28152000-08-09 18:03:12 +00004002 _test()