blob: ff85f837d1d5941c4ae572e64b63ca5140ee5801 [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])
Serhiy Storchaka5bb5bbf2018-06-30 09:20:28 +030064 if _magic_re.search(value):
Serhiy Storchakab1396522013-01-15 17:56:08 +020065 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)
Serhiy Storchaka5bb5bbf2018-06-30 09:20:28 +030075 value = value.replace('\n', r'\n')
Serhiy Storchakab1396522013-01-15 17:56:08 +020076 value = _space_re.sub(r'\\\1', value)
Serhiy Storchaka5bb5bbf2018-06-30 09:20:28 +030077 if value[0] == '"':
78 value = '\\' + value
Serhiy Storchakab1396522013-01-15 17:56:08 +020079 elif value[0] == '"' or _space_re.search(value):
80 value = '{%s}' % value
81 return value
82
Guido van Rossum13257902007-06-07 23:15:56 +000083def _flatten(seq):
Fredrik Lundh06d28152000-08-09 18:03:12 +000084 """Internal function."""
85 res = ()
Guido van Rossum13257902007-06-07 23:15:56 +000086 for item in seq:
87 if isinstance(item, (tuple, list)):
Fredrik Lundh06d28152000-08-09 18:03:12 +000088 res = res + _flatten(item)
89 elif item is not None:
90 res = res + (item,)
91 return res
Guido van Rossum2dcf5291994-07-06 09:23:20 +000092
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +000093try: _flatten = _tkinter._flatten
94except AttributeError: pass
95
Guido van Rossum2dcf5291994-07-06 09:23:20 +000096def _cnfmerge(cnfs):
Fredrik Lundh06d28152000-08-09 18:03:12 +000097 """Internal function."""
Guido van Rossum13257902007-06-07 23:15:56 +000098 if isinstance(cnfs, dict):
Fredrik Lundh06d28152000-08-09 18:03:12 +000099 return cnfs
Guido van Rossum13257902007-06-07 23:15:56 +0000100 elif isinstance(cnfs, (type(None), str)):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000101 return cnfs
102 else:
103 cnf = {}
104 for c in _flatten(cnfs):
105 try:
106 cnf.update(c)
Guido van Rossumb940e112007-01-10 16:19:56 +0000107 except (AttributeError, TypeError) as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000108 print("_cnfmerge: fallback due to:", msg)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000109 for k, v in c.items():
110 cnf[k] = v
111 return cnf
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000112
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +0000113try: _cnfmerge = _tkinter._cnfmerge
114except AttributeError: pass
115
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +0300116def _splitdict(tk, v, cut_minus=True, conv=None):
117 """Return a properly formatted dict built from Tcl list pairs.
118
119 If cut_minus is True, the supposed '-' prefix will be removed from
120 keys. If conv is specified, it is used to convert values.
121
122 Tcl list is expected to contain an even number of elements.
123 """
124 t = tk.splitlist(v)
125 if len(t) % 2:
126 raise RuntimeError('Tcl list representing a dict is expected '
127 'to contain an even number of elements')
128 it = iter(t)
129 dict = {}
130 for key, value in zip(it, it):
131 key = str(key)
132 if cut_minus and key[0] == '-':
133 key = key[1:]
134 if conv:
135 value = conv(value)
136 dict[key] = value
137 return dict
138
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300139
140class EventType(str, enum.Enum):
141 KeyPress = '2'
142 Key = KeyPress,
143 KeyRelease = '3'
144 ButtonPress = '4'
145 Button = ButtonPress,
146 ButtonRelease = '5'
147 Motion = '6'
148 Enter = '7'
149 Leave = '8'
150 FocusIn = '9'
151 FocusOut = '10'
152 Keymap = '11' # undocumented
153 Expose = '12'
154 GraphicsExpose = '13' # undocumented
155 NoExpose = '14' # undocumented
156 Visibility = '15'
157 Create = '16'
158 Destroy = '17'
159 Unmap = '18'
160 Map = '19'
161 MapRequest = '20'
162 Reparent = '21'
163 Configure = '22'
164 ConfigureRequest = '23'
165 Gravity = '24'
166 ResizeRequest = '25'
167 Circulate = '26'
168 CirculateRequest = '27'
169 Property = '28'
170 SelectionClear = '29' # undocumented
171 SelectionRequest = '30' # undocumented
172 Selection = '31' # undocumented
173 Colormap = '32'
174 ClientMessage = '33' # undocumented
175 Mapping = '34' # undocumented
176 VirtualEvent = '35', # undocumented
177 Activate = '36',
178 Deactivate = '37',
179 MouseWheel = '38',
180 def __str__(self):
181 return self.name
182
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000183class Event:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000184 """Container for the properties of an event.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000185
Fredrik Lundh06d28152000-08-09 18:03:12 +0000186 Instances of this type are generated if one of the following events occurs:
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000187
Fredrik Lundh06d28152000-08-09 18:03:12 +0000188 KeyPress, KeyRelease - for keyboard events
189 ButtonPress, ButtonRelease, Motion, Enter, Leave, MouseWheel - for mouse events
190 Visibility, Unmap, Map, Expose, FocusIn, FocusOut, Circulate,
191 Colormap, Gravity, Reparent, Property, Destroy, Activate,
192 Deactivate - for window events.
193
194 If a callback function for one of these events is registered
195 using bind, bind_all, bind_class, or tag_bind, the callback is
196 called with an Event as first argument. It will have the
197 following attributes (in braces are the event types for which
198 the attribute is valid):
199
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000200 serial - serial number of event
Fredrik Lundh06d28152000-08-09 18:03:12 +0000201 num - mouse button pressed (ButtonPress, ButtonRelease)
202 focus - whether the window has the focus (Enter, Leave)
203 height - height of the exposed window (Configure, Expose)
204 width - width of the exposed window (Configure, Expose)
205 keycode - keycode of the pressed key (KeyPress, KeyRelease)
206 state - state of the event as a number (ButtonPress, ButtonRelease,
207 Enter, KeyPress, KeyRelease,
208 Leave, Motion)
209 state - state as a string (Visibility)
210 time - when the event occurred
211 x - x-position of the mouse
212 y - y-position of the mouse
213 x_root - x-position of the mouse on the screen
214 (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
215 y_root - y-position of the mouse on the screen
216 (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
217 char - pressed character (KeyPress, KeyRelease)
218 send_event - see X/Windows documentation
Walter Dörwald966c2642005-11-09 17:12:43 +0000219 keysym - keysym of the event as a string (KeyPress, KeyRelease)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000220 keysym_num - keysym of the event as a number (KeyPress, KeyRelease)
221 type - type of the event as a number
222 widget - widget in which the event occurred
223 delta - delta of wheel movement (MouseWheel)
224 """
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300225 def __repr__(self):
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300226 attrs = {k: v for k, v in self.__dict__.items() if v != '??'}
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300227 if not self.char:
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300228 del attrs['char']
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300229 elif self.char != '??':
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300230 attrs['char'] = repr(self.char)
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300231 if not getattr(self, 'send_event', True):
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300232 del attrs['send_event']
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300233 if self.state == 0:
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300234 del attrs['state']
235 elif isinstance(self.state, int):
236 state = self.state
237 mods = ('Shift', 'Lock', 'Control',
238 'Mod1', 'Mod2', 'Mod3', 'Mod4', 'Mod5',
239 'Button1', 'Button2', 'Button3', 'Button4', 'Button5')
240 s = []
241 for i, n in enumerate(mods):
242 if state & (1 << i):
243 s.append(n)
244 state = state & ~((1<< len(mods)) - 1)
245 if state or not s:
246 s.append(hex(state))
247 attrs['state'] = '|'.join(s)
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300248 if self.delta == 0:
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300249 del attrs['delta']
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300250 # widget usually is known
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700251 # serial and time are not very interesting
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300252 # keysym_num duplicates keysym
253 # x_root and y_root mostly duplicate x and y
254 keys = ('send_event',
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300255 'state', 'keysym', 'keycode', 'char',
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300256 'num', 'delta', 'focus',
257 'x', 'y', 'width', 'height')
258 return '<%s event%s>' % (
259 self.type,
Serhiy Storchaka9c6c3fb2016-06-18 21:55:26 +0300260 ''.join(' %s=%s' % (k, attrs[k]) for k in keys if k in attrs)
Serhiy Storchakad611f4c2016-06-12 00:19:44 +0300261 )
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000262
Guido van Rossumc4570481998-03-20 20:45:49 +0000263_support_default_root = 1
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000264_default_root = None
265
Guido van Rossumc4570481998-03-20 20:45:49 +0000266def NoDefaultRoot():
Fredrik Lundh06d28152000-08-09 18:03:12 +0000267 """Inhibit setting of default root window.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000268
Fredrik Lundh06d28152000-08-09 18:03:12 +0000269 Call this function to inhibit that the first instance of
270 Tk is used for windows without an explicit parent window.
271 """
272 global _support_default_root
273 _support_default_root = 0
274 global _default_root
275 _default_root = None
276 del _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +0000277
Guido van Rossum45853db1994-06-20 12:19:19 +0000278def _tkerror(err):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000279 """Internal function."""
280 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000281
Andrew Svetlov806bfad2012-12-10 00:02:31 +0200282def _exit(code=0):
Andrew Svetlov737fb892012-12-18 21:14:22 +0200283 """Internal function. Calling it will raise the exception SystemExit."""
Andrew Svetlov806bfad2012-12-10 00:02:31 +0200284 try:
285 code = int(code)
286 except ValueError:
287 pass
Collin Winterce36ad82007-08-30 01:19:48 +0000288 raise SystemExit(code)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000289
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000290_varnum = 0
291class Variable:
Guido van Rossum2cd0a652003-04-16 20:10:03 +0000292 """Class to define value holders for e.g. buttons.
293
294 Subclasses StringVar, IntVar, DoubleVar, BooleanVar are specializations
295 that constrain the type of the value returned from get()."""
Fredrik Lundh06d28152000-08-09 18:03:12 +0000296 _default = ""
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700297 _tk = None
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300298 _tclCommands = None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299 def __init__(self, master=None, value=None, name=None):
300 """Construct a variable
301
302 MASTER can be given as master widget.
303 VALUE is an optional value (defaults to "")
304 NAME is an optional Tcl name (defaults to PY_VARnum).
305
306 If NAME matches an existing variable and VALUE is omitted
307 then the existing value is retained.
Fredrik Lundh06d28152000-08-09 18:03:12 +0000308 """
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700309 # check for type of NAME parameter to override weird error message
310 # raised from Modules/_tkinter.c:SetVar like:
311 # TypeError: setvar() takes exactly 3 arguments (2 given)
312 if name is not None and not isinstance(name, str):
313 raise TypeError("name must be a string")
Fredrik Lundh06d28152000-08-09 18:03:12 +0000314 global _varnum
315 if not master:
316 master = _default_root
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300317 self._root = master._root()
Fredrik Lundh06d28152000-08-09 18:03:12 +0000318 self._tk = master.tk
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000319 if name:
320 self._name = name
321 else:
322 self._name = 'PY_VAR' + repr(_varnum)
323 _varnum += 1
Benjamin Peterson2a691a82008-03-31 01:51:45 +0000324 if value is not None:
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700325 self.initialize(value)
Serhiy Storchakad97c01f2013-12-26 20:06:05 +0200326 elif not self._tk.getboolean(self._tk.call("info", "exists", self._name)):
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700327 self.initialize(self._default)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000328 def __del__(self):
329 """Unset the variable in Tcl."""
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300330 if self._tk is None:
331 return
332 if self._tk.getboolean(self._tk.call("info", "exists", self._name)):
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700333 self._tk.globalunsetvar(self._name)
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300334 if self._tclCommands is not None:
335 for name in self._tclCommands:
336 #print '- Tkinter: deleted command', name
337 self._tk.deletecommand(name)
338 self._tclCommands = None
Fredrik Lundh06d28152000-08-09 18:03:12 +0000339 def __str__(self):
340 """Return the name of the variable in Tcl."""
341 return self._name
342 def set(self, value):
343 """Set the variable to VALUE."""
344 return self._tk.globalsetvar(self._name, value)
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700345 initialize = set
Guido van Rossum2cd0a652003-04-16 20:10:03 +0000346 def get(self):
347 """Return value of variable."""
348 return self._tk.globalgetvar(self._name)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000349
Serhiy Storchaka81221742016-06-26 09:46:57 +0300350 def _register(self, callback):
351 f = CallWrapper(callback, None, self._root).__call__
Serhiy Storchaka87bbf252014-08-17 15:31:59 +0300352 cbname = repr(id(f))
353 try:
354 callback = callback.__func__
355 except AttributeError:
356 pass
357 try:
358 cbname = cbname + callback.__name__
359 except AttributeError:
360 pass
361 self._tk.createcommand(cbname, f)
362 if self._tclCommands is None:
363 self._tclCommands = []
364 self._tclCommands.append(cbname)
Serhiy Storchaka81221742016-06-26 09:46:57 +0300365 return cbname
366
367 def trace_add(self, mode, callback):
368 """Define a trace callback for the variable.
369
370 Mode is one of "read", "write", "unset", or a list or tuple of
371 such strings.
372 Callback must be a function which is called when the variable is
373 read, written or unset.
374
375 Return the name of the callback.
376 """
377 cbname = self._register(callback)
378 self._tk.call('trace', 'add', 'variable',
379 self._name, mode, (cbname,))
380 return cbname
381
382 def trace_remove(self, mode, cbname):
383 """Delete the trace callback for a variable.
384
385 Mode is one of "read", "write", "unset" or a list or tuple of
386 such strings. Must be same as were specified in trace_add().
387 cbname is the name of the callback returned from trace_add().
388 """
389 self._tk.call('trace', 'remove', 'variable',
390 self._name, mode, cbname)
391 for m, ca in self.trace_info():
392 if self._tk.splitlist(ca)[0] == cbname:
393 break
394 else:
395 self._tk.deletecommand(cbname)
396 try:
397 self._tclCommands.remove(cbname)
398 except ValueError:
399 pass
400
401 def trace_info(self):
402 """Return all trace callback information."""
403 splitlist = self._tk.splitlist
404 return [(splitlist(k), v) for k, v in map(splitlist,
405 splitlist(self._tk.call('trace', 'info', 'variable', self._name)))]
406
407 def trace_variable(self, mode, callback):
408 """Define a trace callback for the variable.
409
410 MODE is one of "r", "w", "u" for read, write, undefine.
411 CALLBACK must be a function which is called when
412 the variable is read, written or undefined.
413
414 Return the name of the callback.
415
416 This deprecated method wraps a deprecated Tcl method that will
417 likely be removed in the future. Use trace_add() instead.
418 """
419 # TODO: Add deprecation warning
420 cbname = self._register(callback)
Serhiy Storchaka26f9feb2014-07-31 07:46:08 +0300421 self._tk.call("trace", "variable", self._name, mode, cbname)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000422 return cbname
Serhiy Storchaka81221742016-06-26 09:46:57 +0300423
Fredrik Lundh06d28152000-08-09 18:03:12 +0000424 trace = trace_variable
Serhiy Storchaka81221742016-06-26 09:46:57 +0300425
Fredrik Lundh06d28152000-08-09 18:03:12 +0000426 def trace_vdelete(self, mode, cbname):
427 """Delete the trace callback for a variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000428
Fredrik Lundh06d28152000-08-09 18:03:12 +0000429 MODE is one of "r", "w", "u" for read, write, undefine.
430 CBNAME is the name of the callback returned from trace_variable or trace.
Serhiy Storchaka81221742016-06-26 09:46:57 +0300431
432 This deprecated method wraps a deprecated Tcl method that will
433 likely be removed in the future. Use trace_remove() instead.
Fredrik Lundh06d28152000-08-09 18:03:12 +0000434 """
Serhiy Storchaka81221742016-06-26 09:46:57 +0300435 # TODO: Add deprecation warning
Serhiy Storchaka26f9feb2014-07-31 07:46:08 +0300436 self._tk.call("trace", "vdelete", self._name, mode, cbname)
Serhiy Storchaka81221742016-06-26 09:46:57 +0300437 cbname = self._tk.splitlist(cbname)[0]
438 for m, ca in self.trace_info():
439 if self._tk.splitlist(ca)[0] == cbname:
440 break
441 else:
442 self._tk.deletecommand(cbname)
443 try:
444 self._tclCommands.remove(cbname)
445 except ValueError:
446 pass
447
Fredrik Lundh06d28152000-08-09 18:03:12 +0000448 def trace_vinfo(self):
Serhiy Storchaka81221742016-06-26 09:46:57 +0300449 """Return all trace callback information.
450
451 This deprecated method wraps a deprecated Tcl method that will
452 likely be removed in the future. Use trace_info() instead.
453 """
454 # TODO: Add deprecation warning
Serhiy Storchaka6bc87b42016-06-26 00:09:19 +0300455 return [self._tk.splitlist(x) for x in self._tk.splitlist(
Serhiy Storchaka26f9feb2014-07-31 07:46:08 +0300456 self._tk.call("trace", "vinfo", self._name))]
Serhiy Storchaka81221742016-06-26 09:46:57 +0300457
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000458 def __eq__(self, other):
459 """Comparison for equality (==).
460
461 Note: if the Variable's master matters to behavior
462 also compare self._master == other._master
463 """
464 return self.__class__.__name__ == other.__class__.__name__ \
465 and self._name == other._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000466
467class StringVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000468 """Value holder for strings variables."""
469 _default = ""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000470 def __init__(self, master=None, value=None, name=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000471 """Construct a string variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000472
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000473 MASTER can be given as master widget.
474 VALUE is an optional value (defaults to "")
475 NAME is an optional Tcl name (defaults to PY_VARnum).
476
477 If NAME matches an existing variable and VALUE is omitted
478 then the existing value is retained.
479 """
480 Variable.__init__(self, master, value, name)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000481
Fredrik Lundh06d28152000-08-09 18:03:12 +0000482 def get(self):
483 """Return value of variable as string."""
Martin v. Löwisbfe175c2003-04-16 19:42:51 +0000484 value = self._tk.globalgetvar(self._name)
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000485 if isinstance(value, str):
Martin v. Löwisbfe175c2003-04-16 19:42:51 +0000486 return value
487 return str(value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000488
489class IntVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000490 """Value holder for integer variables."""
491 _default = 0
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000492 def __init__(self, master=None, value=None, name=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000493 """Construct an integer variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000494
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000495 MASTER can be given as master widget.
496 VALUE is an optional value (defaults to 0)
497 NAME is an optional Tcl name (defaults to PY_VARnum).
498
499 If NAME matches an existing variable and VALUE is omitted
500 then the existing value is retained.
501 """
502 Variable.__init__(self, master, value, name)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000503
Fredrik Lundh06d28152000-08-09 18:03:12 +0000504 def get(self):
505 """Return the value of the variable as an integer."""
Serhiy Storchaka32c0d3a2016-10-30 18:49:52 +0200506 value = self._tk.globalgetvar(self._name)
507 try:
508 return self._tk.getint(value)
509 except (TypeError, TclError):
510 return int(self._tk.getdouble(value))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000511
512class DoubleVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000513 """Value holder for float variables."""
514 _default = 0.0
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000515 def __init__(self, master=None, value=None, name=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000516 """Construct a float variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000517
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000518 MASTER can be given as master widget.
519 VALUE is an optional value (defaults to 0.0)
520 NAME is an optional Tcl name (defaults to PY_VARnum).
521
522 If NAME matches an existing variable and VALUE is omitted
523 then the existing value is retained.
524 """
525 Variable.__init__(self, master, value, name)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000526
527 def get(self):
528 """Return the value of the variable as a float."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300529 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000530
531class BooleanVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000532 """Value holder for boolean variables."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000533 _default = False
534 def __init__(self, master=None, value=None, name=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000535 """Construct a boolean variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000536
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000537 MASTER can be given as master widget.
538 VALUE is an optional value (defaults to False)
539 NAME is an optional Tcl name (defaults to PY_VARnum).
540
541 If NAME matches an existing variable and VALUE is omitted
542 then the existing value is retained.
543 """
544 Variable.__init__(self, master, value, name)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000545
Serhiy Storchaka9a6e2012015-04-04 12:43:01 +0300546 def set(self, value):
547 """Set the variable to VALUE."""
548 return self._tk.globalsetvar(self._name, self._tk.getboolean(value))
549 initialize = set
550
Fredrik Lundh06d28152000-08-09 18:03:12 +0000551 def get(self):
Martin v. Löwisbfe175c2003-04-16 19:42:51 +0000552 """Return the value of the variable as a bool."""
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700553 try:
554 return self._tk.getboolean(self._tk.globalgetvar(self._name))
555 except TclError:
556 raise ValueError("invalid literal for getboolean()")
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000557
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000558def mainloop(n=0):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000559 """Run the main loop of Tcl."""
560 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000561
Guido van Rossum0132f691998-04-30 17:50:36 +0000562getint = int
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000563
Guido van Rossum0132f691998-04-30 17:50:36 +0000564getdouble = float
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000565
566def getboolean(s):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000567 """Convert true and false to integer values 1 and 0."""
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700568 try:
569 return _default_root.tk.getboolean(s)
570 except TclError:
571 raise ValueError("invalid literal for getboolean()")
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000572
Guido van Rossum368e06b1997-11-07 20:38:49 +0000573# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000574class Misc:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000575 """Internal class.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000576
Fredrik Lundh06d28152000-08-09 18:03:12 +0000577 Base class which defines methods common for interior widgets."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000578
Serhiy Storchaka8acb5682016-06-13 09:24:11 +0300579 # used for generating child widget names
580 _last_child_ids = None
581
Fredrik Lundh06d28152000-08-09 18:03:12 +0000582 # XXX font command?
583 _tclCommands = None
584 def destroy(self):
585 """Internal function.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000586
Fredrik Lundh06d28152000-08-09 18:03:12 +0000587 Delete all Tcl commands created for
588 this widget in the Tcl interpreter."""
589 if self._tclCommands is not None:
590 for name in self._tclCommands:
591 #print '- Tkinter: deleted command', name
592 self.tk.deletecommand(name)
593 self._tclCommands = None
594 def deletecommand(self, name):
595 """Internal function.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000596
Fredrik Lundh06d28152000-08-09 18:03:12 +0000597 Delete the Tcl command provided in NAME."""
598 #print '- Tkinter: deleted command', name
599 self.tk.deletecommand(name)
600 try:
601 self._tclCommands.remove(name)
602 except ValueError:
603 pass
604 def tk_strictMotif(self, boolean=None):
605 """Set Tcl internal variable, whether the look and feel
606 should adhere to Motif.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000607
Fredrik Lundh06d28152000-08-09 18:03:12 +0000608 A parameter of 1 means adhere to Motif (e.g. no color
609 change if mouse passes over slider).
610 Returns the set value."""
611 return self.tk.getboolean(self.tk.call(
612 'set', 'tk_strictMotif', boolean))
613 def tk_bisque(self):
614 """Change the color scheme to light brown as used in Tk 3.6 and before."""
615 self.tk.call('tk_bisque')
616 def tk_setPalette(self, *args, **kw):
617 """Set a new color scheme for all widget elements.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000618
Fredrik Lundh06d28152000-08-09 18:03:12 +0000619 A single color as argument will cause that all colors of Tk
620 widget elements are derived from this.
621 Alternatively several keyword parameters and its associated
622 colors can be given. The following keywords are valid:
623 activeBackground, foreground, selectColor,
624 activeForeground, highlightBackground, selectBackground,
625 background, highlightColor, selectForeground,
626 disabledForeground, insertBackground, troughColor."""
627 self.tk.call(('tk_setPalette',)
Serhiy Storchaka4cf4f3a2013-01-02 00:03:58 +0200628 + _flatten(args) + _flatten(list(kw.items())))
Fredrik Lundh06d28152000-08-09 18:03:12 +0000629 def wait_variable(self, name='PY_VAR'):
630 """Wait until the variable is modified.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000631
Fredrik Lundh06d28152000-08-09 18:03:12 +0000632 A parameter of type IntVar, StringVar, DoubleVar or
633 BooleanVar must be given."""
634 self.tk.call('tkwait', 'variable', name)
635 waitvar = wait_variable # XXX b/w compat
636 def wait_window(self, window=None):
637 """Wait until a WIDGET is destroyed.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000638
Fredrik Lundh06d28152000-08-09 18:03:12 +0000639 If no parameter is given self is used."""
Fred Drake132dce22000-12-12 23:11:42 +0000640 if window is None:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000641 window = self
642 self.tk.call('tkwait', 'window', window._w)
643 def wait_visibility(self, window=None):
644 """Wait until the visibility of a WIDGET changes
645 (e.g. it appears).
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000646
Fredrik Lundh06d28152000-08-09 18:03:12 +0000647 If no parameter is given self is used."""
Fred Drake132dce22000-12-12 23:11:42 +0000648 if window is None:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000649 window = self
650 self.tk.call('tkwait', 'visibility', window._w)
651 def setvar(self, name='PY_VAR', value='1'):
652 """Set Tcl variable NAME to VALUE."""
653 self.tk.setvar(name, value)
654 def getvar(self, name='PY_VAR'):
655 """Return value of Tcl variable NAME."""
656 return self.tk.getvar(name)
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300657
658 def getint(self, s):
659 try:
660 return self.tk.getint(s)
661 except TclError as exc:
662 raise ValueError(str(exc))
663
664 def getdouble(self, s):
665 try:
666 return self.tk.getdouble(s)
667 except TclError as exc:
668 raise ValueError(str(exc))
669
Fredrik Lundh06d28152000-08-09 18:03:12 +0000670 def getboolean(self, s):
Neal Norwitz6e5be222003-04-17 13:13:55 +0000671 """Return a boolean value for Tcl boolean values true and false given as parameter."""
Martin v. Löwis2b695a42012-03-12 17:47:35 -0700672 try:
673 return self.tk.getboolean(s)
674 except TclError:
675 raise ValueError("invalid literal for getboolean()")
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300676
Fredrik Lundh06d28152000-08-09 18:03:12 +0000677 def focus_set(self):
678 """Direct input focus to this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000679
Fredrik Lundh06d28152000-08-09 18:03:12 +0000680 If the application currently does not have the focus
681 this widget will get the focus if the application gets
682 the focus through the window manager."""
683 self.tk.call('focus', self._w)
684 focus = focus_set # XXX b/w compat?
685 def focus_force(self):
686 """Direct input focus to this widget even if the
687 application does not have the focus. Use with
688 caution!"""
689 self.tk.call('focus', '-force', self._w)
690 def focus_get(self):
691 """Return the widget which has currently the focus in the
692 application.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000693
Fredrik Lundh06d28152000-08-09 18:03:12 +0000694 Use focus_displayof to allow working with several
695 displays. Return None if application does not have
696 the focus."""
697 name = self.tk.call('focus')
698 if name == 'none' or not name: return None
699 return self._nametowidget(name)
700 def focus_displayof(self):
701 """Return the widget which has currently the focus on the
702 display where this widget is located.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000703
Fredrik Lundh06d28152000-08-09 18:03:12 +0000704 Return None if the application does not have the focus."""
705 name = self.tk.call('focus', '-displayof', self._w)
706 if name == 'none' or not name: return None
707 return self._nametowidget(name)
708 def focus_lastfor(self):
709 """Return the widget which would have the focus if top level
710 for this widget gets the focus from the window manager."""
711 name = self.tk.call('focus', '-lastfor', self._w)
712 if name == 'none' or not name: return None
713 return self._nametowidget(name)
714 def tk_focusFollowsMouse(self):
715 """The widget under mouse will get automatically focus. Can not
716 be disabled easily."""
717 self.tk.call('tk_focusFollowsMouse')
718 def tk_focusNext(self):
719 """Return the next widget in the focus order which follows
720 widget which has currently the focus.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000721
Fredrik Lundh06d28152000-08-09 18:03:12 +0000722 The focus order first goes to the next child, then to
723 the children of the child recursively and then to the
724 next sibling which is higher in the stacking order. A
725 widget is omitted if it has the takefocus resource set
726 to 0."""
727 name = self.tk.call('tk_focusNext', self._w)
728 if not name: return None
729 return self._nametowidget(name)
730 def tk_focusPrev(self):
731 """Return previous widget in the focus order. See tk_focusNext for details."""
732 name = self.tk.call('tk_focusPrev', self._w)
733 if not name: return None
734 return self._nametowidget(name)
735 def after(self, ms, func=None, *args):
736 """Call function once after given time.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000737
Fredrik Lundh06d28152000-08-09 18:03:12 +0000738 MS specifies the time in milliseconds. FUNC gives the
739 function which shall be called. Additional parameters
740 are given as parameters to the function call. Return
741 identifier to cancel scheduling with after_cancel."""
742 if not func:
743 # I'd rather use time.sleep(ms*0.001)
744 self.tk.call('after', ms)
Cheryl Sabella74382a32018-03-04 05:41:47 -0500745 return None
Fredrik Lundh06d28152000-08-09 18:03:12 +0000746 else:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000747 def callit():
Fredrik Lundh06d28152000-08-09 18:03:12 +0000748 try:
Raymond Hettingerff41c482003-04-06 09:01:11 +0000749 func(*args)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000750 finally:
751 try:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000752 self.deletecommand(name)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000753 except TclError:
754 pass
Serhiy Storchakad00aff22014-08-24 09:07:47 +0300755 callit.__name__ = func.__name__
Fredrik Lundh06d28152000-08-09 18:03:12 +0000756 name = self._register(callit)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000757 return self.tk.call('after', ms, name)
758 def after_idle(self, func, *args):
759 """Call FUNC once if the Tcl main loop has no event to
760 process.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000761
Fredrik Lundh06d28152000-08-09 18:03:12 +0000762 Return an identifier to cancel the scheduling with
763 after_cancel."""
Raymond Hettingerff41c482003-04-06 09:01:11 +0000764 return self.after('idle', func, *args)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000765 def after_cancel(self, id):
766 """Cancel scheduling of function identified with ID.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000767
Fredrik Lundh06d28152000-08-09 18:03:12 +0000768 Identifier returned by after or after_idle must be
Cheryl Sabella74382a32018-03-04 05:41:47 -0500769 given as first parameter.
770 """
771 if not id:
772 raise ValueError('id must be a valid identifier returned from '
773 'after or after_idle')
Martin v. Löwis0f9e5252003-06-07 19:52:38 +0000774 try:
Neal Norwitz3c0f2c92003-07-01 21:12:47 +0000775 data = self.tk.call('after', 'info', id)
Neal Norwitz3c0f2c92003-07-01 21:12:47 +0000776 script = self.tk.splitlist(data)[0]
Martin v. Löwis0f9e5252003-06-07 19:52:38 +0000777 self.deletecommand(script)
778 except TclError:
779 pass
Fredrik Lundh06d28152000-08-09 18:03:12 +0000780 self.tk.call('after', 'cancel', id)
781 def bell(self, displayof=0):
782 """Ring a display's bell."""
783 self.tk.call(('bell',) + self._displayof(displayof))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000784
Fredrik Lundh06d28152000-08-09 18:03:12 +0000785 # Clipboard handling:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000786 def clipboard_get(self, **kw):
787 """Retrieve data from the clipboard on window's display.
788
789 The window keyword defaults to the root window of the Tkinter
790 application.
791
792 The type keyword specifies the form in which the data is
793 to be returned and should be an atom name such as STRING
Ned Deily4d377d92012-05-15 18:08:11 -0700794 or FILE_NAME. Type defaults to STRING, except on X11, where the default
795 is to try UTF8_STRING and fall back to STRING.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000796
797 This command is equivalent to:
798
799 selection_get(CLIPBOARD)
800 """
Ned Deily4d377d92012-05-15 18:08:11 -0700801 if 'type' not in kw and self._windowingsystem == 'x11':
802 try:
803 kw['type'] = 'UTF8_STRING'
804 return self.tk.call(('clipboard', 'get') + self._options(kw))
805 except TclError:
806 del kw['type']
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000807 return self.tk.call(('clipboard', 'get') + self._options(kw))
808
Fredrik Lundh06d28152000-08-09 18:03:12 +0000809 def clipboard_clear(self, **kw):
810 """Clear the data in the Tk clipboard.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000811
Fredrik Lundh06d28152000-08-09 18:03:12 +0000812 A widget specified for the optional displayof keyword
813 argument specifies the target display."""
Guido van Rossume014a132006-08-19 16:53:45 +0000814 if 'displayof' not in kw: kw['displayof'] = self._w
Fredrik Lundh06d28152000-08-09 18:03:12 +0000815 self.tk.call(('clipboard', 'clear') + self._options(kw))
816 def clipboard_append(self, string, **kw):
817 """Append STRING to the Tk clipboard.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000818
Fredrik Lundh06d28152000-08-09 18:03:12 +0000819 A widget specified at the optional displayof keyword
820 argument specifies the target display. The clipboard
821 can be retrieved with selection_get."""
Guido van Rossume014a132006-08-19 16:53:45 +0000822 if 'displayof' not in kw: kw['displayof'] = self._w
Fredrik Lundh06d28152000-08-09 18:03:12 +0000823 self.tk.call(('clipboard', 'append') + self._options(kw)
824 + ('--', string))
825 # XXX grab current w/o window argument
826 def grab_current(self):
827 """Return widget which has currently the grab in this application
828 or None."""
829 name = self.tk.call('grab', 'current', self._w)
830 if not name: return None
831 return self._nametowidget(name)
832 def grab_release(self):
833 """Release grab for this widget if currently set."""
834 self.tk.call('grab', 'release', self._w)
835 def grab_set(self):
836 """Set grab for this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000837
Fredrik Lundh06d28152000-08-09 18:03:12 +0000838 A grab directs all events to this and descendant
839 widgets in the application."""
840 self.tk.call('grab', 'set', self._w)
841 def grab_set_global(self):
842 """Set global grab for this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000843
Fredrik Lundh06d28152000-08-09 18:03:12 +0000844 A global grab directs all events to this and
845 descendant widgets on the display. Use with caution -
846 other applications do not get events anymore."""
847 self.tk.call('grab', 'set', '-global', self._w)
848 def grab_status(self):
849 """Return None, "local" or "global" if this widget has
850 no, a local or a global grab."""
851 status = self.tk.call('grab', 'status', self._w)
852 if status == 'none': status = None
853 return status
Fredrik Lundh06d28152000-08-09 18:03:12 +0000854 def option_add(self, pattern, value, priority = None):
855 """Set a VALUE (second parameter) for an option
856 PATTERN (first parameter).
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000857
Fredrik Lundh06d28152000-08-09 18:03:12 +0000858 An optional third parameter gives the numeric priority
859 (defaults to 80)."""
860 self.tk.call('option', 'add', pattern, value, priority)
861 def option_clear(self):
862 """Clear the option database.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000863
Fredrik Lundh06d28152000-08-09 18:03:12 +0000864 It will be reloaded if option_add is called."""
865 self.tk.call('option', 'clear')
866 def option_get(self, name, className):
867 """Return the value for an option NAME for this widget
868 with CLASSNAME.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000869
Fredrik Lundh06d28152000-08-09 18:03:12 +0000870 Values with higher priority override lower values."""
871 return self.tk.call('option', 'get', self._w, name, className)
872 def option_readfile(self, fileName, priority = None):
873 """Read file FILENAME into the option database.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000874
Fredrik Lundh06d28152000-08-09 18:03:12 +0000875 An optional second parameter gives the numeric
876 priority."""
877 self.tk.call('option', 'readfile', fileName, priority)
878 def selection_clear(self, **kw):
879 """Clear the current X selection."""
Guido van Rossume014a132006-08-19 16:53:45 +0000880 if 'displayof' not in kw: kw['displayof'] = self._w
Fredrik Lundh06d28152000-08-09 18:03:12 +0000881 self.tk.call(('selection', 'clear') + self._options(kw))
882 def selection_get(self, **kw):
883 """Return the contents of the current X selection.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000884
Fredrik Lundh06d28152000-08-09 18:03:12 +0000885 A keyword parameter selection specifies the name of
886 the selection and defaults to PRIMARY. A keyword
887 parameter displayof specifies a widget on the display
Ned Deily4d377d92012-05-15 18:08:11 -0700888 to use. A keyword parameter type specifies the form of data to be
889 fetched, defaulting to STRING except on X11, where UTF8_STRING is tried
890 before STRING."""
Guido van Rossume014a132006-08-19 16:53:45 +0000891 if 'displayof' not in kw: kw['displayof'] = self._w
Ned Deily4d377d92012-05-15 18:08:11 -0700892 if 'type' not in kw and self._windowingsystem == 'x11':
893 try:
894 kw['type'] = 'UTF8_STRING'
895 return self.tk.call(('selection', 'get') + self._options(kw))
896 except TclError:
897 del kw['type']
Fredrik Lundh06d28152000-08-09 18:03:12 +0000898 return self.tk.call(('selection', 'get') + self._options(kw))
899 def selection_handle(self, command, **kw):
900 """Specify a function COMMAND to call if the X
901 selection owned by this widget is queried by another
902 application.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000903
Fredrik Lundh06d28152000-08-09 18:03:12 +0000904 This function must return the contents of the
905 selection. The function will be called with the
906 arguments OFFSET and LENGTH which allows the chunking
907 of very long selections. The following keyword
908 parameters can be provided:
909 selection - name of the selection (default PRIMARY),
910 type - type of the selection (e.g. STRING, FILE_NAME)."""
911 name = self._register(command)
912 self.tk.call(('selection', 'handle') + self._options(kw)
913 + (self._w, name))
914 def selection_own(self, **kw):
915 """Become owner of X selection.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000916
Fredrik Lundh06d28152000-08-09 18:03:12 +0000917 A keyword parameter selection specifies the name of
918 the selection (default PRIMARY)."""
919 self.tk.call(('selection', 'own') +
920 self._options(kw) + (self._w,))
921 def selection_own_get(self, **kw):
922 """Return owner of X selection.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000923
Fredrik Lundh06d28152000-08-09 18:03:12 +0000924 The following keyword parameter can
925 be provided:
926 selection - name of the selection (default PRIMARY),
927 type - type of the selection (e.g. STRING, FILE_NAME)."""
Guido van Rossume014a132006-08-19 16:53:45 +0000928 if 'displayof' not in kw: kw['displayof'] = self._w
Fredrik Lundh06d28152000-08-09 18:03:12 +0000929 name = self.tk.call(('selection', 'own') + self._options(kw))
930 if not name: return None
931 return self._nametowidget(name)
932 def send(self, interp, cmd, *args):
933 """Send Tcl command CMD to different interpreter INTERP to be executed."""
934 return self.tk.call(('send', interp, cmd) + args)
935 def lower(self, belowThis=None):
936 """Lower this widget in the stacking order."""
937 self.tk.call('lower', self._w, belowThis)
938 def tkraise(self, aboveThis=None):
939 """Raise this widget in the stacking order."""
940 self.tk.call('raise', self._w, aboveThis)
941 lift = tkraise
Fredrik Lundh06d28152000-08-09 18:03:12 +0000942 def winfo_atom(self, name, displayof=0):
943 """Return integer which represents atom NAME."""
944 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300945 return self.tk.getint(self.tk.call(args))
Fredrik Lundh06d28152000-08-09 18:03:12 +0000946 def winfo_atomname(self, id, displayof=0):
947 """Return name of atom with identifier ID."""
948 args = ('winfo', 'atomname') \
949 + self._displayof(displayof) + (id,)
950 return self.tk.call(args)
951 def winfo_cells(self):
952 """Return number of cells in the colormap for this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300953 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +0000954 self.tk.call('winfo', 'cells', self._w))
955 def winfo_children(self):
956 """Return a list of all widgets which are children of this widget."""
Martin v. Löwisf2041b82002-03-27 17:15:57 +0000957 result = []
958 for child in self.tk.splitlist(
959 self.tk.call('winfo', 'children', self._w)):
960 try:
961 # Tcl sometimes returns extra windows, e.g. for
962 # menus; those need to be skipped
963 result.append(self._nametowidget(child))
964 except KeyError:
965 pass
966 return result
967
Fredrik Lundh06d28152000-08-09 18:03:12 +0000968 def winfo_class(self):
969 """Return window class name of this widget."""
970 return self.tk.call('winfo', 'class', self._w)
971 def winfo_colormapfull(self):
972 """Return true if at the last color request the colormap was full."""
973 return self.tk.getboolean(
974 self.tk.call('winfo', 'colormapfull', self._w))
975 def winfo_containing(self, rootX, rootY, displayof=0):
976 """Return the widget which is at the root coordinates ROOTX, ROOTY."""
977 args = ('winfo', 'containing') \
978 + self._displayof(displayof) + (rootX, rootY)
979 name = self.tk.call(args)
980 if not name: return None
981 return self._nametowidget(name)
982 def winfo_depth(self):
983 """Return the number of bits per pixel."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300984 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
Fredrik Lundh06d28152000-08-09 18:03:12 +0000985 def winfo_exists(self):
986 """Return true if this widget exists."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300987 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +0000988 self.tk.call('winfo', 'exists', self._w))
989 def winfo_fpixels(self, number):
990 """Return the number of pixels for the given distance NUMBER
991 (e.g. "3c") as float."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300992 return self.tk.getdouble(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +0000993 'winfo', 'fpixels', self._w, number))
994 def winfo_geometry(self):
995 """Return geometry string for this widget in the form "widthxheight+X+Y"."""
996 return self.tk.call('winfo', 'geometry', self._w)
997 def winfo_height(self):
998 """Return height of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +0300999 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001000 self.tk.call('winfo', 'height', self._w))
1001 def winfo_id(self):
1002 """Return identifier ID for this widget."""
Serhiy Storchakab9d67242015-11-09 14:43:31 +02001003 return int(self.tk.call('winfo', 'id', self._w), 0)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001004 def winfo_interps(self, displayof=0):
1005 """Return the name of all Tcl interpreters for this display."""
1006 args = ('winfo', 'interps') + self._displayof(displayof)
1007 return self.tk.splitlist(self.tk.call(args))
1008 def winfo_ismapped(self):
1009 """Return true if this widget is mapped."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001010 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001011 self.tk.call('winfo', 'ismapped', self._w))
1012 def winfo_manager(self):
Vijay Kumar128641d2017-05-22 21:53:52 +05301013 """Return the window manager name for this widget."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00001014 return self.tk.call('winfo', 'manager', self._w)
1015 def winfo_name(self):
1016 """Return the name of this widget."""
1017 return self.tk.call('winfo', 'name', self._w)
1018 def winfo_parent(self):
1019 """Return the name of the parent of this widget."""
1020 return self.tk.call('winfo', 'parent', self._w)
1021 def winfo_pathname(self, id, displayof=0):
1022 """Return the pathname of the widget given by ID."""
1023 args = ('winfo', 'pathname') \
1024 + self._displayof(displayof) + (id,)
1025 return self.tk.call(args)
1026 def winfo_pixels(self, number):
1027 """Rounded integer value of winfo_fpixels."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001028 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001029 self.tk.call('winfo', 'pixels', self._w, number))
1030 def winfo_pointerx(self):
1031 """Return the x coordinate of the pointer on the root window."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001032 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001033 self.tk.call('winfo', 'pointerx', self._w))
1034 def winfo_pointerxy(self):
1035 """Return a tuple of x and y coordinates of the pointer on the root window."""
1036 return self._getints(
1037 self.tk.call('winfo', 'pointerxy', self._w))
1038 def winfo_pointery(self):
1039 """Return the y coordinate of the pointer on the root window."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001040 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001041 self.tk.call('winfo', 'pointery', self._w))
1042 def winfo_reqheight(self):
1043 """Return requested height of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001044 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001045 self.tk.call('winfo', 'reqheight', self._w))
1046 def winfo_reqwidth(self):
1047 """Return requested width of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001048 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001049 self.tk.call('winfo', 'reqwidth', self._w))
1050 def winfo_rgb(self, color):
1051 """Return tuple of decimal values for red, green, blue for
1052 COLOR in this widget."""
1053 return self._getints(
1054 self.tk.call('winfo', 'rgb', self._w, color))
1055 def winfo_rootx(self):
1056 """Return x coordinate of upper left corner of this widget on the
1057 root window."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001058 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001059 self.tk.call('winfo', 'rootx', self._w))
1060 def winfo_rooty(self):
1061 """Return y coordinate of upper left corner of this widget on the
1062 root window."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001063 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001064 self.tk.call('winfo', 'rooty', self._w))
1065 def winfo_screen(self):
1066 """Return the screen name of this widget."""
1067 return self.tk.call('winfo', 'screen', self._w)
1068 def winfo_screencells(self):
1069 """Return the number of the cells in the colormap of the screen
1070 of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001071 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001072 self.tk.call('winfo', 'screencells', self._w))
1073 def winfo_screendepth(self):
1074 """Return the number of bits per pixel of the root window of the
1075 screen of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001076 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001077 self.tk.call('winfo', 'screendepth', self._w))
1078 def winfo_screenheight(self):
1079 """Return the number of pixels of the height of the screen of this widget
1080 in pixel."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001081 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001082 self.tk.call('winfo', 'screenheight', self._w))
1083 def winfo_screenmmheight(self):
1084 """Return the number of pixels of the height of the screen of
1085 this widget in mm."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001086 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001087 self.tk.call('winfo', 'screenmmheight', self._w))
1088 def winfo_screenmmwidth(self):
1089 """Return the number of pixels of the width of the screen of
1090 this widget in mm."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001091 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001092 self.tk.call('winfo', 'screenmmwidth', self._w))
1093 def winfo_screenvisual(self):
1094 """Return one of the strings directcolor, grayscale, pseudocolor,
1095 staticcolor, staticgray, or truecolor for the default
1096 colormodel of this screen."""
1097 return self.tk.call('winfo', 'screenvisual', self._w)
1098 def winfo_screenwidth(self):
1099 """Return the number of pixels of the width of the screen of
1100 this widget in pixel."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001101 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001102 self.tk.call('winfo', 'screenwidth', self._w))
1103 def winfo_server(self):
1104 """Return information of the X-Server of the screen of this widget in
1105 the form "XmajorRminor vendor vendorVersion"."""
1106 return self.tk.call('winfo', 'server', self._w)
1107 def winfo_toplevel(self):
1108 """Return the toplevel widget of this widget."""
1109 return self._nametowidget(self.tk.call(
1110 'winfo', 'toplevel', self._w))
1111 def winfo_viewable(self):
1112 """Return true if the widget and all its higher ancestors are mapped."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001113 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001114 self.tk.call('winfo', 'viewable', self._w))
1115 def winfo_visual(self):
1116 """Return one of the strings directcolor, grayscale, pseudocolor,
1117 staticcolor, staticgray, or truecolor for the
1118 colormodel of this widget."""
1119 return self.tk.call('winfo', 'visual', self._w)
1120 def winfo_visualid(self):
1121 """Return the X identifier for the visual for this widget."""
1122 return self.tk.call('winfo', 'visualid', self._w)
Serhiy Storchaka6bc87b42016-06-26 00:09:19 +03001123 def winfo_visualsavailable(self, includeids=False):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001124 """Return a list of all visuals available for the screen
1125 of this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001126
Fredrik Lundh06d28152000-08-09 18:03:12 +00001127 Each item in the list consists of a visual name (see winfo_visual), a
Serhiy Storchaka6bc87b42016-06-26 00:09:19 +03001128 depth and if includeids is true is given also the X identifier."""
1129 data = self.tk.call('winfo', 'visualsavailable', self._w,
1130 'includeids' if includeids else None)
1131 data = [self.tk.splitlist(x) for x in self.tk.splitlist(data)]
1132 return [self.__winfo_parseitem(x) for x in data]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001133 def __winfo_parseitem(self, t):
1134 """Internal function."""
1135 return t[:1] + tuple(map(self.__winfo_getint, t[1:]))
1136 def __winfo_getint(self, x):
1137 """Internal function."""
Eric S. Raymondfc170b12001-02-09 11:51:27 +00001138 return int(x, 0)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001139 def winfo_vrootheight(self):
1140 """Return the height of the virtual root window associated with this
1141 widget in pixels. If there is no virtual root window return the
1142 height 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', 'vrootheight', self._w))
1145 def winfo_vrootwidth(self):
1146 """Return the width of the virtual root window associated with this
1147 widget in pixel. If there is no virtual root window return the
1148 width of the screen."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001149 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001150 self.tk.call('winfo', 'vrootwidth', self._w))
1151 def winfo_vrootx(self):
1152 """Return the x offset of the virtual root relative to the root
1153 window of the screen of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001154 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001155 self.tk.call('winfo', 'vrootx', self._w))
1156 def winfo_vrooty(self):
1157 """Return the y offset of the virtual root relative to the root
1158 window of the screen of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001159 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001160 self.tk.call('winfo', 'vrooty', self._w))
1161 def winfo_width(self):
1162 """Return the width of this widget."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001163 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001164 self.tk.call('winfo', 'width', self._w))
1165 def winfo_x(self):
1166 """Return the x coordinate of the upper left corner of this widget
1167 in the parent."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001168 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001169 self.tk.call('winfo', 'x', self._w))
1170 def winfo_y(self):
1171 """Return the y coordinate of the upper left corner of this widget
1172 in the parent."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001173 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001174 self.tk.call('winfo', 'y', self._w))
1175 def update(self):
1176 """Enter event loop until all pending events have been processed by Tcl."""
1177 self.tk.call('update')
1178 def update_idletasks(self):
1179 """Enter event loop until all idle callbacks have been called. This
1180 will update the display of windows but not process events caused by
1181 the user."""
1182 self.tk.call('update', 'idletasks')
1183 def bindtags(self, tagList=None):
1184 """Set or get the list of bindtags for this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001185
Fredrik Lundh06d28152000-08-09 18:03:12 +00001186 With no argument return the list of all bindtags associated with
1187 this widget. With a list of strings as argument the bindtags are
1188 set to this list. The bindtags determine in which order events are
1189 processed (see bind)."""
1190 if tagList is None:
1191 return self.tk.splitlist(
1192 self.tk.call('bindtags', self._w))
1193 else:
1194 self.tk.call('bindtags', self._w, tagList)
1195 def _bind(self, what, sequence, func, add, needcleanup=1):
1196 """Internal function."""
Guido van Rossum13257902007-06-07 23:15:56 +00001197 if isinstance(func, str):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001198 self.tk.call(what + (sequence, func))
1199 elif func:
1200 funcid = self._register(func, self._substitute,
1201 needcleanup)
1202 cmd = ('%sif {"[%s %s]" == "break"} break\n'
1203 %
1204 (add and '+' or '',
Martin v. Löwisc8718c12001-08-09 16:57:33 +00001205 funcid, self._subst_format_str))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001206 self.tk.call(what + (sequence, cmd))
1207 return funcid
1208 elif sequence:
1209 return self.tk.call(what + (sequence,))
1210 else:
1211 return self.tk.splitlist(self.tk.call(what))
1212 def bind(self, sequence=None, func=None, add=None):
1213 """Bind to this widget at event SEQUENCE a call to function FUNC.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001214
Fredrik Lundh06d28152000-08-09 18:03:12 +00001215 SEQUENCE is a string of concatenated event
1216 patterns. An event pattern is of the form
1217 <MODIFIER-MODIFIER-TYPE-DETAIL> where MODIFIER is one
1218 of Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4,
1219 Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3,
1220 B3, Alt, Button4, B4, Double, Button5, B5 Triple,
1221 Mod1, M1. TYPE is one of Activate, Enter, Map,
1222 ButtonPress, Button, Expose, Motion, ButtonRelease
1223 FocusIn, MouseWheel, Circulate, FocusOut, Property,
1224 Colormap, Gravity Reparent, Configure, KeyPress, Key,
1225 Unmap, Deactivate, KeyRelease Visibility, Destroy,
1226 Leave and DETAIL is the button number for ButtonPress,
1227 ButtonRelease and DETAIL is the Keysym for KeyPress and
1228 KeyRelease. Examples are
1229 <Control-Button-1> for pressing Control and mouse button 1 or
1230 <Alt-A> for pressing A and the Alt key (KeyPress can be omitted).
1231 An event pattern can also be a virtual event of the form
1232 <<AString>> where AString can be arbitrary. This
1233 event can be generated by event_generate.
1234 If events are concatenated they must appear shortly
1235 after each other.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001236
Fredrik Lundh06d28152000-08-09 18:03:12 +00001237 FUNC will be called if the event sequence occurs with an
1238 instance of Event as argument. If the return value of FUNC is
1239 "break" no further bound function is invoked.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001240
Fredrik Lundh06d28152000-08-09 18:03:12 +00001241 An additional boolean parameter ADD specifies whether FUNC will
1242 be called additionally to the other bound function or whether
1243 it will replace the previous function.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001244
Fredrik Lundh06d28152000-08-09 18:03:12 +00001245 Bind will return an identifier to allow deletion of the bound function with
1246 unbind without memory leak.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001247
Fredrik Lundh06d28152000-08-09 18:03:12 +00001248 If FUNC or SEQUENCE is omitted the bound function or list
1249 of bound events are returned."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001250
Fredrik Lundh06d28152000-08-09 18:03:12 +00001251 return self._bind(('bind', self._w), sequence, func, add)
1252 def unbind(self, sequence, funcid=None):
1253 """Unbind for this widget for event SEQUENCE the
1254 function identified with FUNCID."""
1255 self.tk.call('bind', self._w, sequence, '')
1256 if funcid:
1257 self.deletecommand(funcid)
1258 def bind_all(self, sequence=None, func=None, add=None):
1259 """Bind to all widgets at an event SEQUENCE a call to function FUNC.
1260 An additional boolean parameter ADD specifies whether FUNC will
1261 be called additionally to the other bound function or whether
1262 it will replace the previous function. See bind for the return value."""
1263 return self._bind(('bind', 'all'), sequence, func, add, 0)
1264 def unbind_all(self, sequence):
1265 """Unbind for all widgets for event SEQUENCE all functions."""
1266 self.tk.call('bind', 'all' , sequence, '')
1267 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001268
Fredrik Lundh06d28152000-08-09 18:03:12 +00001269 """Bind to widgets with bindtag CLASSNAME at event
1270 SEQUENCE a call of function FUNC. An additional
1271 boolean parameter ADD specifies whether FUNC will be
1272 called additionally to the other bound function or
1273 whether it will replace the previous function. See bind for
1274 the return value."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001275
Fredrik Lundh06d28152000-08-09 18:03:12 +00001276 return self._bind(('bind', className), sequence, func, add, 0)
1277 def unbind_class(self, className, sequence):
Serhiy Storchakad65c9492015-11-02 14:10:23 +02001278 """Unbind for all widgets with bindtag CLASSNAME for event SEQUENCE
Fredrik Lundh06d28152000-08-09 18:03:12 +00001279 all functions."""
1280 self.tk.call('bind', className , sequence, '')
1281 def mainloop(self, n=0):
1282 """Call the mainloop of Tk."""
1283 self.tk.mainloop(n)
1284 def quit(self):
1285 """Quit the Tcl interpreter. All widgets will be destroyed."""
1286 self.tk.quit()
1287 def _getints(self, string):
1288 """Internal function."""
1289 if string:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001290 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001291 def _getdoubles(self, string):
1292 """Internal function."""
1293 if string:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001294 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001295 def _getboolean(self, string):
1296 """Internal function."""
1297 if string:
1298 return self.tk.getboolean(string)
1299 def _displayof(self, displayof):
1300 """Internal function."""
1301 if displayof:
1302 return ('-displayof', displayof)
1303 if displayof is None:
1304 return ('-displayof', self._w)
1305 return ()
Ned Deily4d377d92012-05-15 18:08:11 -07001306 @property
1307 def _windowingsystem(self):
1308 """Internal function."""
1309 try:
1310 return self._root()._windowingsystem_cached
1311 except AttributeError:
1312 ws = self._root()._windowingsystem_cached = \
1313 self.tk.call('tk', 'windowingsystem')
1314 return ws
Fredrik Lundh06d28152000-08-09 18:03:12 +00001315 def _options(self, cnf, kw = None):
1316 """Internal function."""
1317 if kw:
1318 cnf = _cnfmerge((cnf, kw))
1319 else:
1320 cnf = _cnfmerge(cnf)
1321 res = ()
1322 for k, v in cnf.items():
1323 if v is not None:
1324 if k[-1] == '_': k = k[:-1]
Florent Xicluna5d1155c2011-10-28 14:45:05 +02001325 if callable(v):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001326 v = self._register(v)
Georg Brandlbf1eb632008-05-29 07:19:00 +00001327 elif isinstance(v, (tuple, list)):
Georg Brandl3b550032008-06-03 10:25:47 +00001328 nv = []
Georg Brandlbf1eb632008-05-29 07:19:00 +00001329 for item in v:
Georg Brandl3b550032008-06-03 10:25:47 +00001330 if isinstance(item, int):
1331 nv.append(str(item))
1332 elif isinstance(item, str):
Serhiy Storchakab1396522013-01-15 17:56:08 +02001333 nv.append(_stringify(item))
Georg Brandl3b550032008-06-03 10:25:47 +00001334 else:
Georg Brandlbf1eb632008-05-29 07:19:00 +00001335 break
1336 else:
Georg Brandl3b550032008-06-03 10:25:47 +00001337 v = ' '.join(nv)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001338 res = res + ('-'+k, v)
1339 return res
1340 def nametowidget(self, name):
1341 """Return the Tkinter instance of a widget identified by
1342 its Tcl name NAME."""
Martin v. Löwiscdfae162008-08-02 07:23:15 +00001343 name = str(name).split('.')
Fredrik Lundh06d28152000-08-09 18:03:12 +00001344 w = self
Martin v. Löwiscdfae162008-08-02 07:23:15 +00001345
1346 if not name[0]:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001347 w = w._root()
1348 name = name[1:]
Martin v. Löwiscdfae162008-08-02 07:23:15 +00001349
1350 for n in name:
1351 if not n:
1352 break
1353 w = w.children[n]
1354
Fredrik Lundh06d28152000-08-09 18:03:12 +00001355 return w
1356 _nametowidget = nametowidget
1357 def _register(self, func, subst=None, needcleanup=1):
1358 """Return a newly created Tcl function. If this
1359 function is called, the Python function FUNC will
1360 be executed. An optional function SUBST can
1361 be given which will be executed before FUNC."""
1362 f = CallWrapper(func, subst, self).__call__
Walter Dörwald70a6b492004-02-12 17:35:32 +00001363 name = repr(id(f))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001364 try:
Christian Heimesff737952007-11-27 10:40:20 +00001365 func = func.__func__
Fredrik Lundh06d28152000-08-09 18:03:12 +00001366 except AttributeError:
1367 pass
1368 try:
1369 name = name + func.__name__
1370 except AttributeError:
1371 pass
1372 self.tk.createcommand(name, f)
1373 if needcleanup:
1374 if self._tclCommands is None:
1375 self._tclCommands = []
1376 self._tclCommands.append(name)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001377 return name
1378 register = _register
1379 def _root(self):
1380 """Internal function."""
1381 w = self
1382 while w.master: w = w.master
1383 return w
1384 _subst_format = ('%#', '%b', '%f', '%h', '%k',
1385 '%s', '%t', '%w', '%x', '%y',
1386 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y', '%D')
Martin v. Löwisc8718c12001-08-09 16:57:33 +00001387 _subst_format_str = " ".join(_subst_format)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001388 def _substitute(self, *args):
1389 """Internal function."""
1390 if len(args) != len(self._subst_format): return args
1391 getboolean = self.tk.getboolean
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001392
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001393 getint = self.tk.getint
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001394 def getint_event(s):
1395 """Tk changed behavior in 8.4.2, returning "??" rather more often."""
1396 try:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001397 return getint(s)
1398 except (ValueError, TclError):
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001399 return s
1400
Fredrik Lundh06d28152000-08-09 18:03:12 +00001401 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y, D = args
1402 # Missing: (a, c, d, m, o, v, B, R)
1403 e = Event()
Martin Panterf05641642016-05-08 13:48:10 +00001404 # serial field: valid for all events
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001405 # number of button: ButtonPress and ButtonRelease events only
1406 # height field: Configure, ConfigureRequest, Create,
1407 # ResizeRequest, and Expose events only
1408 # keycode field: KeyPress and KeyRelease events only
1409 # time field: "valid for events that contain a time field"
1410 # width field: Configure, ConfigureRequest, Create, ResizeRequest,
1411 # and Expose events only
Martin Panter4c359642016-05-08 13:53:41 +00001412 # x field: "valid for events that contain an x field"
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001413 # y field: "valid for events that contain a y field"
1414 # keysym as decimal: KeyPress and KeyRelease events only
1415 # x_root, y_root fields: ButtonPress, ButtonRelease, KeyPress,
Martin Panterf05641642016-05-08 13:48:10 +00001416 # KeyRelease, and Motion events
Fredrik Lundh06d28152000-08-09 18:03:12 +00001417 e.serial = getint(nsign)
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001418 e.num = getint_event(b)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001419 try: e.focus = getboolean(f)
1420 except TclError: pass
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001421 e.height = getint_event(h)
1422 e.keycode = getint_event(k)
1423 e.state = getint_event(s)
1424 e.time = getint_event(t)
1425 e.width = getint_event(w)
1426 e.x = getint_event(x)
1427 e.y = getint_event(y)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001428 e.char = A
1429 try: e.send_event = getboolean(E)
1430 except TclError: pass
1431 e.keysym = K
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001432 e.keysym_num = getint_event(N)
Serhiy Storchakad611f4c2016-06-12 00:19:44 +03001433 try:
1434 e.type = EventType(T)
1435 except ValueError:
1436 e.type = T
Fredrik Lundh06d28152000-08-09 18:03:12 +00001437 try:
1438 e.widget = self._nametowidget(W)
1439 except KeyError:
1440 e.widget = W
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001441 e.x_root = getint_event(X)
1442 e.y_root = getint_event(Y)
Fredrik Lundha249f162000-09-07 15:05:09 +00001443 try:
1444 e.delta = getint(D)
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001445 except (ValueError, TclError):
Fredrik Lundha249f162000-09-07 15:05:09 +00001446 e.delta = 0
Fredrik Lundh06d28152000-08-09 18:03:12 +00001447 return (e,)
1448 def _report_exception(self):
1449 """Internal function."""
Neal Norwitzac3625f2006-03-17 05:49:33 +00001450 exc, val, tb = sys.exc_info()
Fredrik Lundh06d28152000-08-09 18:03:12 +00001451 root = self._root()
1452 root.report_callback_exception(exc, val, tb)
Serhiy Storchaka848972c2013-12-25 16:35:38 +02001453
1454 def _getconfigure(self, *args):
1455 """Call Tcl configure command and return the result as a dict."""
1456 cnf = {}
1457 for x in self.tk.splitlist(self.tk.call(*args)):
1458 x = self.tk.splitlist(x)
1459 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1460 return cnf
1461
1462 def _getconfigure1(self, *args):
1463 x = self.tk.splitlist(self.tk.call(*args))
1464 return (x[0][1:],) + x[1:]
1465
Martin v. Löwis6ce13152002-10-10 14:36:13 +00001466 def _configure(self, cmd, cnf, kw):
1467 """Internal function."""
1468 if kw:
1469 cnf = _cnfmerge((cnf, kw))
1470 elif cnf:
1471 cnf = _cnfmerge(cnf)
1472 if cnf is None:
Serhiy Storchaka848972c2013-12-25 16:35:38 +02001473 return self._getconfigure(_flatten((self._w, cmd)))
Guido van Rossum13257902007-06-07 23:15:56 +00001474 if isinstance(cnf, str):
Serhiy Storchaka848972c2013-12-25 16:35:38 +02001475 return self._getconfigure1(_flatten((self._w, cmd, '-'+cnf)))
Martin v. Löwis6ce13152002-10-10 14:36:13 +00001476 self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001477 # These used to be defined in Widget:
1478 def configure(self, cnf=None, **kw):
1479 """Configure resources of a widget.
Barry Warsaw107e6231998-12-15 00:44:15 +00001480
Fredrik Lundh06d28152000-08-09 18:03:12 +00001481 The values for resources are specified as keyword
1482 arguments. To get an overview about
1483 the allowed keyword arguments call the method keys.
1484 """
Martin v. Löwis6ce13152002-10-10 14:36:13 +00001485 return self._configure('configure', cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001486 config = configure
1487 def cget(self, key):
1488 """Return the resource value for a KEY given as string."""
1489 return self.tk.call(self._w, 'cget', '-' + key)
1490 __getitem__ = cget
1491 def __setitem__(self, key, value):
1492 self.configure({key: value})
1493 def keys(self):
1494 """Return a list of all resource names of this widget."""
Serhiy Storchaka42a43662016-03-09 10:33:51 +02001495 splitlist = self.tk.splitlist
1496 return [splitlist(x)[0][1:] for x in
1497 splitlist(self.tk.call(self._w, 'configure'))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001498 def __str__(self):
1499 """Return the window path name of this widget."""
1500 return self._w
Serhiy Storchakabcc17462014-04-04 15:45:02 +03001501
1502 def __repr__(self):
1503 return '<%s.%s object %s>' % (
1504 self.__class__.__module__, self.__class__.__qualname__, self._w)
1505
Fredrik Lundh06d28152000-08-09 18:03:12 +00001506 # Pack methods that apply to the master
1507 _noarg_ = ['_noarg_']
1508 def pack_propagate(self, flag=_noarg_):
1509 """Set or get the status for propagation of geometry information.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001510
Fredrik Lundh06d28152000-08-09 18:03:12 +00001511 A boolean argument specifies whether the geometry information
1512 of the slaves will determine the size of this widget. If no argument
1513 is given the current setting will be returned.
1514 """
1515 if flag is Misc._noarg_:
1516 return self._getboolean(self.tk.call(
1517 'pack', 'propagate', self._w))
1518 else:
1519 self.tk.call('pack', 'propagate', self._w, flag)
1520 propagate = pack_propagate
1521 def pack_slaves(self):
1522 """Return a list of all slaves of this widget
1523 in its packing order."""
Alexander Belopolsky022f0492010-11-22 19:40:51 +00001524 return [self._nametowidget(x) for x in
1525 self.tk.splitlist(
1526 self.tk.call('pack', 'slaves', self._w))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001527 slaves = pack_slaves
1528 # Place method that applies to the master
1529 def place_slaves(self):
1530 """Return a list of all slaves of this widget
1531 in its packing order."""
Alexander Belopolsky022f0492010-11-22 19:40:51 +00001532 return [self._nametowidget(x) for x in
1533 self.tk.splitlist(
Fredrik Lundh06d28152000-08-09 18:03:12 +00001534 self.tk.call(
Alexander Belopolsky022f0492010-11-22 19:40:51 +00001535 'place', 'slaves', self._w))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001536 # Grid methods that apply to the master
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001537 def grid_anchor(self, anchor=None): # new in Tk 8.5
1538 """The anchor value controls how to place the grid within the
1539 master when no row/column has any weight.
1540
1541 The default anchor is nw."""
1542 self.tk.call('grid', 'anchor', self._w, anchor)
1543 anchor = grid_anchor
Fredrik Lundh06d28152000-08-09 18:03:12 +00001544 def grid_bbox(self, column=None, row=None, col2=None, row2=None):
1545 """Return a tuple of integer coordinates for the bounding
1546 box of this widget controlled by the geometry manager grid.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001547
Fredrik Lundh06d28152000-08-09 18:03:12 +00001548 If COLUMN, ROW is given the bounding box applies from
1549 the cell with row and column 0 to the specified
1550 cell. If COL2 and ROW2 are given the bounding box
1551 starts at that cell.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001552
Fredrik Lundh06d28152000-08-09 18:03:12 +00001553 The returned integers specify the offset of the upper left
1554 corner in the master widget and the width and height.
1555 """
1556 args = ('grid', 'bbox', self._w)
1557 if column is not None and row is not None:
1558 args = args + (column, row)
1559 if col2 is not None and row2 is not None:
1560 args = args + (col2, row2)
Raymond Hettingerff41c482003-04-06 09:01:11 +00001561 return self._getints(self.tk.call(*args)) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00001562 bbox = grid_bbox
Serhiy Storchakae9597762014-02-19 18:34:05 +02001563
1564 def _gridconvvalue(self, value):
1565 if isinstance(value, (str, _tkinter.Tcl_Obj)):
1566 try:
1567 svalue = str(value)
1568 if not svalue:
1569 return None
1570 elif '.' in svalue:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001571 return self.tk.getdouble(svalue)
Serhiy Storchakae9597762014-02-19 18:34:05 +02001572 else:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03001573 return self.tk.getint(svalue)
1574 except (ValueError, TclError):
Serhiy Storchakae9597762014-02-19 18:34:05 +02001575 pass
1576 return value
1577
Fredrik Lundh06d28152000-08-09 18:03:12 +00001578 def _grid_configure(self, command, index, cnf, kw):
1579 """Internal function."""
Guido van Rossum13257902007-06-07 23:15:56 +00001580 if isinstance(cnf, str) and not kw:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001581 if cnf[-1:] == '_':
1582 cnf = cnf[:-1]
1583 if cnf[:1] != '-':
1584 cnf = '-'+cnf
1585 options = (cnf,)
1586 else:
1587 options = self._options(cnf, kw)
1588 if not options:
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +03001589 return _splitdict(
1590 self.tk,
1591 self.tk.call('grid', command, self._w, index),
1592 conv=self._gridconvvalue)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001593 res = self.tk.call(
1594 ('grid', command, self._w, index)
1595 + options)
1596 if len(options) == 1:
Serhiy Storchakae9597762014-02-19 18:34:05 +02001597 return self._gridconvvalue(res)
1598
Fredrik Lundh06d28152000-08-09 18:03:12 +00001599 def grid_columnconfigure(self, index, cnf={}, **kw):
1600 """Configure column INDEX of a grid.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001601
Fredrik Lundh06d28152000-08-09 18:03:12 +00001602 Valid resources are minsize (minimum size of the column),
1603 weight (how much does additional space propagate to this column)
1604 and pad (how much space to let additionally)."""
1605 return self._grid_configure('columnconfigure', index, cnf, kw)
1606 columnconfigure = grid_columnconfigure
Martin v. Löwisdc579092001-10-13 09:33:51 +00001607 def grid_location(self, x, y):
1608 """Return a tuple of column and row which identify the cell
1609 at which the pixel at position X and Y inside the master
1610 widget is located."""
1611 return self._getints(
1612 self.tk.call(
1613 'grid', 'location', self._w, x, y)) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00001614 def grid_propagate(self, flag=_noarg_):
1615 """Set or get the status for propagation of geometry information.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001616
Fredrik Lundh06d28152000-08-09 18:03:12 +00001617 A boolean argument specifies whether the geometry information
1618 of the slaves will determine the size of this widget. If no argument
1619 is given, the current setting will be returned.
1620 """
1621 if flag is Misc._noarg_:
1622 return self._getboolean(self.tk.call(
1623 'grid', 'propagate', self._w))
1624 else:
1625 self.tk.call('grid', 'propagate', self._w, flag)
1626 def grid_rowconfigure(self, index, cnf={}, **kw):
1627 """Configure row INDEX of a grid.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001628
Fredrik Lundh06d28152000-08-09 18:03:12 +00001629 Valid resources are minsize (minimum size of the row),
1630 weight (how much does additional space propagate to this row)
1631 and pad (how much space to let additionally)."""
1632 return self._grid_configure('rowconfigure', index, cnf, kw)
1633 rowconfigure = grid_rowconfigure
1634 def grid_size(self):
1635 """Return a tuple of the number of column and rows in the grid."""
1636 return self._getints(
1637 self.tk.call('grid', 'size', self._w)) or None
1638 size = grid_size
1639 def grid_slaves(self, row=None, column=None):
1640 """Return a list of all slaves of this widget
1641 in its packing order."""
1642 args = ()
1643 if row is not None:
1644 args = args + ('-row', row)
1645 if column is not None:
1646 args = args + ('-column', column)
Alexander Belopolsky022f0492010-11-22 19:40:51 +00001647 return [self._nametowidget(x) for x in
1648 self.tk.splitlist(self.tk.call(
1649 ('grid', 'slaves', self._w) + args))]
Guido van Rossum80f8be81997-12-02 19:51:39 +00001650
Fredrik Lundh06d28152000-08-09 18:03:12 +00001651 # Support for the "event" command, new in Tk 4.2.
1652 # By Case Roole.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001653
Fredrik Lundh06d28152000-08-09 18:03:12 +00001654 def event_add(self, virtual, *sequences):
1655 """Bind a virtual event VIRTUAL (of the form <<Name>>)
1656 to an event SEQUENCE such that the virtual event is triggered
1657 whenever SEQUENCE occurs."""
1658 args = ('event', 'add', virtual) + sequences
1659 self.tk.call(args)
Guido van Rossumc2966511998-04-10 19:16:10 +00001660
Fredrik Lundh06d28152000-08-09 18:03:12 +00001661 def event_delete(self, virtual, *sequences):
1662 """Unbind a virtual event VIRTUAL from SEQUENCE."""
1663 args = ('event', 'delete', virtual) + sequences
1664 self.tk.call(args)
Guido van Rossumc2966511998-04-10 19:16:10 +00001665
Fredrik Lundh06d28152000-08-09 18:03:12 +00001666 def event_generate(self, sequence, **kw):
1667 """Generate an event SEQUENCE. Additional
1668 keyword arguments specify parameter of the event
1669 (e.g. x, y, rootx, rooty)."""
1670 args = ('event', 'generate', self._w, sequence)
1671 for k, v in kw.items():
1672 args = args + ('-%s' % k, str(v))
1673 self.tk.call(args)
1674
1675 def event_info(self, virtual=None):
1676 """Return a list of all virtual events or the information
1677 about the SEQUENCE bound to the virtual event VIRTUAL."""
1678 return self.tk.splitlist(
1679 self.tk.call('event', 'info', virtual))
1680
1681 # Image related commands
1682
1683 def image_names(self):
1684 """Return a list of all existing image names."""
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02001685 return self.tk.splitlist(self.tk.call('image', 'names'))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001686
1687 def image_types(self):
Vijay Kumar128641d2017-05-22 21:53:52 +05301688 """Return a list of all available image types (e.g. photo bitmap)."""
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02001689 return self.tk.splitlist(self.tk.call('image', 'types'))
Guido van Rossumc2966511998-04-10 19:16:10 +00001690
Guido van Rossum80f8be81997-12-02 19:51:39 +00001691
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001692class CallWrapper:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001693 """Internal class. Stores function to call when some user
1694 defined Tcl function is called e.g. after an event occurred."""
1695 def __init__(self, func, subst, widget):
1696 """Store FUNC, SUBST and WIDGET as members."""
1697 self.func = func
1698 self.subst = subst
1699 self.widget = widget
1700 def __call__(self, *args):
1701 """Apply first function SUBST to arguments, than FUNC."""
1702 try:
1703 if self.subst:
Raymond Hettingerff41c482003-04-06 09:01:11 +00001704 args = self.subst(*args)
1705 return self.func(*args)
Andrew Svetloveb0abce2012-12-03 16:13:07 +02001706 except SystemExit:
1707 raise
Fredrik Lundh06d28152000-08-09 18:03:12 +00001708 except:
1709 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +00001710
Guido van Rossume365a591998-05-01 19:48:20 +00001711
Guilherme Polo1fff0082009-08-14 15:05:30 +00001712class XView:
1713 """Mix-in class for querying and changing the horizontal position
1714 of a widget's window."""
1715
1716 def xview(self, *args):
1717 """Query and change the horizontal position of the view."""
1718 res = self.tk.call(self._w, 'xview', *args)
1719 if not args:
1720 return self._getdoubles(res)
1721
1722 def xview_moveto(self, fraction):
1723 """Adjusts the view in the window so that FRACTION of the
1724 total width of the canvas is off-screen to the left."""
1725 self.tk.call(self._w, 'xview', 'moveto', fraction)
1726
1727 def xview_scroll(self, number, what):
1728 """Shift the x-view according to NUMBER which is measured in "units"
1729 or "pages" (WHAT)."""
1730 self.tk.call(self._w, 'xview', 'scroll', number, what)
1731
1732
1733class YView:
1734 """Mix-in class for querying and changing the vertical position
1735 of a widget's window."""
1736
1737 def yview(self, *args):
1738 """Query and change the vertical position of the view."""
1739 res = self.tk.call(self._w, 'yview', *args)
1740 if not args:
1741 return self._getdoubles(res)
1742
1743 def yview_moveto(self, fraction):
1744 """Adjusts the view in the window so that FRACTION of the
1745 total height of the canvas is off-screen to the top."""
1746 self.tk.call(self._w, 'yview', 'moveto', fraction)
1747
1748 def yview_scroll(self, number, what):
1749 """Shift the y-view according to NUMBER which is measured in
1750 "units" or "pages" (WHAT)."""
1751 self.tk.call(self._w, 'yview', 'scroll', number, what)
1752
1753
Guido van Rossum18468821994-06-20 07:49:28 +00001754class Wm:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001755 """Provides functions for the communication with the window manager."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00001756
Fredrik Lundh06d28152000-08-09 18:03:12 +00001757 def wm_aspect(self,
1758 minNumer=None, minDenom=None,
1759 maxNumer=None, maxDenom=None):
1760 """Instruct the window manager to set the aspect ratio (width/height)
1761 of this widget to be between MINNUMER/MINDENOM and MAXNUMER/MAXDENOM. Return a tuple
1762 of the actual values if no argument is given."""
1763 return self._getints(
1764 self.tk.call('wm', 'aspect', self._w,
1765 minNumer, minDenom,
1766 maxNumer, maxDenom))
1767 aspect = wm_aspect
Raymond Hettingerff41c482003-04-06 09:01:11 +00001768
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001769 def wm_attributes(self, *args):
1770 """This subcommand returns or sets platform specific attributes
Raymond Hettingerff41c482003-04-06 09:01:11 +00001771
1772 The first form returns a list of the platform specific flags and
1773 their values. The second form returns the value for the specific
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001774 option. The third form sets one or more of the values. The values
1775 are as follows:
Raymond Hettingerff41c482003-04-06 09:01:11 +00001776
1777 On Windows, -disabled gets or sets whether the window is in a
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001778 disabled state. -toolwindow gets or sets the style of the window
Raymond Hettingerff41c482003-04-06 09:01:11 +00001779 to toolwindow (as defined in the MSDN). -topmost gets or sets
1780 whether this is a topmost window (displays above all other
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001781 windows).
Raymond Hettingerff41c482003-04-06 09:01:11 +00001782
1783 On Macintosh, XXXXX
1784
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001785 On Unix, there are currently no special attribute values.
1786 """
1787 args = ('wm', 'attributes', self._w) + args
1788 return self.tk.call(args)
1789 attributes=wm_attributes
Raymond Hettingerff41c482003-04-06 09:01:11 +00001790
Fredrik Lundh06d28152000-08-09 18:03:12 +00001791 def wm_client(self, name=None):
1792 """Store NAME in WM_CLIENT_MACHINE property of this widget. Return
1793 current value."""
1794 return self.tk.call('wm', 'client', self._w, name)
1795 client = wm_client
1796 def wm_colormapwindows(self, *wlist):
1797 """Store list of window names (WLIST) into WM_COLORMAPWINDOWS property
1798 of this widget. This list contains windows whose colormaps differ from their
1799 parents. Return current list of widgets if WLIST is empty."""
1800 if len(wlist) > 1:
1801 wlist = (wlist,) # Tk needs a list of windows here
1802 args = ('wm', 'colormapwindows', self._w) + wlist
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02001803 if wlist:
1804 self.tk.call(args)
1805 else:
1806 return [self._nametowidget(x)
1807 for x in self.tk.splitlist(self.tk.call(args))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00001808 colormapwindows = wm_colormapwindows
1809 def wm_command(self, value=None):
1810 """Store VALUE in WM_COMMAND property. It is the command
1811 which shall be used to invoke the application. Return current
1812 command if VALUE is None."""
1813 return self.tk.call('wm', 'command', self._w, value)
1814 command = wm_command
1815 def wm_deiconify(self):
1816 """Deiconify this widget. If it was never mapped it will not be mapped.
1817 On Windows it will raise this widget and give it the focus."""
1818 return self.tk.call('wm', 'deiconify', self._w)
1819 deiconify = wm_deiconify
1820 def wm_focusmodel(self, model=None):
1821 """Set focus model to MODEL. "active" means that this widget will claim
1822 the focus itself, "passive" means that the window manager shall give
1823 the focus. Return current focus model if MODEL is None."""
1824 return self.tk.call('wm', 'focusmodel', self._w, model)
1825 focusmodel = wm_focusmodel
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001826 def wm_forget(self, window): # new in Tk 8.5
Vijay Kumar128641d2017-05-22 21:53:52 +05301827 """The window will be unmapped from the screen and will no longer
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001828 be managed by wm. toplevel windows will be treated like frame
1829 windows once they are no longer managed by wm, however, the menu
1830 option configuration will be remembered and the menus will return
1831 once the widget is managed again."""
1832 self.tk.call('wm', 'forget', window)
1833 forget = wm_forget
Fredrik Lundh06d28152000-08-09 18:03:12 +00001834 def wm_frame(self):
1835 """Return identifier for decorative frame of this widget if present."""
1836 return self.tk.call('wm', 'frame', self._w)
1837 frame = wm_frame
1838 def wm_geometry(self, newGeometry=None):
1839 """Set geometry to NEWGEOMETRY of the form =widthxheight+x+y. Return
1840 current value if None is given."""
1841 return self.tk.call('wm', 'geometry', self._w, newGeometry)
1842 geometry = wm_geometry
1843 def wm_grid(self,
1844 baseWidth=None, baseHeight=None,
1845 widthInc=None, heightInc=None):
1846 """Instruct the window manager that this widget shall only be
1847 resized on grid boundaries. WIDTHINC and HEIGHTINC are the width and
1848 height of a grid unit in pixels. BASEWIDTH and BASEHEIGHT are the
1849 number of grid units requested in Tk_GeometryRequest."""
1850 return self._getints(self.tk.call(
1851 'wm', 'grid', self._w,
1852 baseWidth, baseHeight, widthInc, heightInc))
1853 grid = wm_grid
1854 def wm_group(self, pathName=None):
1855 """Set the group leader widgets for related widgets to PATHNAME. Return
1856 the group leader of this widget if None is given."""
1857 return self.tk.call('wm', 'group', self._w, pathName)
1858 group = wm_group
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001859 def wm_iconbitmap(self, bitmap=None, default=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001860 """Set bitmap for the iconified widget to BITMAP. Return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001861 the bitmap if None is given.
1862
1863 Under Windows, the DEFAULT parameter can be used to set the icon
1864 for the widget and any descendents that don't have an icon set
1865 explicitly. DEFAULT can be the relative path to a .ico file
1866 (example: root.iconbitmap(default='myicon.ico') ). See Tk
1867 documentation for more information."""
1868 if default:
1869 return self.tk.call('wm', 'iconbitmap', self._w, '-default', default)
1870 else:
1871 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001872 iconbitmap = wm_iconbitmap
1873 def wm_iconify(self):
1874 """Display widget as icon."""
1875 return self.tk.call('wm', 'iconify', self._w)
1876 iconify = wm_iconify
1877 def wm_iconmask(self, bitmap=None):
1878 """Set mask for the icon bitmap of this widget. Return the
1879 mask if None is given."""
1880 return self.tk.call('wm', 'iconmask', self._w, bitmap)
1881 iconmask = wm_iconmask
1882 def wm_iconname(self, newName=None):
1883 """Set the name of the icon for this widget. Return the name if
1884 None is given."""
1885 return self.tk.call('wm', 'iconname', self._w, newName)
1886 iconname = wm_iconname
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001887 def wm_iconphoto(self, default=False, *args): # new in Tk 8.5
1888 """Sets the titlebar icon for this window based on the named photo
1889 images passed through args. If default is True, this is applied to
1890 all future created toplevels as well.
1891
1892 The data in the images is taken as a snapshot at the time of
1893 invocation. If the images are later changed, this is not reflected
1894 to the titlebar icons. Multiple images are accepted to allow
1895 different images sizes to be provided. The window manager may scale
1896 provided icons to an appropriate size.
1897
1898 On Windows, the images are packed into a Windows icon structure.
1899 This will override an icon specified to wm_iconbitmap, and vice
1900 versa.
1901
1902 On X, the images are arranged into the _NET_WM_ICON X property,
1903 which most modern window managers support. An icon specified by
Berker Peksag4882cac2015-04-14 09:30:01 +03001904 wm_iconbitmap may exist simultaneously.
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001905
1906 On Macintosh, this currently does nothing."""
1907 if default:
1908 self.tk.call('wm', 'iconphoto', self._w, "-default", *args)
1909 else:
1910 self.tk.call('wm', 'iconphoto', self._w, *args)
1911 iconphoto = wm_iconphoto
Fredrik Lundh06d28152000-08-09 18:03:12 +00001912 def wm_iconposition(self, x=None, y=None):
1913 """Set the position of the icon of this widget to X and Y. Return
1914 a tuple of the current values of X and X if None is given."""
1915 return self._getints(self.tk.call(
1916 'wm', 'iconposition', self._w, x, y))
1917 iconposition = wm_iconposition
1918 def wm_iconwindow(self, pathName=None):
1919 """Set widget PATHNAME to be displayed instead of icon. Return the current
1920 value if None is given."""
1921 return self.tk.call('wm', 'iconwindow', self._w, pathName)
1922 iconwindow = wm_iconwindow
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07001923 def wm_manage(self, widget): # new in Tk 8.5
1924 """The widget specified will become a stand alone top-level window.
1925 The window will be decorated with the window managers title bar,
1926 etc."""
1927 self.tk.call('wm', 'manage', widget)
1928 manage = wm_manage
Fredrik Lundh06d28152000-08-09 18:03:12 +00001929 def wm_maxsize(self, width=None, height=None):
1930 """Set max WIDTH and HEIGHT for this widget. If the window is gridded
1931 the values are given in grid units. Return the current values if None
1932 is given."""
1933 return self._getints(self.tk.call(
1934 'wm', 'maxsize', self._w, width, height))
1935 maxsize = wm_maxsize
1936 def wm_minsize(self, width=None, height=None):
1937 """Set min WIDTH and HEIGHT for this widget. If the window is gridded
1938 the values are given in grid units. Return the current values if None
1939 is given."""
1940 return self._getints(self.tk.call(
1941 'wm', 'minsize', self._w, width, height))
1942 minsize = wm_minsize
1943 def wm_overrideredirect(self, boolean=None):
1944 """Instruct the window manager to ignore this widget
1945 if BOOLEAN is given with 1. Return the current value if None
1946 is given."""
1947 return self._getboolean(self.tk.call(
1948 'wm', 'overrideredirect', self._w, boolean))
1949 overrideredirect = wm_overrideredirect
1950 def wm_positionfrom(self, who=None):
1951 """Instruct the window manager that the position of this widget shall
1952 be defined by the user if WHO is "user", and by its own policy if WHO is
1953 "program"."""
1954 return self.tk.call('wm', 'positionfrom', self._w, who)
1955 positionfrom = wm_positionfrom
1956 def wm_protocol(self, name=None, func=None):
1957 """Bind function FUNC to command NAME for this widget.
1958 Return the function bound to NAME if None is given. NAME could be
1959 e.g. "WM_SAVE_YOURSELF" or "WM_DELETE_WINDOW"."""
Florent Xicluna5d1155c2011-10-28 14:45:05 +02001960 if callable(func):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001961 command = self._register(func)
1962 else:
1963 command = func
1964 return self.tk.call(
1965 'wm', 'protocol', self._w, name, command)
1966 protocol = wm_protocol
1967 def wm_resizable(self, width=None, height=None):
1968 """Instruct the window manager whether this width can be resized
1969 in WIDTH or HEIGHT. Both values are boolean values."""
1970 return self.tk.call('wm', 'resizable', self._w, width, height)
1971 resizable = wm_resizable
1972 def wm_sizefrom(self, who=None):
1973 """Instruct the window manager that the size of this widget shall
1974 be defined by the user if WHO is "user", and by its own policy if WHO is
1975 "program"."""
1976 return self.tk.call('wm', 'sizefrom', self._w, who)
1977 sizefrom = wm_sizefrom
Fredrik Lundh289ad8f2000-08-09 19:11:59 +00001978 def wm_state(self, newstate=None):
1979 """Query or set the state of this widget as one of normal, icon,
1980 iconic (see wm_iconwindow), withdrawn, or zoomed (Windows only)."""
1981 return self.tk.call('wm', 'state', self._w, newstate)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001982 state = wm_state
1983 def wm_title(self, string=None):
1984 """Set the title of this widget."""
1985 return self.tk.call('wm', 'title', self._w, string)
1986 title = wm_title
1987 def wm_transient(self, master=None):
1988 """Instruct the window manager that this widget is transient
1989 with regard to widget MASTER."""
1990 return self.tk.call('wm', 'transient', self._w, master)
1991 transient = wm_transient
1992 def wm_withdraw(self):
1993 """Withdraw this widget from the screen such that it is unmapped
1994 and forgotten by the window manager. Re-draw it with wm_deiconify."""
1995 return self.tk.call('wm', 'withdraw', self._w)
1996 withdraw = wm_withdraw
Guido van Rossume365a591998-05-01 19:48:20 +00001997
Guido van Rossum18468821994-06-20 07:49:28 +00001998
1999class Tk(Misc, Wm):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002000 """Toplevel widget of Tk which represents mostly the main window
Ezio Melotti42da6632011-03-15 05:18:48 +02002001 of an application. It has an associated Tcl interpreter."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002002 _w = '.'
Martin v. Löwis9441c072004-08-03 18:36:25 +00002003 def __init__(self, screenName=None, baseName=None, className='Tk',
2004 useTk=1, sync=0, use=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002005 """Return a new Toplevel widget on screen SCREENNAME. A new Tcl interpreter will
2006 be created. BASENAME will be used for the identification of the profile file (see
2007 readprofile).
2008 It is constructed from sys.argv[0] without extensions if None is given. CLASSNAME
2009 is the name of the widget class."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002010 self.master = None
2011 self.children = {}
David Aschere2b4b322004-02-18 05:59:53 +00002012 self._tkloaded = 0
2013 # to avoid recursions in the getattr code in case of failure, we
2014 # ensure that self.tk is always _something_.
Tim Peters182b5ac2004-07-18 06:16:08 +00002015 self.tk = None
Fredrik Lundh06d28152000-08-09 18:03:12 +00002016 if baseName is None:
Florent Xicluna54540ec2011-11-04 08:29:17 +01002017 import os
Fredrik Lundh06d28152000-08-09 18:03:12 +00002018 baseName = os.path.basename(sys.argv[0])
2019 baseName, ext = os.path.splitext(baseName)
Brett Cannonf299abd2015-04-13 14:21:02 -04002020 if ext not in ('.py', '.pyc'):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002021 baseName = baseName + ext
David Aschere2b4b322004-02-18 05:59:53 +00002022 interactive = 0
Martin v. Löwis9441c072004-08-03 18:36:25 +00002023 self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
David Aschere2b4b322004-02-18 05:59:53 +00002024 if useTk:
2025 self._loadtk()
Antoine Pitrou7ec3a322012-12-09 14:46:18 +01002026 if not sys.flags.ignore_environment:
2027 # Issue #16248: Honor the -E flag to avoid code injection.
2028 self.readprofile(baseName, className)
David Aschere2b4b322004-02-18 05:59:53 +00002029 def loadtk(self):
2030 if not self._tkloaded:
2031 self.tk.loadtk()
2032 self._loadtk()
2033 def _loadtk(self):
2034 self._tkloaded = 1
2035 global _default_root
Fredrik Lundh06d28152000-08-09 18:03:12 +00002036 # Version sanity checks
2037 tk_version = self.tk.getvar('tk_version')
2038 if tk_version != _tkinter.TK_VERSION:
Collin Winterce36ad82007-08-30 01:19:48 +00002039 raise RuntimeError("tk.h version (%s) doesn't match libtk.a version (%s)"
2040 % (_tkinter.TK_VERSION, tk_version))
Martin v. Löwis54895972003-05-24 11:37:15 +00002041 # Under unknown circumstances, tcl_version gets coerced to float
2042 tcl_version = str(self.tk.getvar('tcl_version'))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002043 if tcl_version != _tkinter.TCL_VERSION:
Collin Winterce36ad82007-08-30 01:19:48 +00002044 raise RuntimeError("tcl.h version (%s) doesn't match libtcl.a version (%s)" \
2045 % (_tkinter.TCL_VERSION, tcl_version))
Martin v. Löwis4afe1542005-03-01 08:09:28 +00002046 # Create and register the tkerror and exit commands
2047 # We need to inline parts of _register here, _ register
2048 # would register differently-named commands.
2049 if self._tclCommands is None:
2050 self._tclCommands = []
Fredrik Lundh06d28152000-08-09 18:03:12 +00002051 self.tk.createcommand('tkerror', _tkerror)
2052 self.tk.createcommand('exit', _exit)
Martin v. Löwis4afe1542005-03-01 08:09:28 +00002053 self._tclCommands.append('tkerror')
2054 self._tclCommands.append('exit')
Fredrik Lundh06d28152000-08-09 18:03:12 +00002055 if _support_default_root and not _default_root:
2056 _default_root = self
2057 self.protocol("WM_DELETE_WINDOW", self.destroy)
2058 def destroy(self):
2059 """Destroy this and all descendants widgets. This will
2060 end the application of this Tcl interpreter."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002061 for c in list(self.children.values()): c.destroy()
Fredrik Lundh06d28152000-08-09 18:03:12 +00002062 self.tk.call('destroy', self._w)
2063 Misc.destroy(self)
2064 global _default_root
2065 if _support_default_root and _default_root is self:
2066 _default_root = None
2067 def readprofile(self, baseName, className):
2068 """Internal function. It reads BASENAME.tcl and CLASSNAME.tcl into
Neal Norwitz01688022007-08-12 00:43:29 +00002069 the Tcl Interpreter and calls exec on the contents of BASENAME.py and
2070 CLASSNAME.py if such a file exists in the home directory."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002071 import os
Guido van Rossume014a132006-08-19 16:53:45 +00002072 if 'HOME' in os.environ: home = os.environ['HOME']
Fredrik Lundh06d28152000-08-09 18:03:12 +00002073 else: home = os.curdir
2074 class_tcl = os.path.join(home, '.%s.tcl' % className)
2075 class_py = os.path.join(home, '.%s.py' % className)
2076 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
2077 base_py = os.path.join(home, '.%s.py' % baseName)
2078 dir = {'self': self}
Georg Brandl14fc4272008-05-17 18:39:55 +00002079 exec('from tkinter import *', dir)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002080 if os.path.isfile(class_tcl):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002081 self.tk.call('source', class_tcl)
2082 if os.path.isfile(class_py):
Neal Norwitz01688022007-08-12 00:43:29 +00002083 exec(open(class_py).read(), dir)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002084 if os.path.isfile(base_tcl):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002085 self.tk.call('source', base_tcl)
2086 if os.path.isfile(base_py):
Neal Norwitz01688022007-08-12 00:43:29 +00002087 exec(open(base_py).read(), dir)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002088 def report_callback_exception(self, exc, val, tb):
Serhiy Storchaka95024872014-09-14 21:17:32 +03002089 """Report callback exception on sys.stderr.
2090
2091 Applications may want to override this internal function, and
2092 should when sys.stderr is None."""
Florent Xicluna54540ec2011-11-04 08:29:17 +01002093 import traceback
Serhiy Storchaka95024872014-09-14 21:17:32 +03002094 print("Exception in Tkinter callback", file=sys.stderr)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002095 sys.last_type = exc
2096 sys.last_value = val
2097 sys.last_traceback = tb
2098 traceback.print_exception(exc, val, tb)
David Aschere2b4b322004-02-18 05:59:53 +00002099 def __getattr__(self, attr):
2100 "Delegate attribute access to the interpreter object"
2101 return getattr(self.tk, attr)
Guido van Rossum18468821994-06-20 07:49:28 +00002102
Guido van Rossum368e06b1997-11-07 20:38:49 +00002103# Ideally, the classes Pack, Place and Grid disappear, the
2104# pack/place/grid methods are defined on the Widget class, and
2105# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
2106# ...), with pack(), place() and grid() being short for
2107# pack_configure(), place_configure() and grid_columnconfigure(), and
2108# forget() being short for pack_forget(). As a practical matter, I'm
2109# afraid that there is too much code out there that may be using the
2110# Pack, Place or Grid class, so I leave them intact -- but only as
2111# backwards compatibility features. Also note that those methods that
2112# take a master as argument (e.g. pack_propagate) have been moved to
2113# the Misc class (which now incorporates all methods common between
2114# toplevel and interior widgets). Again, for compatibility, these are
2115# copied into the Pack, Place or Grid class.
2116
David Aschere2b4b322004-02-18 05:59:53 +00002117
2118def Tcl(screenName=None, baseName=None, className='Tk', useTk=0):
2119 return Tk(screenName, baseName, className, useTk)
2120
Guido van Rossum18468821994-06-20 07:49:28 +00002121class Pack:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002122 """Geometry manager Pack.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002123
Fredrik Lundh06d28152000-08-09 18:03:12 +00002124 Base class to use the methods pack_* in every widget."""
2125 def pack_configure(self, cnf={}, **kw):
2126 """Pack a widget in the parent widget. Use as options:
2127 after=widget - pack it after you have packed widget
2128 anchor=NSEW (or subset) - position widget according to
2129 given direction
Georg Brandlbf1eb632008-05-29 07:19:00 +00002130 before=widget - pack it before you will pack widget
Martin v. Löwisbfe175c2003-04-16 19:42:51 +00002131 expand=bool - expand widget if parent size grows
Fredrik Lundh06d28152000-08-09 18:03:12 +00002132 fill=NONE or X or Y or BOTH - fill widget if widget grows
2133 in=master - use master to contain this widget
Georg Brandlbf1eb632008-05-29 07:19:00 +00002134 in_=master - see 'in' option description
Fredrik Lundh06d28152000-08-09 18:03:12 +00002135 ipadx=amount - add internal padding in x direction
2136 ipady=amount - add internal padding in y direction
2137 padx=amount - add padding in x direction
2138 pady=amount - add padding in y direction
2139 side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget.
2140 """
2141 self.tk.call(
2142 ('pack', 'configure', self._w)
2143 + self._options(cnf, kw))
2144 pack = configure = config = pack_configure
2145 def pack_forget(self):
2146 """Unmap this widget and do not use it for the packing order."""
2147 self.tk.call('pack', 'forget', self._w)
2148 forget = pack_forget
2149 def pack_info(self):
2150 """Return information about the packing options
2151 for this widget."""
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +03002152 d = _splitdict(self.tk, self.tk.call('pack', 'info', self._w))
2153 if 'in' in d:
2154 d['in'] = self.nametowidget(d['in'])
2155 return d
Fredrik Lundh06d28152000-08-09 18:03:12 +00002156 info = pack_info
2157 propagate = pack_propagate = Misc.pack_propagate
2158 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00002159
2160class Place:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002161 """Geometry manager Place.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002162
Fredrik Lundh06d28152000-08-09 18:03:12 +00002163 Base class to use the methods place_* in every widget."""
2164 def place_configure(self, cnf={}, **kw):
2165 """Place a widget in the parent widget. Use as options:
Georg Brandlbf1eb632008-05-29 07:19:00 +00002166 in=master - master relative to which the widget is placed
2167 in_=master - see 'in' option description
Fredrik Lundh06d28152000-08-09 18:03:12 +00002168 x=amount - locate anchor of this widget at position x of master
2169 y=amount - locate anchor of this widget at position y of master
2170 relx=amount - locate anchor of this widget between 0.0 and 1.0
2171 relative to width of master (1.0 is right edge)
Georg Brandlbf1eb632008-05-29 07:19:00 +00002172 rely=amount - locate anchor of this widget between 0.0 and 1.0
Fredrik Lundh06d28152000-08-09 18:03:12 +00002173 relative to height of master (1.0 is bottom edge)
Georg Brandlbf1eb632008-05-29 07:19:00 +00002174 anchor=NSEW (or subset) - position anchor according to given direction
Fredrik Lundh06d28152000-08-09 18:03:12 +00002175 width=amount - width of this widget in pixel
2176 height=amount - height of this widget in pixel
2177 relwidth=amount - width of this widget between 0.0 and 1.0
2178 relative to width of master (1.0 is the same width
Georg Brandlbf1eb632008-05-29 07:19:00 +00002179 as the master)
2180 relheight=amount - height of this widget between 0.0 and 1.0
Fredrik Lundh06d28152000-08-09 18:03:12 +00002181 relative to height of master (1.0 is the same
Georg Brandlbf1eb632008-05-29 07:19:00 +00002182 height as the master)
2183 bordermode="inside" or "outside" - whether to take border width of
2184 master widget into account
2185 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002186 self.tk.call(
2187 ('place', 'configure', self._w)
2188 + self._options(cnf, kw))
2189 place = configure = config = place_configure
2190 def place_forget(self):
2191 """Unmap this widget."""
2192 self.tk.call('place', 'forget', self._w)
2193 forget = place_forget
2194 def place_info(self):
2195 """Return information about the placing options
2196 for this widget."""
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +03002197 d = _splitdict(self.tk, self.tk.call('place', 'info', self._w))
2198 if 'in' in d:
2199 d['in'] = self.nametowidget(d['in'])
2200 return d
Fredrik Lundh06d28152000-08-09 18:03:12 +00002201 info = place_info
2202 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00002203
Guido van Rossum37dcab11996-05-16 16:00:19 +00002204class Grid:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002205 """Geometry manager Grid.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002206
Fredrik Lundh06d28152000-08-09 18:03:12 +00002207 Base class to use the methods grid_* in every widget."""
2208 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
2209 def grid_configure(self, cnf={}, **kw):
2210 """Position a widget in the parent widget in a grid. Use as options:
2211 column=number - use cell identified with given column (starting with 0)
2212 columnspan=number - this widget will span several columns
2213 in=master - use master to contain this widget
Georg Brandlbf1eb632008-05-29 07:19:00 +00002214 in_=master - see 'in' option description
Fredrik Lundh06d28152000-08-09 18:03:12 +00002215 ipadx=amount - add internal padding in x direction
2216 ipady=amount - add internal padding in y direction
2217 padx=amount - add padding in x direction
2218 pady=amount - add padding in y direction
2219 row=number - use cell identified with given row (starting with 0)
2220 rowspan=number - this widget will span several rows
2221 sticky=NSEW - if cell is larger on which sides will this
2222 widget stick to the cell boundary
2223 """
2224 self.tk.call(
2225 ('grid', 'configure', self._w)
2226 + self._options(cnf, kw))
2227 grid = configure = config = grid_configure
2228 bbox = grid_bbox = Misc.grid_bbox
2229 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
2230 def grid_forget(self):
2231 """Unmap this widget."""
2232 self.tk.call('grid', 'forget', self._w)
2233 forget = grid_forget
2234 def grid_remove(self):
2235 """Unmap this widget but remember the grid options."""
2236 self.tk.call('grid', 'remove', self._w)
2237 def grid_info(self):
2238 """Return information about the options
2239 for positioning this widget in a grid."""
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +03002240 d = _splitdict(self.tk, self.tk.call('grid', 'info', self._w))
2241 if 'in' in d:
2242 d['in'] = self.nametowidget(d['in'])
2243 return d
Fredrik Lundh06d28152000-08-09 18:03:12 +00002244 info = grid_info
Martin v. Löwisdc579092001-10-13 09:33:51 +00002245 location = grid_location = Misc.grid_location
Fredrik Lundh06d28152000-08-09 18:03:12 +00002246 propagate = grid_propagate = Misc.grid_propagate
2247 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
2248 size = grid_size = Misc.grid_size
2249 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00002250
Guido van Rossum368e06b1997-11-07 20:38:49 +00002251class BaseWidget(Misc):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002252 """Internal class."""
2253 def _setup(self, master, cnf):
2254 """Internal function. Sets up information about children."""
2255 if _support_default_root:
2256 global _default_root
2257 if not master:
2258 if not _default_root:
2259 _default_root = Tk()
2260 master = _default_root
2261 self.master = master
2262 self.tk = master.tk
2263 name = None
Guido van Rossume014a132006-08-19 16:53:45 +00002264 if 'name' in cnf:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002265 name = cnf['name']
2266 del cnf['name']
2267 if not name:
Serhiy Storchaka8acb5682016-06-13 09:24:11 +03002268 name = self.__class__.__name__.lower()
2269 if master._last_child_ids is None:
2270 master._last_child_ids = {}
2271 count = master._last_child_ids.get(name, 0) + 1
2272 master._last_child_ids[name] = count
2273 if count == 1:
Serhiy Storchakab1f52872016-10-25 09:43:48 +03002274 name = '!%s' % (name,)
Serhiy Storchaka8acb5682016-06-13 09:24:11 +03002275 else:
Serhiy Storchakab1f52872016-10-25 09:43:48 +03002276 name = '!%s%d' % (name, count)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002277 self._name = name
2278 if master._w=='.':
2279 self._w = '.' + name
2280 else:
2281 self._w = master._w + '.' + name
2282 self.children = {}
Guido van Rossume014a132006-08-19 16:53:45 +00002283 if self._name in self.master.children:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002284 self.master.children[self._name].destroy()
2285 self.master.children[self._name] = self
2286 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
2287 """Construct a widget with the parent widget MASTER, a name WIDGETNAME
2288 and appropriate options."""
2289 if kw:
2290 cnf = _cnfmerge((cnf, kw))
2291 self.widgetName = widgetName
2292 BaseWidget._setup(self, master, cnf)
Hirokazu Yamamotoa18424c2008-11-04 06:26:27 +00002293 if self._tclCommands is None:
2294 self._tclCommands = []
Guilherme Polob212b752008-09-04 11:21:31 +00002295 classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)]
2296 for k, v in classes:
2297 del cnf[k]
Fredrik Lundh06d28152000-08-09 18:03:12 +00002298 self.tk.call(
2299 (widgetName, self._w) + extra + self._options(cnf))
2300 for k, v in classes:
2301 k.configure(self, v)
2302 def destroy(self):
2303 """Destroy this and all descendants widgets."""
Guido van Rossum992d4a32007-07-11 13:09:30 +00002304 for c in list(self.children.values()): c.destroy()
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002305 self.tk.call('destroy', self._w)
Guido van Rossume014a132006-08-19 16:53:45 +00002306 if self._name in self.master.children:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002307 del self.master.children[self._name]
Fredrik Lundh06d28152000-08-09 18:03:12 +00002308 Misc.destroy(self)
2309 def _do(self, name, args=()):
2310 # XXX Obsolete -- better use self.tk.call directly!
2311 return self.tk.call((self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00002312
Guido van Rossum368e06b1997-11-07 20:38:49 +00002313class Widget(BaseWidget, Pack, Place, Grid):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002314 """Internal class.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002315
Fredrik Lundh06d28152000-08-09 18:03:12 +00002316 Base class for a widget which can be positioned with the geometry managers
2317 Pack, Place or Grid."""
2318 pass
Guido van Rossum368e06b1997-11-07 20:38:49 +00002319
2320class Toplevel(BaseWidget, Wm):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002321 """Toplevel widget, e.g. for dialogs."""
2322 def __init__(self, master=None, cnf={}, **kw):
2323 """Construct a toplevel widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002324
Fredrik Lundh06d28152000-08-09 18:03:12 +00002325 Valid resource names: background, bd, bg, borderwidth, class,
2326 colormap, container, cursor, height, highlightbackground,
2327 highlightcolor, highlightthickness, menu, relief, screen, takefocus,
2328 use, visual, width."""
2329 if kw:
2330 cnf = _cnfmerge((cnf, kw))
2331 extra = ()
2332 for wmkey in ['screen', 'class_', 'class', 'visual',
2333 'colormap']:
Guido van Rossume014a132006-08-19 16:53:45 +00002334 if wmkey in cnf:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002335 val = cnf[wmkey]
2336 # TBD: a hack needed because some keys
2337 # are not valid as keyword arguments
2338 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
2339 else: opt = '-'+wmkey
2340 extra = extra + (opt, val)
2341 del cnf[wmkey]
2342 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
2343 root = self._root()
2344 self.iconname(root.iconname())
2345 self.title(root.title())
2346 self.protocol("WM_DELETE_WINDOW", self.destroy)
Guido van Rossum18468821994-06-20 07:49:28 +00002347
2348class Button(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002349 """Button widget."""
2350 def __init__(self, master=None, cnf={}, **kw):
2351 """Construct a button widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00002352
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002353 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002354
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002355 activebackground, activeforeground, anchor,
2356 background, bitmap, borderwidth, cursor,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002357 disabledforeground, font, foreground
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002358 highlightbackground, highlightcolor,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002359 highlightthickness, image, justify,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002360 padx, pady, relief, repeatdelay,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002361 repeatinterval, takefocus, text,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002362 textvariable, underline, wraplength
Raymond Hettingerff41c482003-04-06 09:01:11 +00002363
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002364 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002365
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002366 command, compound, default, height,
2367 overrelief, state, width
2368 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002369 Widget.__init__(self, master, 'button', cnf, kw)
Raymond Hettingerff41c482003-04-06 09:01:11 +00002370
Fredrik Lundh06d28152000-08-09 18:03:12 +00002371 def flash(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00002372 """Flash the button.
2373
2374 This is accomplished by redisplaying
2375 the button several times, alternating between active and
2376 normal colors. At the end of the flash the button is left
2377 in the same normal/active state as when the command was
2378 invoked. This command is ignored if the button's state is
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002379 disabled.
2380 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002381 self.tk.call(self._w, 'flash')
Raymond Hettingerff41c482003-04-06 09:01:11 +00002382
Fredrik Lundh06d28152000-08-09 18:03:12 +00002383 def invoke(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00002384 """Invoke the command associated with the button.
2385
2386 The return value is the return value from the command,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002387 or an empty string if there is no command associated with
2388 the button. This command is ignored if the button's state
2389 is disabled.
2390 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002391 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00002392
Guilherme Polo1fff0082009-08-14 15:05:30 +00002393class Canvas(Widget, XView, YView):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002394 """Canvas widget to display graphical elements like lines or text."""
2395 def __init__(self, master=None, cnf={}, **kw):
2396 """Construct a canvas widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002397
Fredrik Lundh06d28152000-08-09 18:03:12 +00002398 Valid resource names: background, bd, bg, borderwidth, closeenough,
2399 confine, cursor, height, highlightbackground, highlightcolor,
2400 highlightthickness, insertbackground, insertborderwidth,
2401 insertofftime, insertontime, insertwidth, offset, relief,
2402 scrollregion, selectbackground, selectborderwidth, selectforeground,
2403 state, takefocus, width, xscrollcommand, xscrollincrement,
2404 yscrollcommand, yscrollincrement."""
2405 Widget.__init__(self, master, 'canvas', cnf, kw)
2406 def addtag(self, *args):
2407 """Internal function."""
2408 self.tk.call((self._w, 'addtag') + args)
2409 def addtag_above(self, newtag, tagOrId):
2410 """Add tag NEWTAG to all items above TAGORID."""
2411 self.addtag(newtag, 'above', tagOrId)
2412 def addtag_all(self, newtag):
2413 """Add tag NEWTAG to all items."""
2414 self.addtag(newtag, 'all')
2415 def addtag_below(self, newtag, tagOrId):
2416 """Add tag NEWTAG to all items below TAGORID."""
2417 self.addtag(newtag, 'below', tagOrId)
2418 def addtag_closest(self, newtag, x, y, halo=None, start=None):
2419 """Add tag NEWTAG to item which is closest to pixel at X, Y.
2420 If several match take the top-most.
2421 All items closer than HALO are considered overlapping (all are
2422 closests). If START is specified the next below this tag is taken."""
2423 self.addtag(newtag, 'closest', x, y, halo, start)
2424 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
2425 """Add tag NEWTAG to all items in the rectangle defined
2426 by X1,Y1,X2,Y2."""
2427 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
2428 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
2429 """Add tag NEWTAG to all items which overlap the rectangle
2430 defined by X1,Y1,X2,Y2."""
2431 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
2432 def addtag_withtag(self, newtag, tagOrId):
2433 """Add tag NEWTAG to all items with TAGORID."""
2434 self.addtag(newtag, 'withtag', tagOrId)
2435 def bbox(self, *args):
2436 """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
2437 which encloses all items with tags specified as arguments."""
2438 return self._getints(
2439 self.tk.call((self._w, 'bbox') + args)) or None
2440 def tag_unbind(self, tagOrId, sequence, funcid=None):
2441 """Unbind for all items with TAGORID for event SEQUENCE the
2442 function identified with FUNCID."""
2443 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
2444 if funcid:
2445 self.deletecommand(funcid)
2446 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
2447 """Bind to all items with TAGORID at event SEQUENCE a call to function FUNC.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002448
Fredrik Lundh06d28152000-08-09 18:03:12 +00002449 An additional boolean parameter ADD specifies whether FUNC will be
2450 called additionally to the other bound function or whether it will
2451 replace the previous function. See bind for the return value."""
2452 return self._bind((self._w, 'bind', tagOrId),
2453 sequence, func, add)
2454 def canvasx(self, screenx, gridspacing=None):
2455 """Return the canvas x coordinate of pixel position SCREENX rounded
2456 to nearest multiple of GRIDSPACING units."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002457 return self.tk.getdouble(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002458 self._w, 'canvasx', screenx, gridspacing))
2459 def canvasy(self, screeny, gridspacing=None):
2460 """Return the canvas y coordinate of pixel position SCREENY rounded
2461 to nearest multiple of GRIDSPACING units."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002462 return self.tk.getdouble(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002463 self._w, 'canvasy', screeny, gridspacing))
2464 def coords(self, *args):
2465 """Return a list of coordinates for the item given in ARGS."""
2466 # XXX Should use _flatten on args
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002467 return [self.tk.getdouble(x) for x in
Guido van Rossum0bd54331998-05-19 21:18:13 +00002468 self.tk.splitlist(
Alexander Belopolsky022f0492010-11-22 19:40:51 +00002469 self.tk.call((self._w, 'coords') + args))]
Fredrik Lundh06d28152000-08-09 18:03:12 +00002470 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
2471 """Internal function."""
2472 args = _flatten(args)
2473 cnf = args[-1]
Guido van Rossum13257902007-06-07 23:15:56 +00002474 if isinstance(cnf, (dict, tuple)):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002475 args = args[:-1]
2476 else:
2477 cnf = {}
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002478 return self.tk.getint(self.tk.call(
Raymond Hettingerff41c482003-04-06 09:01:11 +00002479 self._w, 'create', itemType,
2480 *(args + self._options(cnf, kw))))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002481 def create_arc(self, *args, **kw):
2482 """Create arc shaped region with coordinates x1,y1,x2,y2."""
2483 return self._create('arc', args, kw)
2484 def create_bitmap(self, *args, **kw):
2485 """Create bitmap with coordinates x1,y1."""
2486 return self._create('bitmap', args, kw)
2487 def create_image(self, *args, **kw):
2488 """Create image item with coordinates x1,y1."""
2489 return self._create('image', args, kw)
2490 def create_line(self, *args, **kw):
2491 """Create line with coordinates x1,y1,...,xn,yn."""
2492 return self._create('line', args, kw)
2493 def create_oval(self, *args, **kw):
2494 """Create oval with coordinates x1,y1,x2,y2."""
2495 return self._create('oval', args, kw)
2496 def create_polygon(self, *args, **kw):
2497 """Create polygon with coordinates x1,y1,...,xn,yn."""
2498 return self._create('polygon', args, kw)
2499 def create_rectangle(self, *args, **kw):
2500 """Create rectangle with coordinates x1,y1,x2,y2."""
2501 return self._create('rectangle', args, kw)
2502 def create_text(self, *args, **kw):
2503 """Create text with coordinates x1,y1."""
2504 return self._create('text', args, kw)
2505 def create_window(self, *args, **kw):
2506 """Create window with coordinates x1,y1,x2,y2."""
2507 return self._create('window', args, kw)
2508 def dchars(self, *args):
2509 """Delete characters of text items identified by tag or id in ARGS (possibly
2510 several times) from FIRST to LAST character (including)."""
2511 self.tk.call((self._w, 'dchars') + args)
2512 def delete(self, *args):
2513 """Delete items identified by all tag or ids contained in ARGS."""
2514 self.tk.call((self._w, 'delete') + args)
2515 def dtag(self, *args):
2516 """Delete tag or id given as last arguments in ARGS from items
2517 identified by first argument in ARGS."""
2518 self.tk.call((self._w, 'dtag') + args)
2519 def find(self, *args):
2520 """Internal function."""
2521 return self._getints(
2522 self.tk.call((self._w, 'find') + args)) or ()
2523 def find_above(self, tagOrId):
2524 """Return items above TAGORID."""
2525 return self.find('above', tagOrId)
2526 def find_all(self):
2527 """Return all items."""
2528 return self.find('all')
2529 def find_below(self, tagOrId):
2530 """Return all items below TAGORID."""
2531 return self.find('below', tagOrId)
2532 def find_closest(self, x, y, halo=None, start=None):
2533 """Return item which is closest to pixel at X, Y.
2534 If several match take the top-most.
2535 All items closer than HALO are considered overlapping (all are
Vijay Kumar128641d2017-05-22 21:53:52 +05302536 closest). If START is specified the next below this tag is taken."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002537 return self.find('closest', x, y, halo, start)
2538 def find_enclosed(self, x1, y1, x2, y2):
2539 """Return all items in rectangle defined
2540 by X1,Y1,X2,Y2."""
2541 return self.find('enclosed', x1, y1, x2, y2)
2542 def find_overlapping(self, x1, y1, x2, y2):
2543 """Return all items which overlap the rectangle
2544 defined by X1,Y1,X2,Y2."""
2545 return self.find('overlapping', x1, y1, x2, y2)
2546 def find_withtag(self, tagOrId):
2547 """Return all items with TAGORID."""
2548 return self.find('withtag', tagOrId)
2549 def focus(self, *args):
2550 """Set focus to the first item specified in ARGS."""
2551 return self.tk.call((self._w, 'focus') + args)
2552 def gettags(self, *args):
2553 """Return tags associated with the first item specified in ARGS."""
2554 return self.tk.splitlist(
2555 self.tk.call((self._w, 'gettags') + args))
2556 def icursor(self, *args):
2557 """Set cursor at position POS in the item identified by TAGORID.
2558 In ARGS TAGORID must be first."""
2559 self.tk.call((self._w, 'icursor') + args)
2560 def index(self, *args):
2561 """Return position of cursor as integer in item specified in ARGS."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002562 return self.tk.getint(self.tk.call((self._w, 'index') + args))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002563 def insert(self, *args):
2564 """Insert TEXT in item TAGORID at position POS. ARGS must
2565 be TAGORID POS TEXT."""
2566 self.tk.call((self._w, 'insert') + args)
2567 def itemcget(self, tagOrId, option):
2568 """Return the resource value for an OPTION for item TAGORID."""
2569 return self.tk.call(
2570 (self._w, 'itemcget') + (tagOrId, '-'+option))
2571 def itemconfigure(self, tagOrId, cnf=None, **kw):
2572 """Configure resources of an item TAGORID.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002573
Fredrik Lundh06d28152000-08-09 18:03:12 +00002574 The values for resources are specified as keyword
2575 arguments. To get an overview about
2576 the allowed keyword arguments call the method without arguments.
2577 """
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002578 return self._configure(('itemconfigure', tagOrId), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002579 itemconfig = itemconfigure
2580 # lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
2581 # so the preferred name for them is tag_lower, tag_raise
2582 # (similar to tag_bind, and similar to the Text widget);
2583 # unfortunately can't delete the old ones yet (maybe in 1.6)
2584 def tag_lower(self, *args):
2585 """Lower an item TAGORID given in ARGS
2586 (optional below another item)."""
2587 self.tk.call((self._w, 'lower') + args)
2588 lower = tag_lower
2589 def move(self, *args):
2590 """Move an item TAGORID given in ARGS."""
2591 self.tk.call((self._w, 'move') + args)
2592 def postscript(self, cnf={}, **kw):
2593 """Print the contents of the canvas to a postscript
2594 file. Valid options: colormap, colormode, file, fontmap,
2595 height, pageanchor, pageheight, pagewidth, pagex, pagey,
Vijay Kumar128641d2017-05-22 21:53:52 +05302596 rotate, width, x, y."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002597 return self.tk.call((self._w, 'postscript') +
2598 self._options(cnf, kw))
2599 def tag_raise(self, *args):
2600 """Raise an item TAGORID given in ARGS
2601 (optional above another item)."""
2602 self.tk.call((self._w, 'raise') + args)
2603 lift = tkraise = tag_raise
2604 def scale(self, *args):
2605 """Scale item TAGORID with XORIGIN, YORIGIN, XSCALE, YSCALE."""
2606 self.tk.call((self._w, 'scale') + args)
2607 def scan_mark(self, x, y):
2608 """Remember the current X, Y coordinates."""
2609 self.tk.call(self._w, 'scan', 'mark', x, y)
Neal Norwitze931ed52003-01-10 23:24:32 +00002610 def scan_dragto(self, x, y, gain=10):
2611 """Adjust the view of the canvas to GAIN times the
Fredrik Lundh06d28152000-08-09 18:03:12 +00002612 difference between X and Y and the coordinates given in
2613 scan_mark."""
Neal Norwitze931ed52003-01-10 23:24:32 +00002614 self.tk.call(self._w, 'scan', 'dragto', x, y, gain)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002615 def select_adjust(self, tagOrId, index):
2616 """Adjust the end of the selection near the cursor of an item TAGORID to index."""
2617 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
2618 def select_clear(self):
2619 """Clear the selection if it is in this widget."""
2620 self.tk.call(self._w, 'select', 'clear')
2621 def select_from(self, tagOrId, index):
2622 """Set the fixed end of a selection in item TAGORID to INDEX."""
2623 self.tk.call(self._w, 'select', 'from', tagOrId, index)
2624 def select_item(self):
2625 """Return the item which has the selection."""
Neal Norwitz58b63bf2002-07-23 02:52:58 +00002626 return self.tk.call(self._w, 'select', 'item') or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00002627 def select_to(self, tagOrId, index):
2628 """Set the variable end of a selection in item TAGORID to INDEX."""
2629 self.tk.call(self._w, 'select', 'to', tagOrId, index)
2630 def type(self, tagOrId):
2631 """Return the type of the item TAGORID."""
2632 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +00002633
2634class Checkbutton(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002635 """Checkbutton widget which is either in on- or off-state."""
2636 def __init__(self, master=None, cnf={}, **kw):
2637 """Construct a checkbutton widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002638
Fredrik Lundh06d28152000-08-09 18:03:12 +00002639 Valid resource names: activebackground, activeforeground, anchor,
2640 background, bd, bg, bitmap, borderwidth, command, cursor,
2641 disabledforeground, fg, font, foreground, height,
2642 highlightbackground, highlightcolor, highlightthickness, image,
2643 indicatoron, justify, offvalue, onvalue, padx, pady, relief,
2644 selectcolor, selectimage, state, takefocus, text, textvariable,
2645 underline, variable, width, wraplength."""
2646 Widget.__init__(self, master, 'checkbutton', cnf, kw)
2647 def deselect(self):
2648 """Put the button in off-state."""
2649 self.tk.call(self._w, 'deselect')
2650 def flash(self):
2651 """Flash the button."""
2652 self.tk.call(self._w, 'flash')
2653 def invoke(self):
2654 """Toggle the button and invoke a command if given as resource."""
2655 return self.tk.call(self._w, 'invoke')
2656 def select(self):
2657 """Put the button in on-state."""
2658 self.tk.call(self._w, 'select')
2659 def toggle(self):
2660 """Toggle the button."""
2661 self.tk.call(self._w, 'toggle')
Guido van Rossum18468821994-06-20 07:49:28 +00002662
Guilherme Polo1fff0082009-08-14 15:05:30 +00002663class Entry(Widget, XView):
Martin Panterc04fb562016-02-10 05:44:01 +00002664 """Entry widget which allows displaying simple text."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002665 def __init__(self, master=None, cnf={}, **kw):
2666 """Construct an entry widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002667
Fredrik Lundh06d28152000-08-09 18:03:12 +00002668 Valid resource names: background, bd, bg, borderwidth, cursor,
2669 exportselection, fg, font, foreground, highlightbackground,
2670 highlightcolor, highlightthickness, insertbackground,
2671 insertborderwidth, insertofftime, insertontime, insertwidth,
2672 invalidcommand, invcmd, justify, relief, selectbackground,
2673 selectborderwidth, selectforeground, show, state, takefocus,
2674 textvariable, validate, validatecommand, vcmd, width,
2675 xscrollcommand."""
2676 Widget.__init__(self, master, 'entry', cnf, kw)
2677 def delete(self, first, last=None):
2678 """Delete text from FIRST to LAST (not included)."""
2679 self.tk.call(self._w, 'delete', first, last)
2680 def get(self):
2681 """Return the text."""
2682 return self.tk.call(self._w, 'get')
2683 def icursor(self, index):
2684 """Insert cursor at INDEX."""
2685 self.tk.call(self._w, 'icursor', index)
2686 def index(self, index):
2687 """Return position of cursor."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002688 return self.tk.getint(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002689 self._w, 'index', index))
2690 def insert(self, index, string):
2691 """Insert STRING at INDEX."""
2692 self.tk.call(self._w, 'insert', index, string)
2693 def scan_mark(self, x):
2694 """Remember the current X, Y coordinates."""
2695 self.tk.call(self._w, 'scan', 'mark', x)
2696 def scan_dragto(self, x):
2697 """Adjust the view of the canvas to 10 times the
2698 difference between X and Y and the coordinates given in
2699 scan_mark."""
2700 self.tk.call(self._w, 'scan', 'dragto', x)
2701 def selection_adjust(self, index):
2702 """Adjust the end of the selection near the cursor to INDEX."""
2703 self.tk.call(self._w, 'selection', 'adjust', index)
2704 select_adjust = selection_adjust
2705 def selection_clear(self):
2706 """Clear the selection if it is in this widget."""
2707 self.tk.call(self._w, 'selection', 'clear')
2708 select_clear = selection_clear
2709 def selection_from(self, index):
2710 """Set the fixed end of a selection to INDEX."""
2711 self.tk.call(self._w, 'selection', 'from', index)
2712 select_from = selection_from
2713 def selection_present(self):
Guilherme Polo1fff0082009-08-14 15:05:30 +00002714 """Return True if there are characters selected in the entry, False
2715 otherwise."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002716 return self.tk.getboolean(
2717 self.tk.call(self._w, 'selection', 'present'))
2718 select_present = selection_present
2719 def selection_range(self, start, end):
2720 """Set the selection from START to END (not included)."""
2721 self.tk.call(self._w, 'selection', 'range', start, end)
2722 select_range = selection_range
2723 def selection_to(self, index):
2724 """Set the variable end of a selection to INDEX."""
2725 self.tk.call(self._w, 'selection', 'to', index)
2726 select_to = selection_to
Guido van Rossum18468821994-06-20 07:49:28 +00002727
2728class Frame(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002729 """Frame widget which may contain other widgets and can have a 3D border."""
2730 def __init__(self, master=None, cnf={}, **kw):
2731 """Construct a frame widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002732
Fredrik Lundh06d28152000-08-09 18:03:12 +00002733 Valid resource names: background, bd, bg, borderwidth, class,
2734 colormap, container, cursor, height, highlightbackground,
2735 highlightcolor, highlightthickness, relief, takefocus, visual, width."""
2736 cnf = _cnfmerge((cnf, kw))
2737 extra = ()
Guido van Rossume014a132006-08-19 16:53:45 +00002738 if 'class_' in cnf:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002739 extra = ('-class', cnf['class_'])
2740 del cnf['class_']
Guido van Rossume014a132006-08-19 16:53:45 +00002741 elif 'class' in cnf:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002742 extra = ('-class', cnf['class'])
2743 del cnf['class']
2744 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00002745
2746class Label(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002747 """Label widget which can display text and bitmaps."""
2748 def __init__(self, master=None, cnf={}, **kw):
2749 """Construct a label widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00002750
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002751 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002752
2753 activebackground, activeforeground, anchor,
2754 background, bitmap, borderwidth, cursor,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002755 disabledforeground, font, foreground,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002756 highlightbackground, highlightcolor,
2757 highlightthickness, image, justify,
2758 padx, pady, relief, takefocus, text,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002759 textvariable, underline, wraplength
Raymond Hettingerff41c482003-04-06 09:01:11 +00002760
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002761 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002762
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002763 height, state, width
Raymond Hettingerff41c482003-04-06 09:01:11 +00002764
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002765 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002766 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002767
Guilherme Polo1fff0082009-08-14 15:05:30 +00002768class Listbox(Widget, XView, YView):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002769 """Listbox widget which can display a list of strings."""
2770 def __init__(self, master=None, cnf={}, **kw):
2771 """Construct a listbox widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002772
Fredrik Lundh06d28152000-08-09 18:03:12 +00002773 Valid resource names: background, bd, bg, borderwidth, cursor,
2774 exportselection, fg, font, foreground, height, highlightbackground,
2775 highlightcolor, highlightthickness, relief, selectbackground,
2776 selectborderwidth, selectforeground, selectmode, setgrid, takefocus,
2777 width, xscrollcommand, yscrollcommand, listvariable."""
2778 Widget.__init__(self, master, 'listbox', cnf, kw)
2779 def activate(self, index):
2780 """Activate item identified by INDEX."""
2781 self.tk.call(self._w, 'activate', index)
Serhiy Storchakafc14ad92014-06-02 21:31:07 +03002782 def bbox(self, index):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002783 """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
Serhiy Storchakafc14ad92014-06-02 21:31:07 +03002784 which encloses the item identified by the given index."""
2785 return self._getints(self.tk.call(self._w, 'bbox', index)) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00002786 def curselection(self):
Serhiy Storchakafc14ad92014-06-02 21:31:07 +03002787 """Return the indices of currently selected item."""
2788 return self._getints(self.tk.call(self._w, 'curselection')) or ()
Fredrik Lundh06d28152000-08-09 18:03:12 +00002789 def delete(self, first, last=None):
Serhiy Storchakaca3cd002014-06-02 16:51:44 +03002790 """Delete items from FIRST to LAST (included)."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002791 self.tk.call(self._w, 'delete', first, last)
2792 def get(self, first, last=None):
Serhiy Storchakaca3cd002014-06-02 16:51:44 +03002793 """Get list of items from FIRST to LAST (included)."""
Serhiy Storchakafc14ad92014-06-02 21:31:07 +03002794 if last is not None:
Fredrik Lundh06d28152000-08-09 18:03:12 +00002795 return self.tk.splitlist(self.tk.call(
2796 self._w, 'get', first, last))
2797 else:
2798 return self.tk.call(self._w, 'get', first)
2799 def index(self, index):
2800 """Return index of item identified with INDEX."""
2801 i = self.tk.call(self._w, 'index', index)
2802 if i == 'none': return None
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002803 return self.tk.getint(i)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002804 def insert(self, index, *elements):
2805 """Insert ELEMENTS at INDEX."""
2806 self.tk.call((self._w, 'insert', index) + elements)
2807 def nearest(self, y):
2808 """Get index of item which is nearest to y coordinate Y."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002809 return self.tk.getint(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002810 self._w, 'nearest', y))
2811 def scan_mark(self, x, y):
2812 """Remember the current X, Y coordinates."""
2813 self.tk.call(self._w, 'scan', 'mark', x, y)
2814 def scan_dragto(self, x, y):
2815 """Adjust the view of the listbox to 10 times the
2816 difference between X and Y and the coordinates given in
2817 scan_mark."""
2818 self.tk.call(self._w, 'scan', 'dragto', x, y)
2819 def see(self, index):
2820 """Scroll such that INDEX is visible."""
2821 self.tk.call(self._w, 'see', index)
2822 def selection_anchor(self, index):
2823 """Set the fixed end oft the selection to INDEX."""
2824 self.tk.call(self._w, 'selection', 'anchor', index)
2825 select_anchor = selection_anchor
2826 def selection_clear(self, first, last=None):
Serhiy Storchakaca3cd002014-06-02 16:51:44 +03002827 """Clear the selection from FIRST to LAST (included)."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002828 self.tk.call(self._w,
2829 'selection', 'clear', first, last)
2830 select_clear = selection_clear
2831 def selection_includes(self, index):
2832 """Return 1 if INDEX is part of the selection."""
2833 return self.tk.getboolean(self.tk.call(
2834 self._w, 'selection', 'includes', index))
2835 select_includes = selection_includes
2836 def selection_set(self, first, last=None):
Serhiy Storchakaca3cd002014-06-02 16:51:44 +03002837 """Set the selection from FIRST to LAST (included) without
Fredrik Lundh06d28152000-08-09 18:03:12 +00002838 changing the currently selected elements."""
2839 self.tk.call(self._w, 'selection', 'set', first, last)
2840 select_set = selection_set
2841 def size(self):
2842 """Return the number of elements in the listbox."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002843 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum09f1ad82001-09-05 19:29:56 +00002844 def itemcget(self, index, option):
2845 """Return the resource value for an ITEM and an OPTION."""
2846 return self.tk.call(
2847 (self._w, 'itemcget') + (index, '-'+option))
Guido van Rossuma0adb922001-09-01 18:29:55 +00002848 def itemconfigure(self, index, cnf=None, **kw):
Guido van Rossum09f1ad82001-09-05 19:29:56 +00002849 """Configure resources of an ITEM.
Guido van Rossuma0adb922001-09-01 18:29:55 +00002850
2851 The values for resources are specified as keyword arguments.
2852 To get an overview about the allowed keyword arguments
2853 call the method without arguments.
2854 Valid resource names: background, bg, foreground, fg,
2855 selectbackground, selectforeground."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002856 return self._configure(('itemconfigure', index), cnf, kw)
Guido van Rossuma0adb922001-09-01 18:29:55 +00002857 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00002858
2859class Menu(Widget):
Martin Panterc04fb562016-02-10 05:44:01 +00002860 """Menu widget which allows displaying menu bars, pull-down menus and pop-up menus."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002861 def __init__(self, master=None, cnf={}, **kw):
2862 """Construct menu widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002863
Fredrik Lundh06d28152000-08-09 18:03:12 +00002864 Valid resource names: activebackground, activeborderwidth,
2865 activeforeground, background, bd, bg, borderwidth, cursor,
2866 disabledforeground, fg, font, foreground, postcommand, relief,
2867 selectcolor, takefocus, tearoff, tearoffcommand, title, type."""
2868 Widget.__init__(self, master, 'menu', cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002869 def tk_popup(self, x, y, entry=""):
2870 """Post the menu at position X,Y with entry ENTRY."""
2871 self.tk.call('tk_popup', self._w, x, y, entry)
2872 def activate(self, index):
2873 """Activate entry at INDEX."""
2874 self.tk.call(self._w, 'activate', index)
2875 def add(self, itemType, cnf={}, **kw):
2876 """Internal function."""
2877 self.tk.call((self._w, 'add', itemType) +
2878 self._options(cnf, kw))
2879 def add_cascade(self, cnf={}, **kw):
2880 """Add hierarchical menu item."""
2881 self.add('cascade', cnf or kw)
2882 def add_checkbutton(self, cnf={}, **kw):
2883 """Add checkbutton menu item."""
2884 self.add('checkbutton', cnf or kw)
2885 def add_command(self, cnf={}, **kw):
2886 """Add command menu item."""
2887 self.add('command', cnf or kw)
2888 def add_radiobutton(self, cnf={}, **kw):
2889 """Addd radio menu item."""
2890 self.add('radiobutton', cnf or kw)
2891 def add_separator(self, cnf={}, **kw):
2892 """Add separator."""
2893 self.add('separator', cnf or kw)
2894 def insert(self, index, itemType, cnf={}, **kw):
2895 """Internal function."""
2896 self.tk.call((self._w, 'insert', index, itemType) +
2897 self._options(cnf, kw))
2898 def insert_cascade(self, index, cnf={}, **kw):
2899 """Add hierarchical menu item at INDEX."""
2900 self.insert(index, 'cascade', cnf or kw)
2901 def insert_checkbutton(self, index, cnf={}, **kw):
2902 """Add checkbutton menu item at INDEX."""
2903 self.insert(index, 'checkbutton', cnf or kw)
2904 def insert_command(self, index, cnf={}, **kw):
2905 """Add command menu item at INDEX."""
2906 self.insert(index, 'command', cnf or kw)
2907 def insert_radiobutton(self, index, cnf={}, **kw):
2908 """Addd radio menu item at INDEX."""
2909 self.insert(index, 'radiobutton', cnf or kw)
2910 def insert_separator(self, index, cnf={}, **kw):
2911 """Add separator at INDEX."""
2912 self.insert(index, 'separator', cnf or kw)
2913 def delete(self, index1, index2=None):
Hirokazu Yamamotoa18424c2008-11-04 06:26:27 +00002914 """Delete menu items between INDEX1 and INDEX2 (included)."""
Robert Schuppenies3d1c7de2008-08-10 11:28:17 +00002915 if index2 is None:
2916 index2 = index1
Robert Schuppenies3d1c7de2008-08-10 11:28:17 +00002917
Hirokazu Yamamotoa18424c2008-11-04 06:26:27 +00002918 num_index1, num_index2 = self.index(index1), self.index(index2)
2919 if (num_index1 is None) or (num_index2 is None):
2920 num_index1, num_index2 = 0, -1
2921
2922 for i in range(num_index1, num_index2 + 1):
2923 if 'command' in self.entryconfig(i):
2924 c = str(self.entrycget(i, 'command'))
2925 if c:
2926 self.deletecommand(c)
2927 self.tk.call(self._w, 'delete', index1, index2)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002928 def entrycget(self, index, option):
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002929 """Return the resource value of a menu item for OPTION at INDEX."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002930 return self.tk.call(self._w, 'entrycget', index, '-' + option)
2931 def entryconfigure(self, index, cnf=None, **kw):
2932 """Configure a menu item at INDEX."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002933 return self._configure(('entryconfigure', index), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002934 entryconfig = entryconfigure
2935 def index(self, index):
2936 """Return the index of a menu item identified by INDEX."""
2937 i = self.tk.call(self._w, 'index', index)
2938 if i == 'none': return None
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002939 return self.tk.getint(i)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002940 def invoke(self, index):
2941 """Invoke a menu item identified by INDEX and execute
2942 the associated command."""
2943 return self.tk.call(self._w, 'invoke', index)
2944 def post(self, x, y):
2945 """Display a menu at position X,Y."""
2946 self.tk.call(self._w, 'post', x, y)
2947 def type(self, index):
2948 """Return the type of the menu item at INDEX."""
2949 return self.tk.call(self._w, 'type', index)
2950 def unpost(self):
2951 """Unmap a menu."""
2952 self.tk.call(self._w, 'unpost')
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07002953 def xposition(self, index): # new in Tk 8.5
2954 """Return the x-position of the leftmost pixel of the menu item
2955 at INDEX."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002956 return self.tk.getint(self.tk.call(self._w, 'xposition', index))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002957 def yposition(self, index):
2958 """Return the y-position of the topmost pixel of the menu item at INDEX."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03002959 return self.tk.getint(self.tk.call(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002960 self._w, 'yposition', index))
Guido van Rossum18468821994-06-20 07:49:28 +00002961
2962class Menubutton(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002963 """Menubutton widget, obsolete since Tk8.0."""
2964 def __init__(self, master=None, cnf={}, **kw):
2965 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002966
2967class Message(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002968 """Message widget to display multiline text. Obsolete since Label does it too."""
2969 def __init__(self, master=None, cnf={}, **kw):
2970 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002971
2972class Radiobutton(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002973 """Radiobutton widget which shows only one of several buttons in on-state."""
2974 def __init__(self, master=None, cnf={}, **kw):
2975 """Construct a radiobutton widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002976
Fredrik Lundh06d28152000-08-09 18:03:12 +00002977 Valid resource names: activebackground, activeforeground, anchor,
2978 background, bd, bg, bitmap, borderwidth, command, cursor,
2979 disabledforeground, fg, font, foreground, height,
2980 highlightbackground, highlightcolor, highlightthickness, image,
2981 indicatoron, justify, padx, pady, relief, selectcolor, selectimage,
2982 state, takefocus, text, textvariable, underline, value, variable,
2983 width, wraplength."""
2984 Widget.__init__(self, master, 'radiobutton', cnf, kw)
2985 def deselect(self):
2986 """Put the button in off-state."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002987
Fredrik Lundh06d28152000-08-09 18:03:12 +00002988 self.tk.call(self._w, 'deselect')
2989 def flash(self):
2990 """Flash the button."""
2991 self.tk.call(self._w, 'flash')
2992 def invoke(self):
2993 """Toggle the button and invoke a command if given as resource."""
2994 return self.tk.call(self._w, 'invoke')
2995 def select(self):
2996 """Put the button in on-state."""
2997 self.tk.call(self._w, 'select')
Guido van Rossum18468821994-06-20 07:49:28 +00002998
2999class Scale(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003000 """Scale widget which can display a numerical scale."""
3001 def __init__(self, master=None, cnf={}, **kw):
3002 """Construct a scale widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003003
Fredrik Lundh06d28152000-08-09 18:03:12 +00003004 Valid resource names: activebackground, background, bigincrement, bd,
3005 bg, borderwidth, command, cursor, digits, fg, font, foreground, from,
3006 highlightbackground, highlightcolor, highlightthickness, label,
3007 length, orient, relief, repeatdelay, repeatinterval, resolution,
3008 showvalue, sliderlength, sliderrelief, state, takefocus,
3009 tickinterval, to, troughcolor, variable, width."""
3010 Widget.__init__(self, master, 'scale', cnf, kw)
3011 def get(self):
3012 """Get the current value as integer or float."""
3013 value = self.tk.call(self._w, 'get')
3014 try:
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003015 return self.tk.getint(value)
Serhiy Storchaka32c0d3a2016-10-30 18:49:52 +02003016 except (ValueError, TypeError, TclError):
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003017 return self.tk.getdouble(value)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003018 def set(self, value):
3019 """Set the value to VALUE."""
3020 self.tk.call(self._w, 'set', value)
3021 def coords(self, value=None):
3022 """Return a tuple (X,Y) of the point along the centerline of the
3023 trough that corresponds to VALUE or the current value if None is
3024 given."""
3025
3026 return self._getints(self.tk.call(self._w, 'coords', value))
3027 def identify(self, x, y):
3028 """Return where the point X,Y lies. Valid return values are "slider",
3029 "though1" and "though2"."""
3030 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00003031
3032class Scrollbar(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003033 """Scrollbar widget which displays a slider at a certain position."""
3034 def __init__(self, master=None, cnf={}, **kw):
3035 """Construct a scrollbar widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003036
Fredrik Lundh06d28152000-08-09 18:03:12 +00003037 Valid resource names: activebackground, activerelief,
3038 background, bd, bg, borderwidth, command, cursor,
3039 elementborderwidth, highlightbackground,
3040 highlightcolor, highlightthickness, jump, orient,
3041 relief, repeatdelay, repeatinterval, takefocus,
3042 troughcolor, width."""
3043 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Serhiy Storchaka320f3392014-07-23 22:00:44 +03003044 def activate(self, index=None):
3045 """Marks the element indicated by index as active.
3046 The only index values understood by this method are "arrow1",
3047 "slider", or "arrow2". If any other value is specified then no
3048 element of the scrollbar will be active. If index is not specified,
3049 the method returns the name of the element that is currently active,
3050 or None if no element is active."""
3051 return self.tk.call(self._w, 'activate', index) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00003052 def delta(self, deltax, deltay):
3053 """Return the fractional change of the scrollbar setting if it
3054 would be moved by DELTAX or DELTAY pixels."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003055 return self.tk.getdouble(
Fredrik Lundh06d28152000-08-09 18:03:12 +00003056 self.tk.call(self._w, 'delta', deltax, deltay))
3057 def fraction(self, x, y):
3058 """Return the fractional value which corresponds to a slider
3059 position of X,Y."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003060 return self.tk.getdouble(self.tk.call(self._w, 'fraction', x, y))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003061 def identify(self, x, y):
3062 """Return the element under position X,Y as one of
3063 "arrow1","slider","arrow2" or ""."""
3064 return self.tk.call(self._w, 'identify', x, y)
3065 def get(self):
3066 """Return the current fractional values (upper and lower end)
3067 of the slider position."""
3068 return self._getdoubles(self.tk.call(self._w, 'get'))
Serhiy Storchaka320f3392014-07-23 22:00:44 +03003069 def set(self, first, last):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003070 """Set the fractional values of the slider position (upper and
3071 lower ends as value between 0 and 1)."""
Serhiy Storchaka320f3392014-07-23 22:00:44 +03003072 self.tk.call(self._w, 'set', first, last)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003073
3074
3075
Guilherme Polo1fff0082009-08-14 15:05:30 +00003076class Text(Widget, XView, YView):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003077 """Text widget which can display text in various forms."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00003078 def __init__(self, master=None, cnf={}, **kw):
3079 """Construct a text widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003080
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003081 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003082
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003083 background, borderwidth, cursor,
3084 exportselection, font, foreground,
3085 highlightbackground, highlightcolor,
3086 highlightthickness, insertbackground,
3087 insertborderwidth, insertofftime,
3088 insertontime, insertwidth, padx, pady,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003089 relief, selectbackground,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003090 selectborderwidth, selectforeground,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003091 setgrid, takefocus,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003092 xscrollcommand, yscrollcommand,
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003093
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003094 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003095
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003096 autoseparators, height, maxundo,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003097 spacing1, spacing2, spacing3,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003098 state, tabs, undo, width, wrap,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003099
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003100 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00003101 Widget.__init__(self, master, 'text', cnf, kw)
Serhiy Storchaka4babb912013-11-03 14:34:25 +02003102 def bbox(self, index):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003103 """Return a tuple of (x,y,width,height) which gives the bounding
Serhiy Storchaka4babb912013-11-03 14:34:25 +02003104 box of the visible part of the character at the given index."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00003105 return self._getints(
Serhiy Storchaka4babb912013-11-03 14:34:25 +02003106 self.tk.call(self._w, 'bbox', index)) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00003107 def compare(self, index1, op, index2):
3108 """Return whether between index INDEX1 and index INDEX2 the
3109 relation OP is satisfied. OP is one of <, <=, ==, >=, >, or !=."""
3110 return self.tk.getboolean(self.tk.call(
3111 self._w, 'compare', index1, op, index2))
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07003112 def count(self, index1, index2, *args): # new in Tk 8.5
3113 """Counts the number of relevant things between the two indices.
3114 If index1 is after index2, the result will be a negative number
3115 (and this holds for each of the possible options).
3116
3117 The actual items which are counted depends on the options given by
3118 args. The result is a list of integers, one for the result of each
3119 counting option given. Valid counting options are "chars",
3120 "displaychars", "displayindices", "displaylines", "indices",
3121 "lines", "xpixels" and "ypixels". There is an additional possible
3122 option "update", which if given then all subsequent options ensure
3123 that any possible out of date information is recalculated."""
3124 args = ['-%s' % arg for arg in args if not arg.startswith('-')]
3125 args += [index1, index2]
3126 res = self.tk.call(self._w, 'count', *args) or None
3127 if res is not None and len(args) <= 3:
3128 return (res, )
3129 else:
3130 return res
Fredrik Lundh06d28152000-08-09 18:03:12 +00003131 def debug(self, boolean=None):
3132 """Turn on the internal consistency checks of the B-Tree inside the text
3133 widget according to BOOLEAN."""
Serhiy Storchaka0b9e8152013-11-03 14:29:35 +02003134 if boolean is None:
Serhiy Storchaka2f26c222014-01-11 13:13:46 +02003135 return self.tk.getboolean(self.tk.call(self._w, 'debug'))
Serhiy Storchaka0b9e8152013-11-03 14:29:35 +02003136 self.tk.call(self._w, 'debug', boolean)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003137 def delete(self, index1, index2=None):
3138 """Delete the characters between INDEX1 and INDEX2 (not included)."""
3139 self.tk.call(self._w, 'delete', index1, index2)
3140 def dlineinfo(self, index):
3141 """Return tuple (x,y,width,height,baseline) giving the bounding box
3142 and baseline position of the visible part of the line containing
3143 the character at INDEX."""
3144 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum256705b2002-04-23 13:29:43 +00003145 def dump(self, index1, index2=None, command=None, **kw):
3146 """Return the contents of the widget between index1 and index2.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003147
Guido van Rossum256705b2002-04-23 13:29:43 +00003148 The type of contents returned in filtered based on the keyword
3149 parameters; if 'all', 'image', 'mark', 'tag', 'text', or 'window' are
3150 given and true, then the corresponding items are returned. The result
3151 is a list of triples of the form (key, value, index). If none of the
3152 keywords are true then 'all' is used by default.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003153
Guido van Rossum256705b2002-04-23 13:29:43 +00003154 If the 'command' argument is given, it is called once for each element
3155 of the list of triples, with the values of each triple serving as the
3156 arguments to the function. In this case the list is not returned."""
3157 args = []
3158 func_name = None
3159 result = None
3160 if not command:
3161 # Never call the dump command without the -command flag, since the
3162 # output could involve Tcl quoting and would be a pain to parse
3163 # right. Instead just set the command to build a list of triples
3164 # as if we had done the parsing.
3165 result = []
3166 def append_triple(key, value, index, result=result):
3167 result.append((key, value, index))
3168 command = append_triple
3169 try:
3170 if not isinstance(command, str):
3171 func_name = command = self._register(command)
3172 args += ["-command", command]
3173 for key in kw:
3174 if kw[key]: args.append("-" + key)
3175 args.append(index1)
3176 if index2:
3177 args.append(index2)
3178 self.tk.call(self._w, "dump", *args)
3179 return result
3180 finally:
3181 if func_name:
3182 self.deletecommand(func_name)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003183
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003184 ## new in tk8.4
3185 def edit(self, *args):
3186 """Internal method
Raymond Hettingerff41c482003-04-06 09:01:11 +00003187
3188 This method controls the undo mechanism and
3189 the modified flag. The exact behavior of the
3190 command depends on the option argument that
3191 follows the edit argument. The following forms
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003192 of the command are currently supported:
Raymond Hettingerff41c482003-04-06 09:01:11 +00003193
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003194 edit_modified, edit_redo, edit_reset, edit_separator
3195 and edit_undo
Raymond Hettingerff41c482003-04-06 09:01:11 +00003196
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003197 """
Georg Brandlb533e262008-05-25 18:19:30 +00003198 return self.tk.call(self._w, 'edit', *args)
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003199
3200 def edit_modified(self, arg=None):
3201 """Get or Set the modified flag
Raymond Hettingerff41c482003-04-06 09:01:11 +00003202
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003203 If arg is not specified, returns the modified
Raymond Hettingerff41c482003-04-06 09:01:11 +00003204 flag of the widget. The insert, delete, edit undo and
3205 edit redo commands or the user can set or clear the
3206 modified flag. If boolean is specified, sets the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003207 modified flag of the widget to arg.
3208 """
3209 return self.edit("modified", arg)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003210
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003211 def edit_redo(self):
3212 """Redo the last undone edit
Raymond Hettingerff41c482003-04-06 09:01:11 +00003213
3214 When the undo option is true, reapplies the last
3215 undone edits provided no other edits were done since
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003216 then. Generates an error when the redo stack is empty.
3217 Does nothing when the undo option is false.
3218 """
3219 return self.edit("redo")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003220
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003221 def edit_reset(self):
3222 """Clears the undo and redo stacks
3223 """
3224 return self.edit("reset")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003225
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003226 def edit_separator(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003227 """Inserts a separator (boundary) on the undo stack.
3228
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003229 Does nothing when the undo option is false
3230 """
3231 return self.edit("separator")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003232
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003233 def edit_undo(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003234 """Undoes the last edit action
3235
3236 If the undo option is true. An edit action is defined
3237 as all the insert and delete commands that are recorded
3238 on the undo stack in between two separators. Generates
3239 an error when the undo stack is empty. Does nothing
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003240 when the undo option is false
3241 """
3242 return self.edit("undo")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003243
Fredrik Lundh06d28152000-08-09 18:03:12 +00003244 def get(self, index1, index2=None):
3245 """Return the text from INDEX1 to INDEX2 (not included)."""
3246 return self.tk.call(self._w, 'get', index1, index2)
3247 # (Image commands are new in 8.0)
3248 def image_cget(self, index, option):
3249 """Return the value of OPTION of an embedded image at INDEX."""
3250 if option[:1] != "-":
3251 option = "-" + option
3252 if option[-1:] == "_":
3253 option = option[:-1]
3254 return self.tk.call(self._w, "image", "cget", index, option)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003255 def image_configure(self, index, cnf=None, **kw):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003256 """Configure an embedded image at INDEX."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003257 return self._configure(('image', 'configure', index), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003258 def image_create(self, index, cnf={}, **kw):
3259 """Create an embedded image at INDEX."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00003260 return self.tk.call(
3261 self._w, "image", "create", index,
3262 *self._options(cnf, kw))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003263 def image_names(self):
3264 """Return all names of embedded images in this widget."""
3265 return self.tk.call(self._w, "image", "names")
3266 def index(self, index):
3267 """Return the index in the form line.char for INDEX."""
Christian Heimes57dddfb2008-01-02 18:30:52 +00003268 return str(self.tk.call(self._w, 'index', index))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003269 def insert(self, index, chars, *args):
3270 """Insert CHARS before the characters at INDEX. An additional
3271 tag can be given in ARGS. Additional CHARS and tags can follow in ARGS."""
3272 self.tk.call((self._w, 'insert', index, chars) + args)
3273 def mark_gravity(self, markName, direction=None):
3274 """Change the gravity of a mark MARKNAME to DIRECTION (LEFT or RIGHT).
3275 Return the current value if None is given for DIRECTION."""
3276 return self.tk.call(
3277 (self._w, 'mark', 'gravity', markName, direction))
3278 def mark_names(self):
3279 """Return all mark names."""
3280 return self.tk.splitlist(self.tk.call(
3281 self._w, 'mark', 'names'))
3282 def mark_set(self, markName, index):
3283 """Set mark MARKNAME before the character at INDEX."""
3284 self.tk.call(self._w, 'mark', 'set', markName, index)
3285 def mark_unset(self, *markNames):
3286 """Delete all marks in MARKNAMES."""
3287 self.tk.call((self._w, 'mark', 'unset') + markNames)
3288 def mark_next(self, index):
3289 """Return the name of the next mark after INDEX."""
3290 return self.tk.call(self._w, 'mark', 'next', index) or None
3291 def mark_previous(self, index):
3292 """Return the name of the previous mark before INDEX."""
3293 return self.tk.call(self._w, 'mark', 'previous', index) or None
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07003294 def peer_create(self, newPathName, cnf={}, **kw): # new in Tk 8.5
3295 """Creates a peer text widget with the given newPathName, and any
3296 optional standard configuration options. By default the peer will
Terry Jan Reedy0f847642013-03-11 18:34:00 -04003297 have the same start and end line as the parent widget, but
Martin Pantere26da7c2016-06-02 10:07:09 +00003298 these can be overridden with the standard configuration options."""
Martin v. Löwis5c3c4242012-03-13 13:40:42 -07003299 self.tk.call(self._w, 'peer', 'create', newPathName,
3300 *self._options(cnf, kw))
3301 def peer_names(self): # new in Tk 8.5
3302 """Returns a list of peers of this widget (this does not include
3303 the widget itself)."""
3304 return self.tk.splitlist(self.tk.call(self._w, 'peer', 'names'))
3305 def replace(self, index1, index2, chars, *args): # new in Tk 8.5
3306 """Replaces the range of characters between index1 and index2 with
3307 the given characters and tags specified by args.
3308
3309 See the method insert for some more information about args, and the
3310 method delete for information about the indices."""
3311 self.tk.call(self._w, 'replace', index1, index2, chars, *args)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003312 def scan_mark(self, x, y):
3313 """Remember the current X, Y coordinates."""
3314 self.tk.call(self._w, 'scan', 'mark', x, y)
3315 def scan_dragto(self, x, y):
3316 """Adjust the view of the text to 10 times the
3317 difference between X and Y and the coordinates given in
3318 scan_mark."""
3319 self.tk.call(self._w, 'scan', 'dragto', x, y)
3320 def search(self, pattern, index, stopindex=None,
3321 forwards=None, backwards=None, exact=None,
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 regexp=None, nocase=None, count=None, elide=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003323 """Search PATTERN beginning from INDEX until STOPINDEX.
Guilherme Poloae098992009-02-09 16:44:24 +00003324 Return the index of the first character of a match or an
3325 empty string."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00003326 args = [self._w, 'search']
3327 if forwards: args.append('-forwards')
3328 if backwards: args.append('-backwards')
3329 if exact: args.append('-exact')
3330 if regexp: args.append('-regexp')
3331 if nocase: args.append('-nocase')
Thomas Wouters89f507f2006-12-13 04:49:30 +00003332 if elide: args.append('-elide')
Fredrik Lundh06d28152000-08-09 18:03:12 +00003333 if count: args.append('-count'); args.append(count)
Guilherme Poloae098992009-02-09 16:44:24 +00003334 if pattern and pattern[0] == '-': args.append('--')
Fredrik Lundh06d28152000-08-09 18:03:12 +00003335 args.append(pattern)
3336 args.append(index)
3337 if stopindex: args.append(stopindex)
Guilherme Polo56f5be52009-03-07 01:54:57 +00003338 return str(self.tk.call(tuple(args)))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003339 def see(self, index):
3340 """Scroll such that the character at INDEX is visible."""
3341 self.tk.call(self._w, 'see', index)
3342 def tag_add(self, tagName, index1, *args):
3343 """Add tag TAGNAME to all characters between INDEX1 and index2 in ARGS.
3344 Additional pairs of indices may follow in ARGS."""
3345 self.tk.call(
3346 (self._w, 'tag', 'add', tagName, index1) + args)
3347 def tag_unbind(self, tagName, sequence, funcid=None):
3348 """Unbind for all characters with TAGNAME for event SEQUENCE the
3349 function identified with FUNCID."""
3350 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
3351 if funcid:
3352 self.deletecommand(funcid)
3353 def tag_bind(self, tagName, sequence, func, add=None):
3354 """Bind to all characters with TAGNAME at event SEQUENCE a call to function FUNC.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003355
Fredrik Lundh06d28152000-08-09 18:03:12 +00003356 An additional boolean parameter ADD specifies whether FUNC will be
3357 called additionally to the other bound function or whether it will
3358 replace the previous function. See bind for the return value."""
3359 return self._bind((self._w, 'tag', 'bind', tagName),
3360 sequence, func, add)
3361 def tag_cget(self, tagName, option):
3362 """Return the value of OPTION for tag TAGNAME."""
3363 if option[:1] != '-':
3364 option = '-' + option
3365 if option[-1:] == '_':
3366 option = option[:-1]
3367 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003368 def tag_configure(self, tagName, cnf=None, **kw):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003369 """Configure a tag TAGNAME."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003370 return self._configure(('tag', 'configure', tagName), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003371 tag_config = tag_configure
3372 def tag_delete(self, *tagNames):
3373 """Delete all tags in TAGNAMES."""
3374 self.tk.call((self._w, 'tag', 'delete') + tagNames)
3375 def tag_lower(self, tagName, belowThis=None):
3376 """Change the priority of tag TAGNAME such that it is lower
3377 than the priority of BELOWTHIS."""
3378 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
3379 def tag_names(self, index=None):
3380 """Return a list of all tag names."""
3381 return self.tk.splitlist(
3382 self.tk.call(self._w, 'tag', 'names', index))
3383 def tag_nextrange(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 forward from INDEX1."""
3387 return self.tk.splitlist(self.tk.call(
3388 self._w, 'tag', 'nextrange', tagName, index1, index2))
3389 def tag_prevrange(self, tagName, index1, index2=None):
3390 """Return a list of start and end index for the first sequence of
3391 characters between INDEX1 and INDEX2 which all have tag TAGNAME.
3392 The text is searched backwards from INDEX1."""
3393 return self.tk.splitlist(self.tk.call(
3394 self._w, 'tag', 'prevrange', tagName, index1, index2))
3395 def tag_raise(self, tagName, aboveThis=None):
3396 """Change the priority of tag TAGNAME such that it is higher
3397 than the priority of ABOVETHIS."""
3398 self.tk.call(
3399 self._w, 'tag', 'raise', tagName, aboveThis)
3400 def tag_ranges(self, tagName):
3401 """Return a list of ranges of text which have tag TAGNAME."""
3402 return self.tk.splitlist(self.tk.call(
3403 self._w, 'tag', 'ranges', tagName))
3404 def tag_remove(self, tagName, index1, index2=None):
3405 """Remove tag TAGNAME from all characters between INDEX1 and INDEX2."""
3406 self.tk.call(
3407 self._w, 'tag', 'remove', tagName, index1, index2)
3408 def window_cget(self, index, option):
3409 """Return the value of OPTION of an embedded window at INDEX."""
3410 if option[:1] != '-':
3411 option = '-' + option
3412 if option[-1:] == '_':
3413 option = option[:-1]
3414 return self.tk.call(self._w, 'window', 'cget', index, option)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003415 def window_configure(self, index, cnf=None, **kw):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003416 """Configure an embedded window at INDEX."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003417 return self._configure(('window', 'configure', index), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003418 window_config = window_configure
3419 def window_create(self, index, cnf={}, **kw):
3420 """Create a window at INDEX."""
3421 self.tk.call(
3422 (self._w, 'window', 'create', index)
3423 + self._options(cnf, kw))
3424 def window_names(self):
3425 """Return all names of embedded windows in this widget."""
3426 return self.tk.splitlist(
3427 self.tk.call(self._w, 'window', 'names'))
Fredrik Lundh06d28152000-08-09 18:03:12 +00003428 def yview_pickplace(self, *what):
3429 """Obsolete function, use see."""
3430 self.tk.call((self._w, 'yview', '-pickplace') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00003431
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003432
Guido van Rossum28574b51996-10-21 15:16:51 +00003433class _setit:
Fredrik Lundh06d28152000-08-09 18:03:12 +00003434 """Internal class. It wraps the command in the widget OptionMenu."""
3435 def __init__(self, var, value, callback=None):
3436 self.__value = value
3437 self.__var = var
3438 self.__callback = callback
3439 def __call__(self, *args):
3440 self.__var.set(self.__value)
3441 if self.__callback:
Raymond Hettingerff41c482003-04-06 09:01:11 +00003442 self.__callback(self.__value, *args)
Guido van Rossum28574b51996-10-21 15:16:51 +00003443
3444class OptionMenu(Menubutton):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003445 """OptionMenu which allows the user to select a value from a menu."""
3446 def __init__(self, master, variable, value, *values, **kwargs):
3447 """Construct an optionmenu widget with the parent MASTER, with
3448 the resource textvariable set to VARIABLE, the initially selected
3449 value VALUE, the other menu values VALUES and an additional
3450 keyword argument command."""
3451 kw = {"borderwidth": 2, "textvariable": variable,
3452 "indicatoron": 1, "relief": RAISED, "anchor": "c",
3453 "highlightthickness": 2}
3454 Widget.__init__(self, master, "menubutton", kw)
3455 self.widgetName = 'tk_optionMenu'
3456 menu = self.__menu = Menu(self, name="menu", tearoff=0)
3457 self.menuname = menu._w
3458 # 'command' is the only supported keyword
3459 callback = kwargs.get('command')
Guido van Rossume014a132006-08-19 16:53:45 +00003460 if 'command' in kwargs:
Fredrik Lundh06d28152000-08-09 18:03:12 +00003461 del kwargs['command']
3462 if kwargs:
Collin Winterce36ad82007-08-30 01:19:48 +00003463 raise TclError('unknown option -'+kwargs.keys()[0])
Fredrik Lundh06d28152000-08-09 18:03:12 +00003464 menu.add_command(label=value,
3465 command=_setit(variable, value, callback))
3466 for v in values:
3467 menu.add_command(label=v,
3468 command=_setit(variable, v, callback))
3469 self["menu"] = menu
Guido van Rossum28574b51996-10-21 15:16:51 +00003470
Fredrik Lundh06d28152000-08-09 18:03:12 +00003471 def __getitem__(self, name):
3472 if name == 'menu':
3473 return self.__menu
3474 return Widget.__getitem__(self, name)
Guido van Rossum28574b51996-10-21 15:16:51 +00003475
Fredrik Lundh06d28152000-08-09 18:03:12 +00003476 def destroy(self):
3477 """Destroy this widget and the associated menu."""
3478 Menubutton.destroy(self)
3479 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00003480
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003481class Image:
Fredrik Lundh06d28152000-08-09 18:03:12 +00003482 """Base class for images."""
Martin v. Löwis0d8ce612000-09-08 16:28:30 +00003483 _last_id = 0
Fredrik Lundh06d28152000-08-09 18:03:12 +00003484 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
3485 self.name = None
3486 if not master:
3487 master = _default_root
3488 if not master:
Collin Winterce36ad82007-08-30 01:19:48 +00003489 raise RuntimeError('Too early to create image')
Serhiy Storchakad00aff22014-08-24 09:07:47 +03003490 self.tk = getattr(master, 'tk', master)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003491 if not name:
Martin v. Löwis0d8ce612000-09-08 16:28:30 +00003492 Image._last_id += 1
Walter Dörwald70a6b492004-02-12 17:35:32 +00003493 name = "pyimage%r" % (Image._last_id,) # tk itself would use image<x>
Fredrik Lundh06d28152000-08-09 18:03:12 +00003494 if kw and cnf: cnf = _cnfmerge((cnf, kw))
3495 elif kw: cnf = kw
3496 options = ()
3497 for k, v in cnf.items():
Florent Xicluna5d1155c2011-10-28 14:45:05 +02003498 if callable(v):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003499 v = self._register(v)
3500 options = options + ('-'+k, v)
3501 self.tk.call(('image', 'create', imgtype, name,) + options)
3502 self.name = name
3503 def __str__(self): return self.name
3504 def __del__(self):
3505 if self.name:
3506 try:
3507 self.tk.call('image', 'delete', self.name)
3508 except TclError:
3509 # May happen if the root was destroyed
3510 pass
3511 def __setitem__(self, key, value):
3512 self.tk.call(self.name, 'configure', '-'+key, value)
3513 def __getitem__(self, key):
3514 return self.tk.call(self.name, 'configure', '-'+key)
3515 def configure(self, **kw):
3516 """Configure the image."""
3517 res = ()
3518 for k, v in _cnfmerge(kw).items():
3519 if v is not None:
3520 if k[-1] == '_': k = k[:-1]
Florent Xicluna5d1155c2011-10-28 14:45:05 +02003521 if callable(v):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003522 v = self._register(v)
3523 res = res + ('-'+k, v)
3524 self.tk.call((self.name, 'config') + res)
3525 config = configure
3526 def height(self):
3527 """Return the height of the image."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003528 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00003529 self.tk.call('image', 'height', self.name))
3530 def type(self):
Vijay Kumar128641d2017-05-22 21:53:52 +05303531 """Return the type of the image, e.g. "photo" or "bitmap"."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00003532 return self.tk.call('image', 'type', self.name)
3533 def width(self):
3534 """Return the width of the image."""
Serhiy Storchaka645058d2015-05-06 14:00:04 +03003535 return self.tk.getint(
Fredrik Lundh06d28152000-08-09 18:03:12 +00003536 self.tk.call('image', 'width', self.name))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003537
3538class PhotoImage(Image):
Andrés Delfino4b685bf2018-04-17 02:34:35 -03003539 """Widget which can display images in PGM, PPM, GIF, PNG format."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00003540 def __init__(self, name=None, cnf={}, master=None, **kw):
3541 """Create an image with NAME.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003542
Fredrik Lundh06d28152000-08-09 18:03:12 +00003543 Valid resource names: data, format, file, gamma, height, palette,
3544 width."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00003545 Image.__init__(self, 'photo', name, cnf, master, **kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003546 def blank(self):
3547 """Display a transparent image."""
3548 self.tk.call(self.name, 'blank')
3549 def cget(self, option):
3550 """Return the value of OPTION."""
3551 return self.tk.call(self.name, 'cget', '-' + option)
3552 # XXX config
3553 def __getitem__(self, key):
3554 return self.tk.call(self.name, 'cget', '-' + key)
3555 # XXX copy -from, -to, ...?
3556 def copy(self):
3557 """Return a new PhotoImage with the same image as this widget."""
Serhiy Storchakad00aff22014-08-24 09:07:47 +03003558 destImage = PhotoImage(master=self.tk)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003559 self.tk.call(destImage, 'copy', self.name)
3560 return destImage
Terry Jan Reedyca0250a2016-03-11 15:30:35 -05003561 def zoom(self, x, y=''):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003562 """Return a new PhotoImage with the same image as this widget
Terry Jan Reedyca0250a2016-03-11 15:30:35 -05003563 but zoom it with a factor of x in the X direction and y in the Y
3564 direction. If y is not given, the default value is the same as x.
3565 """
Serhiy Storchakad00aff22014-08-24 09:07:47 +03003566 destImage = PhotoImage(master=self.tk)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003567 if y=='': y=x
3568 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
3569 return destImage
Terry Jan Reedyca0250a2016-03-11 15:30:35 -05003570 def subsample(self, x, y=''):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003571 """Return a new PhotoImage based on the same image as this widget
Terry Jan Reedyca0250a2016-03-11 15:30:35 -05003572 but use only every Xth or Yth pixel. If y is not given, the
3573 default value is the same as x.
3574 """
Serhiy Storchakad00aff22014-08-24 09:07:47 +03003575 destImage = PhotoImage(master=self.tk)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003576 if y=='': y=x
3577 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
3578 return destImage
3579 def get(self, x, y):
3580 """Return the color (red, green, blue) of the pixel at X,Y."""
3581 return self.tk.call(self.name, 'get', x, y)
3582 def put(self, data, to=None):
Mark Dickinson934896d2009-02-21 20:59:32 +00003583 """Put row formatted colors to image starting from
Fredrik Lundh06d28152000-08-09 18:03:12 +00003584 position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))"""
3585 args = (self.name, 'put', data)
3586 if to:
3587 if to[0] == '-to':
3588 to = to[1:]
3589 args = args + ('-to',) + tuple(to)
3590 self.tk.call(args)
3591 # XXX read
3592 def write(self, filename, format=None, from_coords=None):
3593 """Write image to file FILENAME in FORMAT starting from
3594 position FROM_COORDS."""
3595 args = (self.name, 'write', filename)
3596 if format:
3597 args = args + ('-format', format)
3598 if from_coords:
3599 args = args + ('-from',) + tuple(from_coords)
3600 self.tk.call(args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003601
3602class BitmapImage(Image):
Andrés Delfino4b685bf2018-04-17 02:34:35 -03003603 """Widget which can display images in XBM format."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00003604 def __init__(self, name=None, cnf={}, master=None, **kw):
3605 """Create a bitmap with NAME.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003606
Fredrik Lundh06d28152000-08-09 18:03:12 +00003607 Valid resource names: background, data, file, foreground, maskdata, maskfile."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00003608 Image.__init__(self, 'bitmap', name, cnf, master, **kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003609
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02003610def image_names():
3611 return _default_root.tk.splitlist(_default_root.tk.call('image', 'names'))
3612
3613def image_types():
3614 return _default_root.tk.splitlist(_default_root.tk.call('image', 'types'))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003615
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003616
Guilherme Polo1fff0082009-08-14 15:05:30 +00003617class Spinbox(Widget, XView):
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003618 """spinbox widget."""
3619 def __init__(self, master=None, cnf={}, **kw):
3620 """Construct a spinbox widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003621
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003622 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003623
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003624 activebackground, background, borderwidth,
3625 cursor, exportselection, font, foreground,
3626 highlightbackground, highlightcolor,
3627 highlightthickness, insertbackground,
3628 insertborderwidth, insertofftime,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003629 insertontime, insertwidth, justify, relief,
3630 repeatdelay, repeatinterval,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003631 selectbackground, selectborderwidth
3632 selectforeground, takefocus, textvariable
3633 xscrollcommand.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003634
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003635 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003636
3637 buttonbackground, buttoncursor,
3638 buttondownrelief, buttonuprelief,
3639 command, disabledbackground,
3640 disabledforeground, format, from,
3641 invalidcommand, increment,
3642 readonlybackground, state, to,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003643 validate, validatecommand values,
3644 width, wrap,
3645 """
3646 Widget.__init__(self, master, 'spinbox', cnf, kw)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003647
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003648 def bbox(self, index):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003649 """Return a tuple of X1,Y1,X2,Y2 coordinates for a
3650 rectangle which encloses the character given by index.
3651
3652 The first two elements of the list give the x and y
3653 coordinates of the upper-left corner of the screen
3654 area covered by the character (in pixels relative
3655 to the widget) and the last two elements give the
3656 width and height of the character, in pixels. The
3657 bounding box may refer to a region outside the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003658 visible area of the window.
3659 """
Serhiy Storchaka2849e0d2013-11-03 14:13:34 +02003660 return self._getints(self.tk.call(self._w, 'bbox', index)) or None
Raymond Hettingerff41c482003-04-06 09:01:11 +00003661
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003662 def delete(self, first, last=None):
3663 """Delete one or more elements of the spinbox.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003664
3665 First is the index of the first character to delete,
3666 and last is the index of the character just after
3667 the last one to delete. If last isn't specified it
3668 defaults to first+1, i.e. a single character is
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003669 deleted. This command returns an empty string.
3670 """
3671 return self.tk.call(self._w, 'delete', first, last)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003672
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003673 def get(self):
3674 """Returns the spinbox's string"""
3675 return self.tk.call(self._w, 'get')
Raymond Hettingerff41c482003-04-06 09:01:11 +00003676
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003677 def icursor(self, index):
3678 """Alter the position of the insertion cursor.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003679
3680 The insertion cursor will be displayed just before
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003681 the character given by index. Returns an empty string
3682 """
3683 return self.tk.call(self._w, 'icursor', index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003684
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003685 def identify(self, x, y):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003686 """Returns the name of the widget at position x, y
3687
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003688 Return value is one of: none, buttondown, buttonup, entry
3689 """
3690 return self.tk.call(self._w, 'identify', x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003691
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003692 def index(self, index):
3693 """Returns the numerical index corresponding to index
3694 """
3695 return self.tk.call(self._w, 'index', index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003696
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003697 def insert(self, index, s):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003698 """Insert string s at index
3699
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003700 Returns an empty string.
3701 """
3702 return self.tk.call(self._w, 'insert', index, s)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003703
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003704 def invoke(self, element):
3705 """Causes the specified element to be invoked
Raymond Hettingerff41c482003-04-06 09:01:11 +00003706
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003707 The element could be buttondown or buttonup
3708 triggering the action associated with it.
3709 """
3710 return self.tk.call(self._w, 'invoke', element)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003711
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003712 def scan(self, *args):
3713 """Internal function."""
3714 return self._getints(
3715 self.tk.call((self._w, 'scan') + args)) or ()
Raymond Hettingerff41c482003-04-06 09:01:11 +00003716
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003717 def scan_mark(self, x):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003718 """Records x and the current view in the spinbox window;
3719
3720 used in conjunction with later scan dragto commands.
3721 Typically this command is associated with a mouse button
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003722 press in the widget. It returns an empty string.
3723 """
3724 return self.scan("mark", x)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003725
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003726 def scan_dragto(self, x):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003727 """Compute the difference between the given x argument
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003728 and the x argument to the last scan mark command
Raymond Hettingerff41c482003-04-06 09:01:11 +00003729
3730 It then adjusts the view left or right by 10 times the
3731 difference in x-coordinates. This command is typically
3732 associated with mouse motion events in the widget, to
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003733 produce the effect of dragging the spinbox at high speed
3734 through the window. The return value is an empty string.
3735 """
3736 return self.scan("dragto", x)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003737
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003738 def selection(self, *args):
3739 """Internal function."""
3740 return self._getints(
3741 self.tk.call((self._w, 'selection') + args)) or ()
Raymond Hettingerff41c482003-04-06 09:01:11 +00003742
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003743 def selection_adjust(self, index):
3744 """Locate the end of the selection nearest to the character
Raymond Hettingerff41c482003-04-06 09:01:11 +00003745 given by index,
3746
3747 Then adjust that end of the selection to be at index
3748 (i.e including but not going beyond index). The other
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003749 end of the selection is made the anchor point for future
Raymond Hettingerff41c482003-04-06 09:01:11 +00003750 select to commands. If the selection isn't currently in
3751 the spinbox, then a new selection is created to include
3752 the characters between index and the most recent selection
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003753 anchor point, inclusive. Returns an empty string.
3754 """
3755 return self.selection("adjust", index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003756
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003757 def selection_clear(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003758 """Clear the selection
3759
3760 If the selection isn't in this widget then the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003761 command has no effect. Returns an empty string.
3762 """
3763 return self.selection("clear")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003764
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003765 def selection_element(self, element=None):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003766 """Sets or gets the currently selected element.
3767
3768 If a spinbutton element is specified, it will be
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003769 displayed depressed
3770 """
3771 return self.selection("element", element)
3772
3773###########################################################################
3774
3775class LabelFrame(Widget):
3776 """labelframe widget."""
3777 def __init__(self, master=None, cnf={}, **kw):
3778 """Construct a labelframe widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003779
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003780 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003781
3782 borderwidth, cursor, font, foreground,
3783 highlightbackground, highlightcolor,
3784 highlightthickness, padx, pady, relief,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003785 takefocus, text
Raymond Hettingerff41c482003-04-06 09:01:11 +00003786
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003787 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003788
3789 background, class, colormap, container,
3790 height, labelanchor, labelwidget,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003791 visual, width
3792 """
3793 Widget.__init__(self, master, 'labelframe', cnf, kw)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003794
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003795########################################################################
3796
3797class PanedWindow(Widget):
3798 """panedwindow widget."""
3799 def __init__(self, master=None, cnf={}, **kw):
3800 """Construct a panedwindow widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003801
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003802 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003803
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003804 background, borderwidth, cursor, height,
3805 orient, relief, width
Raymond Hettingerff41c482003-04-06 09:01:11 +00003806
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003807 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003808
3809 handlepad, handlesize, opaqueresize,
3810 sashcursor, sashpad, sashrelief,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003811 sashwidth, showhandle,
3812 """
3813 Widget.__init__(self, master, 'panedwindow', cnf, kw)
3814
3815 def add(self, child, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003816 """Add a child widget to the panedwindow in a new pane.
3817
3818 The child argument is the name of the child widget
3819 followed by pairs of arguments that specify how to
Guilherme Polo86425562009-05-31 21:35:23 +00003820 manage the windows. The possible options and values
3821 are the ones accepted by the paneconfigure method.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003822 """
3823 self.tk.call((self._w, 'add', child) + self._options(kw))
Raymond Hettingerff41c482003-04-06 09:01:11 +00003824
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003825 def remove(self, child):
3826 """Remove the pane containing child from the panedwindow
Raymond Hettingerff41c482003-04-06 09:01:11 +00003827
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003828 All geometry management options for child will be forgotten.
3829 """
3830 self.tk.call(self._w, 'forget', child)
3831 forget=remove
Raymond Hettingerff41c482003-04-06 09:01:11 +00003832
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003833 def identify(self, x, y):
3834 """Identify the panedwindow component at point x, y
Raymond Hettingerff41c482003-04-06 09:01:11 +00003835
3836 If the point is over a sash or a sash handle, the result
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003837 is a two element list containing the index of the sash or
Raymond Hettingerff41c482003-04-06 09:01:11 +00003838 handle, and a word indicating whether it is over a sash
3839 or a handle, such as {0 sash} or {2 handle}. If the point
3840 is over any other part of the panedwindow, the result is
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003841 an empty list.
3842 """
3843 return self.tk.call(self._w, 'identify', x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003844
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003845 def proxy(self, *args):
3846 """Internal function."""
3847 return self._getints(
Raymond Hettingerff41c482003-04-06 09:01:11 +00003848 self.tk.call((self._w, 'proxy') + args)) or ()
3849
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003850 def proxy_coord(self):
3851 """Return the x and y pair of the most recent proxy location
3852 """
3853 return self.proxy("coord")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003854
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003855 def proxy_forget(self):
3856 """Remove the proxy from the display.
3857 """
3858 return self.proxy("forget")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003859
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003860 def proxy_place(self, x, y):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003861 """Place the proxy at the given x and y coordinates.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003862 """
3863 return self.proxy("place", x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003864
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003865 def sash(self, *args):
3866 """Internal function."""
3867 return self._getints(
3868 self.tk.call((self._w, 'sash') + args)) or ()
Raymond Hettingerff41c482003-04-06 09:01:11 +00003869
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003870 def sash_coord(self, index):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003871 """Return the current x and y pair for the sash given by index.
3872
3873 Index must be an integer between 0 and 1 less than the
3874 number of panes in the panedwindow. The coordinates given are
3875 those of the top left corner of the region containing the sash.
3876 pathName sash dragto index x y This command computes the
3877 difference between the given coordinates and the coordinates
3878 given to the last sash coord command for the given sash. It then
3879 moves that sash the computed difference. The return value is the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003880 empty string.
3881 """
3882 return self.sash("coord", index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003883
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003884 def sash_mark(self, index):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003885 """Records x and y for the sash given by index;
3886
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003887 Used in conjunction with later dragto commands to move the sash.
3888 """
3889 return self.sash("mark", index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003890
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003891 def sash_place(self, index, x, y):
3892 """Place the sash given by index at the given coordinates
3893 """
3894 return self.sash("place", index, x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003895
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003896 def panecget(self, child, option):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003897 """Query a management option for window.
3898
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003899 Option may be any value allowed by the paneconfigure subcommand
3900 """
3901 return self.tk.call(
3902 (self._w, 'panecget') + (child, '-'+option))
Raymond Hettingerff41c482003-04-06 09:01:11 +00003903
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003904 def paneconfigure(self, tagOrId, cnf=None, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003905 """Query or modify the management options for window.
3906
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003907 If no option is specified, returns a list describing all
Raymond Hettingerff41c482003-04-06 09:01:11 +00003908 of the available options for pathName. If option is
3909 specified with no value, then the command returns a list
3910 describing the one named option (this list will be identical
3911 to the corresponding sublist of the value returned if no
3912 option is specified). If one or more option-value pairs are
3913 specified, then the command modifies the given widget
3914 option(s) to have the given value(s); in this case the
3915 command returns an empty string. The following options
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003916 are supported:
Raymond Hettingerff41c482003-04-06 09:01:11 +00003917
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003918 after window
Raymond Hettingerff41c482003-04-06 09:01:11 +00003919 Insert the window after the window specified. window
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003920 should be the name of a window already managed by pathName.
3921 before window
Raymond Hettingerff41c482003-04-06 09:01:11 +00003922 Insert the window before the window specified. window
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003923 should be the name of a window already managed by pathName.
3924 height size
Raymond Hettingerff41c482003-04-06 09:01:11 +00003925 Specify a height for the window. The height will be the
3926 outer dimension of the window including its border, if
3927 any. If size is an empty string, or if -height is not
3928 specified, then the height requested internally by the
3929 window will be used initially; the height may later be
3930 adjusted by the movement of sashes in the panedwindow.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003931 Size may be any value accepted by Tk_GetPixels.
3932 minsize n
Raymond Hettingerff41c482003-04-06 09:01:11 +00003933 Specifies that the size of the window cannot be made
3934 less than n. This constraint only affects the size of
3935 the widget in the paned dimension -- the x dimension
3936 for horizontal panedwindows, the y dimension for
3937 vertical panedwindows. May be any value accepted by
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003938 Tk_GetPixels.
3939 padx n
Raymond Hettingerff41c482003-04-06 09:01:11 +00003940 Specifies a non-negative value indicating how much
3941 extra space to leave on each side of the window in
3942 the X-direction. The value may have any of the forms
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003943 accepted by Tk_GetPixels.
3944 pady n
3945 Specifies a non-negative value indicating how much
Raymond Hettingerff41c482003-04-06 09:01:11 +00003946 extra space to leave on each side of the window in
3947 the Y-direction. The value may have any of the forms
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003948 accepted by Tk_GetPixels.
3949 sticky style
Raymond Hettingerff41c482003-04-06 09:01:11 +00003950 If a window's pane is larger than the requested
3951 dimensions of the window, this option may be used
3952 to position (or stretch) the window within its pane.
3953 Style is a string that contains zero or more of the
3954 characters n, s, e or w. The string can optionally
3955 contains spaces or commas, but they are ignored. Each
3956 letter refers to a side (north, south, east, or west)
3957 that the window will "stick" to. If both n and s
3958 (or e and w) are specified, the window will be
3959 stretched to fill the entire height (or width) of
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003960 its cavity.
3961 width size
Raymond Hettingerff41c482003-04-06 09:01:11 +00003962 Specify a width for the window. The width will be
3963 the outer dimension of the window including its
3964 border, if any. If size is an empty string, or
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003965 if -width is not specified, then the width requested
Raymond Hettingerff41c482003-04-06 09:01:11 +00003966 internally by the window will be used initially; the
3967 width may later be adjusted by the movement of sashes
3968 in the panedwindow. Size may be any value accepted by
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003969 Tk_GetPixels.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003970
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003971 """
3972 if cnf is None and not kw:
Serhiy Storchaka848972c2013-12-25 16:35:38 +02003973 return self._getconfigure(self._w, 'paneconfigure', tagOrId)
Guido van Rossum13257902007-06-07 23:15:56 +00003974 if isinstance(cnf, str) and not kw:
Serhiy Storchaka848972c2013-12-25 16:35:38 +02003975 return self._getconfigure1(
3976 self._w, 'paneconfigure', tagOrId, '-'+cnf)
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003977 self.tk.call((self._w, 'paneconfigure', tagOrId) +
3978 self._options(cnf, kw))
3979 paneconfig = paneconfigure
3980
3981 def panes(self):
3982 """Returns an ordered list of the child panes."""
Serhiy Storchakaa21acb52014-01-07 19:27:42 +02003983 return self.tk.splitlist(self.tk.call(self._w, 'panes'))
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003984
Guido van Rossumc417ef81996-08-21 23:38:59 +00003985# Test:
3986
3987def _test():
Fredrik Lundh06d28152000-08-09 18:03:12 +00003988 root = Tk()
3989 text = "This is Tcl/Tk version %s" % TclVersion
Serhiy Storchaka6716d602014-07-30 19:19:21 +03003990 text += "\nThis should be a cedilla: \xe7"
Fredrik Lundh06d28152000-08-09 18:03:12 +00003991 label = Label(root, text=text)
3992 label.pack()
3993 test = Button(root, text="Click me!",
3994 command=lambda root=root: root.test.configure(
3995 text="[%s]" % root.test['text']))
3996 test.pack()
3997 root.test = test
3998 quit = Button(root, text="QUIT", command=root.destroy)
3999 quit.pack()
4000 # The following three commands are needed so the window pops
4001 # up on top on Windows...
4002 root.iconify()
4003 root.update()
4004 root.deiconify()
4005 root.mainloop()
Guido van Rossumc417ef81996-08-21 23:38:59 +00004006
4007if __name__ == '__main__':
Fredrik Lundh06d28152000-08-09 18:03:12 +00004008 _test()