1 | n/a | """Ttk wrapper. |
---|
2 | n/a | |
---|
3 | n/a | This module provides classes to allow using Tk themed widget set. |
---|
4 | n/a | |
---|
5 | n/a | Ttk is based on a revised and enhanced version of |
---|
6 | n/a | TIP #48 (http://tip.tcl.tk/48) specified style engine. |
---|
7 | n/a | |
---|
8 | n/a | Its basic idea is to separate, to the extent possible, the code |
---|
9 | n/a | implementing a widget's behavior from the code implementing its |
---|
10 | n/a | appearance. Widget class bindings are primarily responsible for |
---|
11 | n/a | maintaining the widget state and invoking callbacks, all aspects |
---|
12 | n/a | of the widgets appearance lies at Themes. |
---|
13 | n/a | """ |
---|
14 | n/a | |
---|
15 | n/a | __version__ = "0.3.1" |
---|
16 | n/a | |
---|
17 | n/a | __author__ = "Guilherme Polo <ggpolo@gmail.com>" |
---|
18 | n/a | |
---|
19 | n/a | __all__ = ["Button", "Checkbutton", "Combobox", "Entry", "Frame", "Label", |
---|
20 | n/a | "Labelframe", "LabelFrame", "Menubutton", "Notebook", "Panedwindow", |
---|
21 | n/a | "PanedWindow", "Progressbar", "Radiobutton", "Scale", "Scrollbar", |
---|
22 | n/a | "Separator", "Sizegrip", "Style", "Treeview", |
---|
23 | n/a | # Extensions |
---|
24 | n/a | "LabeledScale", "OptionMenu", |
---|
25 | n/a | # functions |
---|
26 | n/a | "tclobjs_to_py", "setup_master"] |
---|
27 | n/a | |
---|
28 | n/a | import tkinter |
---|
29 | n/a | from tkinter import _flatten, _join, _stringify, _splitdict |
---|
30 | n/a | |
---|
31 | n/a | _sentinel = object() |
---|
32 | n/a | |
---|
33 | n/a | # Verify if Tk is new enough to not need the Tile package |
---|
34 | n/a | _REQUIRE_TILE = True if tkinter.TkVersion < 8.5 else False |
---|
35 | n/a | |
---|
36 | n/a | def _load_tile(master): |
---|
37 | n/a | if _REQUIRE_TILE: |
---|
38 | n/a | import os |
---|
39 | n/a | tilelib = os.environ.get('TILE_LIBRARY') |
---|
40 | n/a | if tilelib: |
---|
41 | n/a | # append custom tile path to the list of directories that |
---|
42 | n/a | # Tcl uses when attempting to resolve packages with the package |
---|
43 | n/a | # command |
---|
44 | n/a | master.tk.eval( |
---|
45 | n/a | 'global auto_path; ' |
---|
46 | n/a | 'lappend auto_path {%s}' % tilelib) |
---|
47 | n/a | |
---|
48 | n/a | master.tk.eval('package require tile') # TclError may be raised here |
---|
49 | n/a | master._tile_loaded = True |
---|
50 | n/a | |
---|
51 | n/a | def _format_optvalue(value, script=False): |
---|
52 | n/a | """Internal function.""" |
---|
53 | n/a | if script: |
---|
54 | n/a | # if caller passes a Tcl script to tk.call, all the values need to |
---|
55 | n/a | # be grouped into words (arguments to a command in Tcl dialect) |
---|
56 | n/a | value = _stringify(value) |
---|
57 | n/a | elif isinstance(value, (list, tuple)): |
---|
58 | n/a | value = _join(value) |
---|
59 | n/a | return value |
---|
60 | n/a | |
---|
61 | n/a | def _format_optdict(optdict, script=False, ignore=None): |
---|
62 | n/a | """Formats optdict to a tuple to pass it to tk.call. |
---|
63 | n/a | |
---|
64 | n/a | E.g. (script=False): |
---|
65 | n/a | {'foreground': 'blue', 'padding': [1, 2, 3, 4]} returns: |
---|
66 | n/a | ('-foreground', 'blue', '-padding', '1 2 3 4')""" |
---|
67 | n/a | |
---|
68 | n/a | opts = [] |
---|
69 | n/a | for opt, value in optdict.items(): |
---|
70 | n/a | if not ignore or opt not in ignore: |
---|
71 | n/a | opts.append("-%s" % opt) |
---|
72 | n/a | if value is not None: |
---|
73 | n/a | opts.append(_format_optvalue(value, script)) |
---|
74 | n/a | |
---|
75 | n/a | return _flatten(opts) |
---|
76 | n/a | |
---|
77 | n/a | def _mapdict_values(items): |
---|
78 | n/a | # each value in mapdict is expected to be a sequence, where each item |
---|
79 | n/a | # is another sequence containing a state (or several) and a value |
---|
80 | n/a | # E.g. (script=False): |
---|
81 | n/a | # [('active', 'selected', 'grey'), ('focus', [1, 2, 3, 4])] |
---|
82 | n/a | # returns: |
---|
83 | n/a | # ['active selected', 'grey', 'focus', [1, 2, 3, 4]] |
---|
84 | n/a | opt_val = [] |
---|
85 | n/a | for *state, val in items: |
---|
86 | n/a | # hacks for bakward compatibility |
---|
87 | n/a | state[0] # raise IndexError if empty |
---|
88 | n/a | if len(state) == 1: |
---|
89 | n/a | # if it is empty (something that evaluates to False), then |
---|
90 | n/a | # format it to Tcl code to denote the "normal" state |
---|
91 | n/a | state = state[0] or '' |
---|
92 | n/a | else: |
---|
93 | n/a | # group multiple states |
---|
94 | n/a | state = ' '.join(state) # raise TypeError if not str |
---|
95 | n/a | opt_val.append(state) |
---|
96 | n/a | if val is not None: |
---|
97 | n/a | opt_val.append(val) |
---|
98 | n/a | return opt_val |
---|
99 | n/a | |
---|
100 | n/a | def _format_mapdict(mapdict, script=False): |
---|
101 | n/a | """Formats mapdict to pass it to tk.call. |
---|
102 | n/a | |
---|
103 | n/a | E.g. (script=False): |
---|
104 | n/a | {'expand': [('active', 'selected', 'grey'), ('focus', [1, 2, 3, 4])]} |
---|
105 | n/a | |
---|
106 | n/a | returns: |
---|
107 | n/a | |
---|
108 | n/a | ('-expand', '{active selected} grey focus {1, 2, 3, 4}')""" |
---|
109 | n/a | |
---|
110 | n/a | opts = [] |
---|
111 | n/a | for opt, value in mapdict.items(): |
---|
112 | n/a | opts.extend(("-%s" % opt, |
---|
113 | n/a | _format_optvalue(_mapdict_values(value), script))) |
---|
114 | n/a | |
---|
115 | n/a | return _flatten(opts) |
---|
116 | n/a | |
---|
117 | n/a | def _format_elemcreate(etype, script=False, *args, **kw): |
---|
118 | n/a | """Formats args and kw according to the given element factory etype.""" |
---|
119 | n/a | spec = None |
---|
120 | n/a | opts = () |
---|
121 | n/a | if etype in ("image", "vsapi"): |
---|
122 | n/a | if etype == "image": # define an element based on an image |
---|
123 | n/a | # first arg should be the default image name |
---|
124 | n/a | iname = args[0] |
---|
125 | n/a | # next args, if any, are statespec/value pairs which is almost |
---|
126 | n/a | # a mapdict, but we just need the value |
---|
127 | n/a | imagespec = _join(_mapdict_values(args[1:])) |
---|
128 | n/a | spec = "%s %s" % (iname, imagespec) |
---|
129 | n/a | |
---|
130 | n/a | else: |
---|
131 | n/a | # define an element whose visual appearance is drawn using the |
---|
132 | n/a | # Microsoft Visual Styles API which is responsible for the |
---|
133 | n/a | # themed styles on Windows XP and Vista. |
---|
134 | n/a | # Availability: Tk 8.6, Windows XP and Vista. |
---|
135 | n/a | class_name, part_id = args[:2] |
---|
136 | n/a | statemap = _join(_mapdict_values(args[2:])) |
---|
137 | n/a | spec = "%s %s %s" % (class_name, part_id, statemap) |
---|
138 | n/a | |
---|
139 | n/a | opts = _format_optdict(kw, script) |
---|
140 | n/a | |
---|
141 | n/a | elif etype == "from": # clone an element |
---|
142 | n/a | # it expects a themename and optionally an element to clone from, |
---|
143 | n/a | # otherwise it will clone {} (empty element) |
---|
144 | n/a | spec = args[0] # theme name |
---|
145 | n/a | if len(args) > 1: # elementfrom specified |
---|
146 | n/a | opts = (_format_optvalue(args[1], script),) |
---|
147 | n/a | |
---|
148 | n/a | if script: |
---|
149 | n/a | spec = '{%s}' % spec |
---|
150 | n/a | opts = ' '.join(opts) |
---|
151 | n/a | |
---|
152 | n/a | return spec, opts |
---|
153 | n/a | |
---|
154 | n/a | def _format_layoutlist(layout, indent=0, indent_size=2): |
---|
155 | n/a | """Formats a layout list so we can pass the result to ttk::style |
---|
156 | n/a | layout and ttk::style settings. Note that the layout doesn't have to |
---|
157 | n/a | be a list necessarily. |
---|
158 | n/a | |
---|
159 | n/a | E.g.: |
---|
160 | n/a | [("Menubutton.background", None), |
---|
161 | n/a | ("Menubutton.button", {"children": |
---|
162 | n/a | [("Menubutton.focus", {"children": |
---|
163 | n/a | [("Menubutton.padding", {"children": |
---|
164 | n/a | [("Menubutton.label", {"side": "left", "expand": 1})] |
---|
165 | n/a | })] |
---|
166 | n/a | })] |
---|
167 | n/a | }), |
---|
168 | n/a | ("Menubutton.indicator", {"side": "right"}) |
---|
169 | n/a | ] |
---|
170 | n/a | |
---|
171 | n/a | returns: |
---|
172 | n/a | |
---|
173 | n/a | Menubutton.background |
---|
174 | n/a | Menubutton.button -children { |
---|
175 | n/a | Menubutton.focus -children { |
---|
176 | n/a | Menubutton.padding -children { |
---|
177 | n/a | Menubutton.label -side left -expand 1 |
---|
178 | n/a | } |
---|
179 | n/a | } |
---|
180 | n/a | } |
---|
181 | n/a | Menubutton.indicator -side right""" |
---|
182 | n/a | script = [] |
---|
183 | n/a | |
---|
184 | n/a | for layout_elem in layout: |
---|
185 | n/a | elem, opts = layout_elem |
---|
186 | n/a | opts = opts or {} |
---|
187 | n/a | fopts = ' '.join(_format_optdict(opts, True, ("children",))) |
---|
188 | n/a | head = "%s%s%s" % (' ' * indent, elem, (" %s" % fopts) if fopts else '') |
---|
189 | n/a | |
---|
190 | n/a | if "children" in opts: |
---|
191 | n/a | script.append(head + " -children {") |
---|
192 | n/a | indent += indent_size |
---|
193 | n/a | newscript, indent = _format_layoutlist(opts['children'], indent, |
---|
194 | n/a | indent_size) |
---|
195 | n/a | script.append(newscript) |
---|
196 | n/a | indent -= indent_size |
---|
197 | n/a | script.append('%s}' % (' ' * indent)) |
---|
198 | n/a | else: |
---|
199 | n/a | script.append(head) |
---|
200 | n/a | |
---|
201 | n/a | return '\n'.join(script), indent |
---|
202 | n/a | |
---|
203 | n/a | def _script_from_settings(settings): |
---|
204 | n/a | """Returns an appropriate script, based on settings, according to |
---|
205 | n/a | theme_settings definition to be used by theme_settings and |
---|
206 | n/a | theme_create.""" |
---|
207 | n/a | script = [] |
---|
208 | n/a | # a script will be generated according to settings passed, which |
---|
209 | n/a | # will then be evaluated by Tcl |
---|
210 | n/a | for name, opts in settings.items(): |
---|
211 | n/a | # will format specific keys according to Tcl code |
---|
212 | n/a | if opts.get('configure'): # format 'configure' |
---|
213 | n/a | s = ' '.join(_format_optdict(opts['configure'], True)) |
---|
214 | n/a | script.append("ttk::style configure %s %s;" % (name, s)) |
---|
215 | n/a | |
---|
216 | n/a | if opts.get('map'): # format 'map' |
---|
217 | n/a | s = ' '.join(_format_mapdict(opts['map'], True)) |
---|
218 | n/a | script.append("ttk::style map %s %s;" % (name, s)) |
---|
219 | n/a | |
---|
220 | n/a | if 'layout' in opts: # format 'layout' which may be empty |
---|
221 | n/a | if not opts['layout']: |
---|
222 | n/a | s = 'null' # could be any other word, but this one makes sense |
---|
223 | n/a | else: |
---|
224 | n/a | s, _ = _format_layoutlist(opts['layout']) |
---|
225 | n/a | script.append("ttk::style layout %s {\n%s\n}" % (name, s)) |
---|
226 | n/a | |
---|
227 | n/a | if opts.get('element create'): # format 'element create' |
---|
228 | n/a | eopts = opts['element create'] |
---|
229 | n/a | etype = eopts[0] |
---|
230 | n/a | |
---|
231 | n/a | # find where args end, and where kwargs start |
---|
232 | n/a | argc = 1 # etype was the first one |
---|
233 | n/a | while argc < len(eopts) and not hasattr(eopts[argc], 'items'): |
---|
234 | n/a | argc += 1 |
---|
235 | n/a | |
---|
236 | n/a | elemargs = eopts[1:argc] |
---|
237 | n/a | elemkw = eopts[argc] if argc < len(eopts) and eopts[argc] else {} |
---|
238 | n/a | spec, opts = _format_elemcreate(etype, True, *elemargs, **elemkw) |
---|
239 | n/a | |
---|
240 | n/a | script.append("ttk::style element create %s %s %s %s" % ( |
---|
241 | n/a | name, etype, spec, opts)) |
---|
242 | n/a | |
---|
243 | n/a | return '\n'.join(script) |
---|
244 | n/a | |
---|
245 | n/a | def _list_from_statespec(stuple): |
---|
246 | n/a | """Construct a list from the given statespec tuple according to the |
---|
247 | n/a | accepted statespec accepted by _format_mapdict.""" |
---|
248 | n/a | nval = [] |
---|
249 | n/a | for val in stuple: |
---|
250 | n/a | typename = getattr(val, 'typename', None) |
---|
251 | n/a | if typename is None: |
---|
252 | n/a | nval.append(val) |
---|
253 | n/a | else: # this is a Tcl object |
---|
254 | n/a | val = str(val) |
---|
255 | n/a | if typename == 'StateSpec': |
---|
256 | n/a | val = val.split() |
---|
257 | n/a | nval.append(val) |
---|
258 | n/a | |
---|
259 | n/a | it = iter(nval) |
---|
260 | n/a | return [_flatten(spec) for spec in zip(it, it)] |
---|
261 | n/a | |
---|
262 | n/a | def _list_from_layouttuple(tk, ltuple): |
---|
263 | n/a | """Construct a list from the tuple returned by ttk::layout, this is |
---|
264 | n/a | somewhat the reverse of _format_layoutlist.""" |
---|
265 | n/a | ltuple = tk.splitlist(ltuple) |
---|
266 | n/a | res = [] |
---|
267 | n/a | |
---|
268 | n/a | indx = 0 |
---|
269 | n/a | while indx < len(ltuple): |
---|
270 | n/a | name = ltuple[indx] |
---|
271 | n/a | opts = {} |
---|
272 | n/a | res.append((name, opts)) |
---|
273 | n/a | indx += 1 |
---|
274 | n/a | |
---|
275 | n/a | while indx < len(ltuple): # grab name's options |
---|
276 | n/a | opt, val = ltuple[indx:indx + 2] |
---|
277 | n/a | if not opt.startswith('-'): # found next name |
---|
278 | n/a | break |
---|
279 | n/a | |
---|
280 | n/a | opt = opt[1:] # remove the '-' from the option |
---|
281 | n/a | indx += 2 |
---|
282 | n/a | |
---|
283 | n/a | if opt == 'children': |
---|
284 | n/a | val = _list_from_layouttuple(tk, val) |
---|
285 | n/a | |
---|
286 | n/a | opts[opt] = val |
---|
287 | n/a | |
---|
288 | n/a | return res |
---|
289 | n/a | |
---|
290 | n/a | def _val_or_dict(tk, options, *args): |
---|
291 | n/a | """Format options then call Tk command with args and options and return |
---|
292 | n/a | the appropriate result. |
---|
293 | n/a | |
---|
294 | n/a | If no option is specified, a dict is returned. If an option is |
---|
295 | n/a | specified with the None value, the value for that option is returned. |
---|
296 | n/a | Otherwise, the function just sets the passed options and the caller |
---|
297 | n/a | shouldn't be expecting a return value anyway.""" |
---|
298 | n/a | options = _format_optdict(options) |
---|
299 | n/a | res = tk.call(*(args + options)) |
---|
300 | n/a | |
---|
301 | n/a | if len(options) % 2: # option specified without a value, return its value |
---|
302 | n/a | return res |
---|
303 | n/a | |
---|
304 | n/a | return _splitdict(tk, res, conv=_tclobj_to_py) |
---|
305 | n/a | |
---|
306 | n/a | def _convert_stringval(value): |
---|
307 | n/a | """Converts a value to, hopefully, a more appropriate Python object.""" |
---|
308 | n/a | value = str(value) |
---|
309 | n/a | try: |
---|
310 | n/a | value = int(value) |
---|
311 | n/a | except (ValueError, TypeError): |
---|
312 | n/a | pass |
---|
313 | n/a | |
---|
314 | n/a | return value |
---|
315 | n/a | |
---|
316 | n/a | def _to_number(x): |
---|
317 | n/a | if isinstance(x, str): |
---|
318 | n/a | if '.' in x: |
---|
319 | n/a | x = float(x) |
---|
320 | n/a | else: |
---|
321 | n/a | x = int(x) |
---|
322 | n/a | return x |
---|
323 | n/a | |
---|
324 | n/a | def _tclobj_to_py(val): |
---|
325 | n/a | """Return value converted from Tcl object to Python object.""" |
---|
326 | n/a | if val and hasattr(val, '__len__') and not isinstance(val, str): |
---|
327 | n/a | if getattr(val[0], 'typename', None) == 'StateSpec': |
---|
328 | n/a | val = _list_from_statespec(val) |
---|
329 | n/a | else: |
---|
330 | n/a | val = list(map(_convert_stringval, val)) |
---|
331 | n/a | |
---|
332 | n/a | elif hasattr(val, 'typename'): # some other (single) Tcl object |
---|
333 | n/a | val = _convert_stringval(val) |
---|
334 | n/a | |
---|
335 | n/a | return val |
---|
336 | n/a | |
---|
337 | n/a | def tclobjs_to_py(adict): |
---|
338 | n/a | """Returns adict with its values converted from Tcl objects to Python |
---|
339 | n/a | objects.""" |
---|
340 | n/a | for opt, val in adict.items(): |
---|
341 | n/a | adict[opt] = _tclobj_to_py(val) |
---|
342 | n/a | |
---|
343 | n/a | return adict |
---|
344 | n/a | |
---|
345 | n/a | def setup_master(master=None): |
---|
346 | n/a | """If master is not None, itself is returned. If master is None, |
---|
347 | n/a | the default master is returned if there is one, otherwise a new |
---|
348 | n/a | master is created and returned. |
---|
349 | n/a | |
---|
350 | n/a | If it is not allowed to use the default root and master is None, |
---|
351 | n/a | RuntimeError is raised.""" |
---|
352 | n/a | if master is None: |
---|
353 | n/a | if tkinter._support_default_root: |
---|
354 | n/a | master = tkinter._default_root or tkinter.Tk() |
---|
355 | n/a | else: |
---|
356 | n/a | raise RuntimeError( |
---|
357 | n/a | "No master specified and tkinter is " |
---|
358 | n/a | "configured to not support default root") |
---|
359 | n/a | return master |
---|
360 | n/a | |
---|
361 | n/a | |
---|
362 | n/a | class Style(object): |
---|
363 | n/a | """Manipulate style database.""" |
---|
364 | n/a | |
---|
365 | n/a | _name = "ttk::style" |
---|
366 | n/a | |
---|
367 | n/a | def __init__(self, master=None): |
---|
368 | n/a | master = setup_master(master) |
---|
369 | n/a | |
---|
370 | n/a | if not getattr(master, '_tile_loaded', False): |
---|
371 | n/a | # Load tile now, if needed |
---|
372 | n/a | _load_tile(master) |
---|
373 | n/a | |
---|
374 | n/a | self.master = master |
---|
375 | n/a | self.tk = self.master.tk |
---|
376 | n/a | |
---|
377 | n/a | |
---|
378 | n/a | def configure(self, style, query_opt=None, **kw): |
---|
379 | n/a | """Query or sets the default value of the specified option(s) in |
---|
380 | n/a | style. |
---|
381 | n/a | |
---|
382 | n/a | Each key in kw is an option and each value is either a string or |
---|
383 | n/a | a sequence identifying the value for that option.""" |
---|
384 | n/a | if query_opt is not None: |
---|
385 | n/a | kw[query_opt] = None |
---|
386 | n/a | result = _val_or_dict(self.tk, kw, self._name, "configure", style) |
---|
387 | n/a | if result or query_opt: |
---|
388 | n/a | return result |
---|
389 | n/a | |
---|
390 | n/a | |
---|
391 | n/a | def map(self, style, query_opt=None, **kw): |
---|
392 | n/a | """Query or sets dynamic values of the specified option(s) in |
---|
393 | n/a | style. |
---|
394 | n/a | |
---|
395 | n/a | Each key in kw is an option and each value should be a list or a |
---|
396 | n/a | tuple (usually) containing statespecs grouped in tuples, or list, |
---|
397 | n/a | or something else of your preference. A statespec is compound of |
---|
398 | n/a | one or more states and then a value.""" |
---|
399 | n/a | if query_opt is not None: |
---|
400 | n/a | return _list_from_statespec(self.tk.splitlist( |
---|
401 | n/a | self.tk.call(self._name, "map", style, '-%s' % query_opt))) |
---|
402 | n/a | |
---|
403 | n/a | return _splitdict( |
---|
404 | n/a | self.tk, |
---|
405 | n/a | self.tk.call(self._name, "map", style, *_format_mapdict(kw)), |
---|
406 | n/a | conv=_tclobj_to_py) |
---|
407 | n/a | |
---|
408 | n/a | |
---|
409 | n/a | def lookup(self, style, option, state=None, default=None): |
---|
410 | n/a | """Returns the value specified for option in style. |
---|
411 | n/a | |
---|
412 | n/a | If state is specified it is expected to be a sequence of one |
---|
413 | n/a | or more states. If the default argument is set, it is used as |
---|
414 | n/a | a fallback value in case no specification for option is found.""" |
---|
415 | n/a | state = ' '.join(state) if state else '' |
---|
416 | n/a | |
---|
417 | n/a | return self.tk.call(self._name, "lookup", style, '-%s' % option, |
---|
418 | n/a | state, default) |
---|
419 | n/a | |
---|
420 | n/a | |
---|
421 | n/a | def layout(self, style, layoutspec=None): |
---|
422 | n/a | """Define the widget layout for given style. If layoutspec is |
---|
423 | n/a | omitted, return the layout specification for given style. |
---|
424 | n/a | |
---|
425 | n/a | layoutspec is expected to be a list or an object different than |
---|
426 | n/a | None that evaluates to False if you want to "turn off" that style. |
---|
427 | n/a | If it is a list (or tuple, or something else), each item should be |
---|
428 | n/a | a tuple where the first item is the layout name and the second item |
---|
429 | n/a | should have the format described below: |
---|
430 | n/a | |
---|
431 | n/a | LAYOUTS |
---|
432 | n/a | |
---|
433 | n/a | A layout can contain the value None, if takes no options, or |
---|
434 | n/a | a dict of options specifying how to arrange the element. |
---|
435 | n/a | The layout mechanism uses a simplified version of the pack |
---|
436 | n/a | geometry manager: given an initial cavity, each element is |
---|
437 | n/a | allocated a parcel. Valid options/values are: |
---|
438 | n/a | |
---|
439 | n/a | side: whichside |
---|
440 | n/a | Specifies which side of the cavity to place the |
---|
441 | n/a | element; one of top, right, bottom or left. If |
---|
442 | n/a | omitted, the element occupies the entire cavity. |
---|
443 | n/a | |
---|
444 | n/a | sticky: nswe |
---|
445 | n/a | Specifies where the element is placed inside its |
---|
446 | n/a | allocated parcel. |
---|
447 | n/a | |
---|
448 | n/a | children: [sublayout... ] |
---|
449 | n/a | Specifies a list of elements to place inside the |
---|
450 | n/a | element. Each element is a tuple (or other sequence) |
---|
451 | n/a | where the first item is the layout name, and the other |
---|
452 | n/a | is a LAYOUT.""" |
---|
453 | n/a | lspec = None |
---|
454 | n/a | if layoutspec: |
---|
455 | n/a | lspec = _format_layoutlist(layoutspec)[0] |
---|
456 | n/a | elif layoutspec is not None: # will disable the layout ({}, '', etc) |
---|
457 | n/a | lspec = "null" # could be any other word, but this may make sense |
---|
458 | n/a | # when calling layout(style) later |
---|
459 | n/a | |
---|
460 | n/a | return _list_from_layouttuple(self.tk, |
---|
461 | n/a | self.tk.call(self._name, "layout", style, lspec)) |
---|
462 | n/a | |
---|
463 | n/a | |
---|
464 | n/a | def element_create(self, elementname, etype, *args, **kw): |
---|
465 | n/a | """Create a new element in the current theme of given etype.""" |
---|
466 | n/a | spec, opts = _format_elemcreate(etype, False, *args, **kw) |
---|
467 | n/a | self.tk.call(self._name, "element", "create", elementname, etype, |
---|
468 | n/a | spec, *opts) |
---|
469 | n/a | |
---|
470 | n/a | |
---|
471 | n/a | def element_names(self): |
---|
472 | n/a | """Returns the list of elements defined in the current theme.""" |
---|
473 | n/a | return tuple(n.lstrip('-') for n in self.tk.splitlist( |
---|
474 | n/a | self.tk.call(self._name, "element", "names"))) |
---|
475 | n/a | |
---|
476 | n/a | |
---|
477 | n/a | def element_options(self, elementname): |
---|
478 | n/a | """Return the list of elementname's options.""" |
---|
479 | n/a | return tuple(o.lstrip('-') for o in self.tk.splitlist( |
---|
480 | n/a | self.tk.call(self._name, "element", "options", elementname))) |
---|
481 | n/a | |
---|
482 | n/a | |
---|
483 | n/a | def theme_create(self, themename, parent=None, settings=None): |
---|
484 | n/a | """Creates a new theme. |
---|
485 | n/a | |
---|
486 | n/a | It is an error if themename already exists. If parent is |
---|
487 | n/a | specified, the new theme will inherit styles, elements and |
---|
488 | n/a | layouts from the specified parent theme. If settings are present, |
---|
489 | n/a | they are expected to have the same syntax used for theme_settings.""" |
---|
490 | n/a | script = _script_from_settings(settings) if settings else '' |
---|
491 | n/a | |
---|
492 | n/a | if parent: |
---|
493 | n/a | self.tk.call(self._name, "theme", "create", themename, |
---|
494 | n/a | "-parent", parent, "-settings", script) |
---|
495 | n/a | else: |
---|
496 | n/a | self.tk.call(self._name, "theme", "create", themename, |
---|
497 | n/a | "-settings", script) |
---|
498 | n/a | |
---|
499 | n/a | |
---|
500 | n/a | def theme_settings(self, themename, settings): |
---|
501 | n/a | """Temporarily sets the current theme to themename, apply specified |
---|
502 | n/a | settings and then restore the previous theme. |
---|
503 | n/a | |
---|
504 | n/a | Each key in settings is a style and each value may contain the |
---|
505 | n/a | keys 'configure', 'map', 'layout' and 'element create' and they |
---|
506 | n/a | are expected to have the same format as specified by the methods |
---|
507 | n/a | configure, map, layout and element_create respectively.""" |
---|
508 | n/a | script = _script_from_settings(settings) |
---|
509 | n/a | self.tk.call(self._name, "theme", "settings", themename, script) |
---|
510 | n/a | |
---|
511 | n/a | |
---|
512 | n/a | def theme_names(self): |
---|
513 | n/a | """Returns a list of all known themes.""" |
---|
514 | n/a | return self.tk.splitlist(self.tk.call(self._name, "theme", "names")) |
---|
515 | n/a | |
---|
516 | n/a | |
---|
517 | n/a | def theme_use(self, themename=None): |
---|
518 | n/a | """If themename is None, returns the theme in use, otherwise, set |
---|
519 | n/a | the current theme to themename, refreshes all widgets and emits |
---|
520 | n/a | a <<ThemeChanged>> event.""" |
---|
521 | n/a | if themename is None: |
---|
522 | n/a | # Starting on Tk 8.6, checking this global is no longer needed |
---|
523 | n/a | # since it allows doing self.tk.call(self._name, "theme", "use") |
---|
524 | n/a | return self.tk.eval("return $ttk::currentTheme") |
---|
525 | n/a | |
---|
526 | n/a | # using "ttk::setTheme" instead of "ttk::style theme use" causes |
---|
527 | n/a | # the variable currentTheme to be updated, also, ttk::setTheme calls |
---|
528 | n/a | # "ttk::style theme use" in order to change theme. |
---|
529 | n/a | self.tk.call("ttk::setTheme", themename) |
---|
530 | n/a | |
---|
531 | n/a | |
---|
532 | n/a | class Widget(tkinter.Widget): |
---|
533 | n/a | """Base class for Tk themed widgets.""" |
---|
534 | n/a | |
---|
535 | n/a | def __init__(self, master, widgetname, kw=None): |
---|
536 | n/a | """Constructs a Ttk Widget with the parent master. |
---|
537 | n/a | |
---|
538 | n/a | STANDARD OPTIONS |
---|
539 | n/a | |
---|
540 | n/a | class, cursor, takefocus, style |
---|
541 | n/a | |
---|
542 | n/a | SCROLLABLE WIDGET OPTIONS |
---|
543 | n/a | |
---|
544 | n/a | xscrollcommand, yscrollcommand |
---|
545 | n/a | |
---|
546 | n/a | LABEL WIDGET OPTIONS |
---|
547 | n/a | |
---|
548 | n/a | text, textvariable, underline, image, compound, width |
---|
549 | n/a | |
---|
550 | n/a | WIDGET STATES |
---|
551 | n/a | |
---|
552 | n/a | active, disabled, focus, pressed, selected, background, |
---|
553 | n/a | readonly, alternate, invalid |
---|
554 | n/a | """ |
---|
555 | n/a | master = setup_master(master) |
---|
556 | n/a | if not getattr(master, '_tile_loaded', False): |
---|
557 | n/a | # Load tile now, if needed |
---|
558 | n/a | _load_tile(master) |
---|
559 | n/a | tkinter.Widget.__init__(self, master, widgetname, kw=kw) |
---|
560 | n/a | |
---|
561 | n/a | |
---|
562 | n/a | def identify(self, x, y): |
---|
563 | n/a | """Returns the name of the element at position x, y, or the empty |
---|
564 | n/a | string if the point does not lie within any element. |
---|
565 | n/a | |
---|
566 | n/a | x and y are pixel coordinates relative to the widget.""" |
---|
567 | n/a | return self.tk.call(self._w, "identify", x, y) |
---|
568 | n/a | |
---|
569 | n/a | |
---|
570 | n/a | def instate(self, statespec, callback=None, *args, **kw): |
---|
571 | n/a | """Test the widget's state. |
---|
572 | n/a | |
---|
573 | n/a | If callback is not specified, returns True if the widget state |
---|
574 | n/a | matches statespec and False otherwise. If callback is specified, |
---|
575 | n/a | then it will be invoked with *args, **kw if the widget state |
---|
576 | n/a | matches statespec. statespec is expected to be a sequence.""" |
---|
577 | n/a | ret = self.tk.getboolean( |
---|
578 | n/a | self.tk.call(self._w, "instate", ' '.join(statespec))) |
---|
579 | n/a | if ret and callback: |
---|
580 | n/a | return callback(*args, **kw) |
---|
581 | n/a | |
---|
582 | n/a | return ret |
---|
583 | n/a | |
---|
584 | n/a | |
---|
585 | n/a | def state(self, statespec=None): |
---|
586 | n/a | """Modify or inquire widget state. |
---|
587 | n/a | |
---|
588 | n/a | Widget state is returned if statespec is None, otherwise it is |
---|
589 | n/a | set according to the statespec flags and then a new state spec |
---|
590 | n/a | is returned indicating which flags were changed. statespec is |
---|
591 | n/a | expected to be a sequence.""" |
---|
592 | n/a | if statespec is not None: |
---|
593 | n/a | statespec = ' '.join(statespec) |
---|
594 | n/a | |
---|
595 | n/a | return self.tk.splitlist(str(self.tk.call(self._w, "state", statespec))) |
---|
596 | n/a | |
---|
597 | n/a | |
---|
598 | n/a | class Button(Widget): |
---|
599 | n/a | """Ttk Button widget, displays a textual label and/or image, and |
---|
600 | n/a | evaluates a command when pressed.""" |
---|
601 | n/a | |
---|
602 | n/a | def __init__(self, master=None, **kw): |
---|
603 | n/a | """Construct a Ttk Button widget with the parent master. |
---|
604 | n/a | |
---|
605 | n/a | STANDARD OPTIONS |
---|
606 | n/a | |
---|
607 | n/a | class, compound, cursor, image, state, style, takefocus, |
---|
608 | n/a | text, textvariable, underline, width |
---|
609 | n/a | |
---|
610 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
611 | n/a | |
---|
612 | n/a | command, default, width |
---|
613 | n/a | """ |
---|
614 | n/a | Widget.__init__(self, master, "ttk::button", kw) |
---|
615 | n/a | |
---|
616 | n/a | |
---|
617 | n/a | def invoke(self): |
---|
618 | n/a | """Invokes the command associated with the button.""" |
---|
619 | n/a | return self.tk.call(self._w, "invoke") |
---|
620 | n/a | |
---|
621 | n/a | |
---|
622 | n/a | class Checkbutton(Widget): |
---|
623 | n/a | """Ttk Checkbutton widget which is either in on- or off-state.""" |
---|
624 | n/a | |
---|
625 | n/a | def __init__(self, master=None, **kw): |
---|
626 | n/a | """Construct a Ttk Checkbutton widget with the parent master. |
---|
627 | n/a | |
---|
628 | n/a | STANDARD OPTIONS |
---|
629 | n/a | |
---|
630 | n/a | class, compound, cursor, image, state, style, takefocus, |
---|
631 | n/a | text, textvariable, underline, width |
---|
632 | n/a | |
---|
633 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
634 | n/a | |
---|
635 | n/a | command, offvalue, onvalue, variable |
---|
636 | n/a | """ |
---|
637 | n/a | Widget.__init__(self, master, "ttk::checkbutton", kw) |
---|
638 | n/a | |
---|
639 | n/a | |
---|
640 | n/a | def invoke(self): |
---|
641 | n/a | """Toggles between the selected and deselected states and |
---|
642 | n/a | invokes the associated command. If the widget is currently |
---|
643 | n/a | selected, sets the option variable to the offvalue option |
---|
644 | n/a | and deselects the widget; otherwise, sets the option variable |
---|
645 | n/a | to the option onvalue. |
---|
646 | n/a | |
---|
647 | n/a | Returns the result of the associated command.""" |
---|
648 | n/a | return self.tk.call(self._w, "invoke") |
---|
649 | n/a | |
---|
650 | n/a | |
---|
651 | n/a | class Entry(Widget, tkinter.Entry): |
---|
652 | n/a | """Ttk Entry widget displays a one-line text string and allows that |
---|
653 | n/a | string to be edited by the user.""" |
---|
654 | n/a | |
---|
655 | n/a | def __init__(self, master=None, widget=None, **kw): |
---|
656 | n/a | """Constructs a Ttk Entry widget with the parent master. |
---|
657 | n/a | |
---|
658 | n/a | STANDARD OPTIONS |
---|
659 | n/a | |
---|
660 | n/a | class, cursor, style, takefocus, xscrollcommand |
---|
661 | n/a | |
---|
662 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
663 | n/a | |
---|
664 | n/a | exportselection, invalidcommand, justify, show, state, |
---|
665 | n/a | textvariable, validate, validatecommand, width |
---|
666 | n/a | |
---|
667 | n/a | VALIDATION MODES |
---|
668 | n/a | |
---|
669 | n/a | none, key, focus, focusin, focusout, all |
---|
670 | n/a | """ |
---|
671 | n/a | Widget.__init__(self, master, widget or "ttk::entry", kw) |
---|
672 | n/a | |
---|
673 | n/a | |
---|
674 | n/a | def bbox(self, index): |
---|
675 | n/a | """Return a tuple of (x, y, width, height) which describes the |
---|
676 | n/a | bounding box of the character given by index.""" |
---|
677 | n/a | return self._getints(self.tk.call(self._w, "bbox", index)) |
---|
678 | n/a | |
---|
679 | n/a | |
---|
680 | n/a | def identify(self, x, y): |
---|
681 | n/a | """Returns the name of the element at position x, y, or the |
---|
682 | n/a | empty string if the coordinates are outside the window.""" |
---|
683 | n/a | return self.tk.call(self._w, "identify", x, y) |
---|
684 | n/a | |
---|
685 | n/a | |
---|
686 | n/a | def validate(self): |
---|
687 | n/a | """Force revalidation, independent of the conditions specified |
---|
688 | n/a | by the validate option. Returns False if validation fails, True |
---|
689 | n/a | if it succeeds. Sets or clears the invalid state accordingly.""" |
---|
690 | n/a | return self.tk.getboolean(self.tk.call(self._w, "validate")) |
---|
691 | n/a | |
---|
692 | n/a | |
---|
693 | n/a | class Combobox(Entry): |
---|
694 | n/a | """Ttk Combobox widget combines a text field with a pop-down list of |
---|
695 | n/a | values.""" |
---|
696 | n/a | |
---|
697 | n/a | def __init__(self, master=None, **kw): |
---|
698 | n/a | """Construct a Ttk Combobox widget with the parent master. |
---|
699 | n/a | |
---|
700 | n/a | STANDARD OPTIONS |
---|
701 | n/a | |
---|
702 | n/a | class, cursor, style, takefocus |
---|
703 | n/a | |
---|
704 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
705 | n/a | |
---|
706 | n/a | exportselection, justify, height, postcommand, state, |
---|
707 | n/a | textvariable, values, width |
---|
708 | n/a | """ |
---|
709 | n/a | Entry.__init__(self, master, "ttk::combobox", **kw) |
---|
710 | n/a | |
---|
711 | n/a | |
---|
712 | n/a | def current(self, newindex=None): |
---|
713 | n/a | """If newindex is supplied, sets the combobox value to the |
---|
714 | n/a | element at position newindex in the list of values. Otherwise, |
---|
715 | n/a | returns the index of the current value in the list of values |
---|
716 | n/a | or -1 if the current value does not appear in the list.""" |
---|
717 | n/a | if newindex is None: |
---|
718 | n/a | return self.tk.getint(self.tk.call(self._w, "current")) |
---|
719 | n/a | return self.tk.call(self._w, "current", newindex) |
---|
720 | n/a | |
---|
721 | n/a | |
---|
722 | n/a | def set(self, value): |
---|
723 | n/a | """Sets the value of the combobox to value.""" |
---|
724 | n/a | self.tk.call(self._w, "set", value) |
---|
725 | n/a | |
---|
726 | n/a | |
---|
727 | n/a | class Frame(Widget): |
---|
728 | n/a | """Ttk Frame widget is a container, used to group other widgets |
---|
729 | n/a | together.""" |
---|
730 | n/a | |
---|
731 | n/a | def __init__(self, master=None, **kw): |
---|
732 | n/a | """Construct a Ttk Frame with parent master. |
---|
733 | n/a | |
---|
734 | n/a | STANDARD OPTIONS |
---|
735 | n/a | |
---|
736 | n/a | class, cursor, style, takefocus |
---|
737 | n/a | |
---|
738 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
739 | n/a | |
---|
740 | n/a | borderwidth, relief, padding, width, height |
---|
741 | n/a | """ |
---|
742 | n/a | Widget.__init__(self, master, "ttk::frame", kw) |
---|
743 | n/a | |
---|
744 | n/a | |
---|
745 | n/a | class Label(Widget): |
---|
746 | n/a | """Ttk Label widget displays a textual label and/or image.""" |
---|
747 | n/a | |
---|
748 | n/a | def __init__(self, master=None, **kw): |
---|
749 | n/a | """Construct a Ttk Label with parent master. |
---|
750 | n/a | |
---|
751 | n/a | STANDARD OPTIONS |
---|
752 | n/a | |
---|
753 | n/a | class, compound, cursor, image, style, takefocus, text, |
---|
754 | n/a | textvariable, underline, width |
---|
755 | n/a | |
---|
756 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
757 | n/a | |
---|
758 | n/a | anchor, background, font, foreground, justify, padding, |
---|
759 | n/a | relief, text, wraplength |
---|
760 | n/a | """ |
---|
761 | n/a | Widget.__init__(self, master, "ttk::label", kw) |
---|
762 | n/a | |
---|
763 | n/a | |
---|
764 | n/a | class Labelframe(Widget): |
---|
765 | n/a | """Ttk Labelframe widget is a container used to group other widgets |
---|
766 | n/a | together. It has an optional label, which may be a plain text string |
---|
767 | n/a | or another widget.""" |
---|
768 | n/a | |
---|
769 | n/a | def __init__(self, master=None, **kw): |
---|
770 | n/a | """Construct a Ttk Labelframe with parent master. |
---|
771 | n/a | |
---|
772 | n/a | STANDARD OPTIONS |
---|
773 | n/a | |
---|
774 | n/a | class, cursor, style, takefocus |
---|
775 | n/a | |
---|
776 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
777 | n/a | labelanchor, text, underline, padding, labelwidget, width, |
---|
778 | n/a | height |
---|
779 | n/a | """ |
---|
780 | n/a | Widget.__init__(self, master, "ttk::labelframe", kw) |
---|
781 | n/a | |
---|
782 | n/a | LabelFrame = Labelframe # tkinter name compatibility |
---|
783 | n/a | |
---|
784 | n/a | |
---|
785 | n/a | class Menubutton(Widget): |
---|
786 | n/a | """Ttk Menubutton widget displays a textual label and/or image, and |
---|
787 | n/a | displays a menu when pressed.""" |
---|
788 | n/a | |
---|
789 | n/a | def __init__(self, master=None, **kw): |
---|
790 | n/a | """Construct a Ttk Menubutton with parent master. |
---|
791 | n/a | |
---|
792 | n/a | STANDARD OPTIONS |
---|
793 | n/a | |
---|
794 | n/a | class, compound, cursor, image, state, style, takefocus, |
---|
795 | n/a | text, textvariable, underline, width |
---|
796 | n/a | |
---|
797 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
798 | n/a | |
---|
799 | n/a | direction, menu |
---|
800 | n/a | """ |
---|
801 | n/a | Widget.__init__(self, master, "ttk::menubutton", kw) |
---|
802 | n/a | |
---|
803 | n/a | |
---|
804 | n/a | class Notebook(Widget): |
---|
805 | n/a | """Ttk Notebook widget manages a collection of windows and displays |
---|
806 | n/a | a single one at a time. Each child window is associated with a tab, |
---|
807 | n/a | which the user may select to change the currently-displayed window.""" |
---|
808 | n/a | |
---|
809 | n/a | def __init__(self, master=None, **kw): |
---|
810 | n/a | """Construct a Ttk Notebook with parent master. |
---|
811 | n/a | |
---|
812 | n/a | STANDARD OPTIONS |
---|
813 | n/a | |
---|
814 | n/a | class, cursor, style, takefocus |
---|
815 | n/a | |
---|
816 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
817 | n/a | |
---|
818 | n/a | height, padding, width |
---|
819 | n/a | |
---|
820 | n/a | TAB OPTIONS |
---|
821 | n/a | |
---|
822 | n/a | state, sticky, padding, text, image, compound, underline |
---|
823 | n/a | |
---|
824 | n/a | TAB IDENTIFIERS (tab_id) |
---|
825 | n/a | |
---|
826 | n/a | The tab_id argument found in several methods may take any of |
---|
827 | n/a | the following forms: |
---|
828 | n/a | |
---|
829 | n/a | * An integer between zero and the number of tabs |
---|
830 | n/a | * The name of a child window |
---|
831 | n/a | * A positional specification of the form "@x,y", which |
---|
832 | n/a | defines the tab |
---|
833 | n/a | * The string "current", which identifies the |
---|
834 | n/a | currently-selected tab |
---|
835 | n/a | * The string "end", which returns the number of tabs (only |
---|
836 | n/a | valid for method index) |
---|
837 | n/a | """ |
---|
838 | n/a | Widget.__init__(self, master, "ttk::notebook", kw) |
---|
839 | n/a | |
---|
840 | n/a | |
---|
841 | n/a | def add(self, child, **kw): |
---|
842 | n/a | """Adds a new tab to the notebook. |
---|
843 | n/a | |
---|
844 | n/a | If window is currently managed by the notebook but hidden, it is |
---|
845 | n/a | restored to its previous position.""" |
---|
846 | n/a | self.tk.call(self._w, "add", child, *(_format_optdict(kw))) |
---|
847 | n/a | |
---|
848 | n/a | |
---|
849 | n/a | def forget(self, tab_id): |
---|
850 | n/a | """Removes the tab specified by tab_id, unmaps and unmanages the |
---|
851 | n/a | associated window.""" |
---|
852 | n/a | self.tk.call(self._w, "forget", tab_id) |
---|
853 | n/a | |
---|
854 | n/a | |
---|
855 | n/a | def hide(self, tab_id): |
---|
856 | n/a | """Hides the tab specified by tab_id. |
---|
857 | n/a | |
---|
858 | n/a | The tab will not be displayed, but the associated window remains |
---|
859 | n/a | managed by the notebook and its configuration remembered. Hidden |
---|
860 | n/a | tabs may be restored with the add command.""" |
---|
861 | n/a | self.tk.call(self._w, "hide", tab_id) |
---|
862 | n/a | |
---|
863 | n/a | |
---|
864 | n/a | def identify(self, x, y): |
---|
865 | n/a | """Returns the name of the tab element at position x, y, or the |
---|
866 | n/a | empty string if none.""" |
---|
867 | n/a | return self.tk.call(self._w, "identify", x, y) |
---|
868 | n/a | |
---|
869 | n/a | |
---|
870 | n/a | def index(self, tab_id): |
---|
871 | n/a | """Returns the numeric index of the tab specified by tab_id, or |
---|
872 | n/a | the total number of tabs if tab_id is the string "end".""" |
---|
873 | n/a | return self.tk.getint(self.tk.call(self._w, "index", tab_id)) |
---|
874 | n/a | |
---|
875 | n/a | |
---|
876 | n/a | def insert(self, pos, child, **kw): |
---|
877 | n/a | """Inserts a pane at the specified position. |
---|
878 | n/a | |
---|
879 | n/a | pos is either the string end, an integer index, or the name of |
---|
880 | n/a | a managed child. If child is already managed by the notebook, |
---|
881 | n/a | moves it to the specified position.""" |
---|
882 | n/a | self.tk.call(self._w, "insert", pos, child, *(_format_optdict(kw))) |
---|
883 | n/a | |
---|
884 | n/a | |
---|
885 | n/a | def select(self, tab_id=None): |
---|
886 | n/a | """Selects the specified tab. |
---|
887 | n/a | |
---|
888 | n/a | The associated child window will be displayed, and the |
---|
889 | n/a | previously-selected window (if different) is unmapped. If tab_id |
---|
890 | n/a | is omitted, returns the widget name of the currently selected |
---|
891 | n/a | pane.""" |
---|
892 | n/a | return self.tk.call(self._w, "select", tab_id) |
---|
893 | n/a | |
---|
894 | n/a | |
---|
895 | n/a | def tab(self, tab_id, option=None, **kw): |
---|
896 | n/a | """Query or modify the options of the specific tab_id. |
---|
897 | n/a | |
---|
898 | n/a | If kw is not given, returns a dict of the tab option values. If option |
---|
899 | n/a | is specified, returns the value of that option. Otherwise, sets the |
---|
900 | n/a | options to the corresponding values.""" |
---|
901 | n/a | if option is not None: |
---|
902 | n/a | kw[option] = None |
---|
903 | n/a | return _val_or_dict(self.tk, kw, self._w, "tab", tab_id) |
---|
904 | n/a | |
---|
905 | n/a | |
---|
906 | n/a | def tabs(self): |
---|
907 | n/a | """Returns a list of windows managed by the notebook.""" |
---|
908 | n/a | return self.tk.splitlist(self.tk.call(self._w, "tabs") or ()) |
---|
909 | n/a | |
---|
910 | n/a | |
---|
911 | n/a | def enable_traversal(self): |
---|
912 | n/a | """Enable keyboard traversal for a toplevel window containing |
---|
913 | n/a | this notebook. |
---|
914 | n/a | |
---|
915 | n/a | This will extend the bindings for the toplevel window containing |
---|
916 | n/a | this notebook as follows: |
---|
917 | n/a | |
---|
918 | n/a | Control-Tab: selects the tab following the currently selected |
---|
919 | n/a | one |
---|
920 | n/a | |
---|
921 | n/a | Shift-Control-Tab: selects the tab preceding the currently |
---|
922 | n/a | selected one |
---|
923 | n/a | |
---|
924 | n/a | Alt-K: where K is the mnemonic (underlined) character of any |
---|
925 | n/a | tab, will select that tab. |
---|
926 | n/a | |
---|
927 | n/a | Multiple notebooks in a single toplevel may be enabled for |
---|
928 | n/a | traversal, including nested notebooks. However, notebook traversal |
---|
929 | n/a | only works properly if all panes are direct children of the |
---|
930 | n/a | notebook.""" |
---|
931 | n/a | # The only, and good, difference I see is about mnemonics, which works |
---|
932 | n/a | # after calling this method. Control-Tab and Shift-Control-Tab always |
---|
933 | n/a | # works (here at least). |
---|
934 | n/a | self.tk.call("ttk::notebook::enableTraversal", self._w) |
---|
935 | n/a | |
---|
936 | n/a | |
---|
937 | n/a | class Panedwindow(Widget, tkinter.PanedWindow): |
---|
938 | n/a | """Ttk Panedwindow widget displays a number of subwindows, stacked |
---|
939 | n/a | either vertically or horizontally.""" |
---|
940 | n/a | |
---|
941 | n/a | def __init__(self, master=None, **kw): |
---|
942 | n/a | """Construct a Ttk Panedwindow with parent master. |
---|
943 | n/a | |
---|
944 | n/a | STANDARD OPTIONS |
---|
945 | n/a | |
---|
946 | n/a | class, cursor, style, takefocus |
---|
947 | n/a | |
---|
948 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
949 | n/a | |
---|
950 | n/a | orient, width, height |
---|
951 | n/a | |
---|
952 | n/a | PANE OPTIONS |
---|
953 | n/a | |
---|
954 | n/a | weight |
---|
955 | n/a | """ |
---|
956 | n/a | Widget.__init__(self, master, "ttk::panedwindow", kw) |
---|
957 | n/a | |
---|
958 | n/a | |
---|
959 | n/a | forget = tkinter.PanedWindow.forget # overrides Pack.forget |
---|
960 | n/a | |
---|
961 | n/a | |
---|
962 | n/a | def insert(self, pos, child, **kw): |
---|
963 | n/a | """Inserts a pane at the specified positions. |
---|
964 | n/a | |
---|
965 | n/a | pos is either the string end, and integer index, or the name |
---|
966 | n/a | of a child. If child is already managed by the paned window, |
---|
967 | n/a | moves it to the specified position.""" |
---|
968 | n/a | self.tk.call(self._w, "insert", pos, child, *(_format_optdict(kw))) |
---|
969 | n/a | |
---|
970 | n/a | |
---|
971 | n/a | def pane(self, pane, option=None, **kw): |
---|
972 | n/a | """Query or modify the options of the specified pane. |
---|
973 | n/a | |
---|
974 | n/a | pane is either an integer index or the name of a managed subwindow. |
---|
975 | n/a | If kw is not given, returns a dict of the pane option values. If |
---|
976 | n/a | option is specified then the value for that option is returned. |
---|
977 | n/a | Otherwise, sets the options to the corresponding values.""" |
---|
978 | n/a | if option is not None: |
---|
979 | n/a | kw[option] = None |
---|
980 | n/a | return _val_or_dict(self.tk, kw, self._w, "pane", pane) |
---|
981 | n/a | |
---|
982 | n/a | |
---|
983 | n/a | def sashpos(self, index, newpos=None): |
---|
984 | n/a | """If newpos is specified, sets the position of sash number index. |
---|
985 | n/a | |
---|
986 | n/a | May adjust the positions of adjacent sashes to ensure that |
---|
987 | n/a | positions are monotonically increasing. Sash positions are further |
---|
988 | n/a | constrained to be between 0 and the total size of the widget. |
---|
989 | n/a | |
---|
990 | n/a | Returns the new position of sash number index.""" |
---|
991 | n/a | return self.tk.getint(self.tk.call(self._w, "sashpos", index, newpos)) |
---|
992 | n/a | |
---|
993 | n/a | PanedWindow = Panedwindow # tkinter name compatibility |
---|
994 | n/a | |
---|
995 | n/a | |
---|
996 | n/a | class Progressbar(Widget): |
---|
997 | n/a | """Ttk Progressbar widget shows the status of a long-running |
---|
998 | n/a | operation. They can operate in two modes: determinate mode shows the |
---|
999 | n/a | amount completed relative to the total amount of work to be done, and |
---|
1000 | n/a | indeterminate mode provides an animated display to let the user know |
---|
1001 | n/a | that something is happening.""" |
---|
1002 | n/a | |
---|
1003 | n/a | def __init__(self, master=None, **kw): |
---|
1004 | n/a | """Construct a Ttk Progressbar with parent master. |
---|
1005 | n/a | |
---|
1006 | n/a | STANDARD OPTIONS |
---|
1007 | n/a | |
---|
1008 | n/a | class, cursor, style, takefocus |
---|
1009 | n/a | |
---|
1010 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
1011 | n/a | |
---|
1012 | n/a | orient, length, mode, maximum, value, variable, phase |
---|
1013 | n/a | """ |
---|
1014 | n/a | Widget.__init__(self, master, "ttk::progressbar", kw) |
---|
1015 | n/a | |
---|
1016 | n/a | |
---|
1017 | n/a | def start(self, interval=None): |
---|
1018 | n/a | """Begin autoincrement mode: schedules a recurring timer event |
---|
1019 | n/a | that calls method step every interval milliseconds. |
---|
1020 | n/a | |
---|
1021 | n/a | interval defaults to 50 milliseconds (20 steps/second) if omitted.""" |
---|
1022 | n/a | self.tk.call(self._w, "start", interval) |
---|
1023 | n/a | |
---|
1024 | n/a | |
---|
1025 | n/a | def step(self, amount=None): |
---|
1026 | n/a | """Increments the value option by amount. |
---|
1027 | n/a | |
---|
1028 | n/a | amount defaults to 1.0 if omitted.""" |
---|
1029 | n/a | self.tk.call(self._w, "step", amount) |
---|
1030 | n/a | |
---|
1031 | n/a | |
---|
1032 | n/a | def stop(self): |
---|
1033 | n/a | """Stop autoincrement mode: cancels any recurring timer event |
---|
1034 | n/a | initiated by start.""" |
---|
1035 | n/a | self.tk.call(self._w, "stop") |
---|
1036 | n/a | |
---|
1037 | n/a | |
---|
1038 | n/a | class Radiobutton(Widget): |
---|
1039 | n/a | """Ttk Radiobutton widgets are used in groups to show or change a |
---|
1040 | n/a | set of mutually-exclusive options.""" |
---|
1041 | n/a | |
---|
1042 | n/a | def __init__(self, master=None, **kw): |
---|
1043 | n/a | """Construct a Ttk Radiobutton with parent master. |
---|
1044 | n/a | |
---|
1045 | n/a | STANDARD OPTIONS |
---|
1046 | n/a | |
---|
1047 | n/a | class, compound, cursor, image, state, style, takefocus, |
---|
1048 | n/a | text, textvariable, underline, width |
---|
1049 | n/a | |
---|
1050 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
1051 | n/a | |
---|
1052 | n/a | command, value, variable |
---|
1053 | n/a | """ |
---|
1054 | n/a | Widget.__init__(self, master, "ttk::radiobutton", kw) |
---|
1055 | n/a | |
---|
1056 | n/a | |
---|
1057 | n/a | def invoke(self): |
---|
1058 | n/a | """Sets the option variable to the option value, selects the |
---|
1059 | n/a | widget, and invokes the associated command. |
---|
1060 | n/a | |
---|
1061 | n/a | Returns the result of the command, or an empty string if |
---|
1062 | n/a | no command is specified.""" |
---|
1063 | n/a | return self.tk.call(self._w, "invoke") |
---|
1064 | n/a | |
---|
1065 | n/a | |
---|
1066 | n/a | class Scale(Widget, tkinter.Scale): |
---|
1067 | n/a | """Ttk Scale widget is typically used to control the numeric value of |
---|
1068 | n/a | a linked variable that varies uniformly over some range.""" |
---|
1069 | n/a | |
---|
1070 | n/a | def __init__(self, master=None, **kw): |
---|
1071 | n/a | """Construct a Ttk Scale with parent master. |
---|
1072 | n/a | |
---|
1073 | n/a | STANDARD OPTIONS |
---|
1074 | n/a | |
---|
1075 | n/a | class, cursor, style, takefocus |
---|
1076 | n/a | |
---|
1077 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
1078 | n/a | |
---|
1079 | n/a | command, from, length, orient, to, value, variable |
---|
1080 | n/a | """ |
---|
1081 | n/a | Widget.__init__(self, master, "ttk::scale", kw) |
---|
1082 | n/a | |
---|
1083 | n/a | |
---|
1084 | n/a | def configure(self, cnf=None, **kw): |
---|
1085 | n/a | """Modify or query scale options. |
---|
1086 | n/a | |
---|
1087 | n/a | Setting a value for any of the "from", "from_" or "to" options |
---|
1088 | n/a | generates a <<RangeChanged>> event.""" |
---|
1089 | n/a | if cnf: |
---|
1090 | n/a | kw.update(cnf) |
---|
1091 | n/a | Widget.configure(self, **kw) |
---|
1092 | n/a | if any(['from' in kw, 'from_' in kw, 'to' in kw]): |
---|
1093 | n/a | self.event_generate('<<RangeChanged>>') |
---|
1094 | n/a | |
---|
1095 | n/a | |
---|
1096 | n/a | def get(self, x=None, y=None): |
---|
1097 | n/a | """Get the current value of the value option, or the value |
---|
1098 | n/a | corresponding to the coordinates x, y if they are specified. |
---|
1099 | n/a | |
---|
1100 | n/a | x and y are pixel coordinates relative to the scale widget |
---|
1101 | n/a | origin.""" |
---|
1102 | n/a | return self.tk.call(self._w, 'get', x, y) |
---|
1103 | n/a | |
---|
1104 | n/a | |
---|
1105 | n/a | class Scrollbar(Widget, tkinter.Scrollbar): |
---|
1106 | n/a | """Ttk Scrollbar controls the viewport of a scrollable widget.""" |
---|
1107 | n/a | |
---|
1108 | n/a | def __init__(self, master=None, **kw): |
---|
1109 | n/a | """Construct a Ttk Scrollbar with parent master. |
---|
1110 | n/a | |
---|
1111 | n/a | STANDARD OPTIONS |
---|
1112 | n/a | |
---|
1113 | n/a | class, cursor, style, takefocus |
---|
1114 | n/a | |
---|
1115 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
1116 | n/a | |
---|
1117 | n/a | command, orient |
---|
1118 | n/a | """ |
---|
1119 | n/a | Widget.__init__(self, master, "ttk::scrollbar", kw) |
---|
1120 | n/a | |
---|
1121 | n/a | |
---|
1122 | n/a | class Separator(Widget): |
---|
1123 | n/a | """Ttk Separator widget displays a horizontal or vertical separator |
---|
1124 | n/a | bar.""" |
---|
1125 | n/a | |
---|
1126 | n/a | def __init__(self, master=None, **kw): |
---|
1127 | n/a | """Construct a Ttk Separator with parent master. |
---|
1128 | n/a | |
---|
1129 | n/a | STANDARD OPTIONS |
---|
1130 | n/a | |
---|
1131 | n/a | class, cursor, style, takefocus |
---|
1132 | n/a | |
---|
1133 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
1134 | n/a | |
---|
1135 | n/a | orient |
---|
1136 | n/a | """ |
---|
1137 | n/a | Widget.__init__(self, master, "ttk::separator", kw) |
---|
1138 | n/a | |
---|
1139 | n/a | |
---|
1140 | n/a | class Sizegrip(Widget): |
---|
1141 | n/a | """Ttk Sizegrip allows the user to resize the containing toplevel |
---|
1142 | n/a | window by pressing and dragging the grip.""" |
---|
1143 | n/a | |
---|
1144 | n/a | def __init__(self, master=None, **kw): |
---|
1145 | n/a | """Construct a Ttk Sizegrip with parent master. |
---|
1146 | n/a | |
---|
1147 | n/a | STANDARD OPTIONS |
---|
1148 | n/a | |
---|
1149 | n/a | class, cursor, state, style, takefocus |
---|
1150 | n/a | """ |
---|
1151 | n/a | Widget.__init__(self, master, "ttk::sizegrip", kw) |
---|
1152 | n/a | |
---|
1153 | n/a | |
---|
1154 | n/a | class Treeview(Widget, tkinter.XView, tkinter.YView): |
---|
1155 | n/a | """Ttk Treeview widget displays a hierarchical collection of items. |
---|
1156 | n/a | |
---|
1157 | n/a | Each item has a textual label, an optional image, and an optional list |
---|
1158 | n/a | of data values. The data values are displayed in successive columns |
---|
1159 | n/a | after the tree label.""" |
---|
1160 | n/a | |
---|
1161 | n/a | def __init__(self, master=None, **kw): |
---|
1162 | n/a | """Construct a Ttk Treeview with parent master. |
---|
1163 | n/a | |
---|
1164 | n/a | STANDARD OPTIONS |
---|
1165 | n/a | |
---|
1166 | n/a | class, cursor, style, takefocus, xscrollcommand, |
---|
1167 | n/a | yscrollcommand |
---|
1168 | n/a | |
---|
1169 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
1170 | n/a | |
---|
1171 | n/a | columns, displaycolumns, height, padding, selectmode, show |
---|
1172 | n/a | |
---|
1173 | n/a | ITEM OPTIONS |
---|
1174 | n/a | |
---|
1175 | n/a | text, image, values, open, tags |
---|
1176 | n/a | |
---|
1177 | n/a | TAG OPTIONS |
---|
1178 | n/a | |
---|
1179 | n/a | foreground, background, font, image |
---|
1180 | n/a | """ |
---|
1181 | n/a | Widget.__init__(self, master, "ttk::treeview", kw) |
---|
1182 | n/a | |
---|
1183 | n/a | |
---|
1184 | n/a | def bbox(self, item, column=None): |
---|
1185 | n/a | """Returns the bounding box (relative to the treeview widget's |
---|
1186 | n/a | window) of the specified item in the form x y width height. |
---|
1187 | n/a | |
---|
1188 | n/a | If column is specified, returns the bounding box of that cell. |
---|
1189 | n/a | If the item is not visible (i.e., if it is a descendant of a |
---|
1190 | n/a | closed item or is scrolled offscreen), returns an empty string.""" |
---|
1191 | n/a | return self._getints(self.tk.call(self._w, "bbox", item, column)) or '' |
---|
1192 | n/a | |
---|
1193 | n/a | |
---|
1194 | n/a | def get_children(self, item=None): |
---|
1195 | n/a | """Returns a tuple of children belonging to item. |
---|
1196 | n/a | |
---|
1197 | n/a | If item is not specified, returns root children.""" |
---|
1198 | n/a | return self.tk.splitlist( |
---|
1199 | n/a | self.tk.call(self._w, "children", item or '') or ()) |
---|
1200 | n/a | |
---|
1201 | n/a | |
---|
1202 | n/a | def set_children(self, item, *newchildren): |
---|
1203 | n/a | """Replaces item's child with newchildren. |
---|
1204 | n/a | |
---|
1205 | n/a | Children present in item that are not present in newchildren |
---|
1206 | n/a | are detached from tree. No items in newchildren may be an |
---|
1207 | n/a | ancestor of item.""" |
---|
1208 | n/a | self.tk.call(self._w, "children", item, newchildren) |
---|
1209 | n/a | |
---|
1210 | n/a | |
---|
1211 | n/a | def column(self, column, option=None, **kw): |
---|
1212 | n/a | """Query or modify the options for the specified column. |
---|
1213 | n/a | |
---|
1214 | n/a | If kw is not given, returns a dict of the column option values. If |
---|
1215 | n/a | option is specified then the value for that option is returned. |
---|
1216 | n/a | Otherwise, sets the options to the corresponding values.""" |
---|
1217 | n/a | if option is not None: |
---|
1218 | n/a | kw[option] = None |
---|
1219 | n/a | return _val_or_dict(self.tk, kw, self._w, "column", column) |
---|
1220 | n/a | |
---|
1221 | n/a | |
---|
1222 | n/a | def delete(self, *items): |
---|
1223 | n/a | """Delete all specified items and all their descendants. The root |
---|
1224 | n/a | item may not be deleted.""" |
---|
1225 | n/a | self.tk.call(self._w, "delete", items) |
---|
1226 | n/a | |
---|
1227 | n/a | |
---|
1228 | n/a | def detach(self, *items): |
---|
1229 | n/a | """Unlinks all of the specified items from the tree. |
---|
1230 | n/a | |
---|
1231 | n/a | The items and all of their descendants are still present, and may |
---|
1232 | n/a | be reinserted at another point in the tree, but will not be |
---|
1233 | n/a | displayed. The root item may not be detached.""" |
---|
1234 | n/a | self.tk.call(self._w, "detach", items) |
---|
1235 | n/a | |
---|
1236 | n/a | |
---|
1237 | n/a | def exists(self, item): |
---|
1238 | n/a | """Returns True if the specified item is present in the tree, |
---|
1239 | n/a | False otherwise.""" |
---|
1240 | n/a | return self.tk.getboolean(self.tk.call(self._w, "exists", item)) |
---|
1241 | n/a | |
---|
1242 | n/a | |
---|
1243 | n/a | def focus(self, item=None): |
---|
1244 | n/a | """If item is specified, sets the focus item to item. Otherwise, |
---|
1245 | n/a | returns the current focus item, or '' if there is none.""" |
---|
1246 | n/a | return self.tk.call(self._w, "focus", item) |
---|
1247 | n/a | |
---|
1248 | n/a | |
---|
1249 | n/a | def heading(self, column, option=None, **kw): |
---|
1250 | n/a | """Query or modify the heading options for the specified column. |
---|
1251 | n/a | |
---|
1252 | n/a | If kw is not given, returns a dict of the heading option values. If |
---|
1253 | n/a | option is specified then the value for that option is returned. |
---|
1254 | n/a | Otherwise, sets the options to the corresponding values. |
---|
1255 | n/a | |
---|
1256 | n/a | Valid options/values are: |
---|
1257 | n/a | text: text |
---|
1258 | n/a | The text to display in the column heading |
---|
1259 | n/a | image: image_name |
---|
1260 | n/a | Specifies an image to display to the right of the column |
---|
1261 | n/a | heading |
---|
1262 | n/a | anchor: anchor |
---|
1263 | n/a | Specifies how the heading text should be aligned. One of |
---|
1264 | n/a | the standard Tk anchor values |
---|
1265 | n/a | command: callback |
---|
1266 | n/a | A callback to be invoked when the heading label is |
---|
1267 | n/a | pressed. |
---|
1268 | n/a | |
---|
1269 | n/a | To configure the tree column heading, call this with column = "#0" """ |
---|
1270 | n/a | cmd = kw.get('command') |
---|
1271 | n/a | if cmd and not isinstance(cmd, str): |
---|
1272 | n/a | # callback not registered yet, do it now |
---|
1273 | n/a | kw['command'] = self.master.register(cmd, self._substitute) |
---|
1274 | n/a | |
---|
1275 | n/a | if option is not None: |
---|
1276 | n/a | kw[option] = None |
---|
1277 | n/a | |
---|
1278 | n/a | return _val_or_dict(self.tk, kw, self._w, 'heading', column) |
---|
1279 | n/a | |
---|
1280 | n/a | |
---|
1281 | n/a | def identify(self, component, x, y): |
---|
1282 | n/a | """Returns a description of the specified component under the |
---|
1283 | n/a | point given by x and y, or the empty string if no such component |
---|
1284 | n/a | is present at that position.""" |
---|
1285 | n/a | return self.tk.call(self._w, "identify", component, x, y) |
---|
1286 | n/a | |
---|
1287 | n/a | |
---|
1288 | n/a | def identify_row(self, y): |
---|
1289 | n/a | """Returns the item ID of the item at position y.""" |
---|
1290 | n/a | return self.identify("row", 0, y) |
---|
1291 | n/a | |
---|
1292 | n/a | |
---|
1293 | n/a | def identify_column(self, x): |
---|
1294 | n/a | """Returns the data column identifier of the cell at position x. |
---|
1295 | n/a | |
---|
1296 | n/a | The tree column has ID #0.""" |
---|
1297 | n/a | return self.identify("column", x, 0) |
---|
1298 | n/a | |
---|
1299 | n/a | |
---|
1300 | n/a | def identify_region(self, x, y): |
---|
1301 | n/a | """Returns one of: |
---|
1302 | n/a | |
---|
1303 | n/a | heading: Tree heading area. |
---|
1304 | n/a | separator: Space between two columns headings; |
---|
1305 | n/a | tree: The tree area. |
---|
1306 | n/a | cell: A data cell. |
---|
1307 | n/a | |
---|
1308 | n/a | * Availability: Tk 8.6""" |
---|
1309 | n/a | return self.identify("region", x, y) |
---|
1310 | n/a | |
---|
1311 | n/a | |
---|
1312 | n/a | def identify_element(self, x, y): |
---|
1313 | n/a | """Returns the element at position x, y. |
---|
1314 | n/a | |
---|
1315 | n/a | * Availability: Tk 8.6""" |
---|
1316 | n/a | return self.identify("element", x, y) |
---|
1317 | n/a | |
---|
1318 | n/a | |
---|
1319 | n/a | def index(self, item): |
---|
1320 | n/a | """Returns the integer index of item within its parent's list |
---|
1321 | n/a | of children.""" |
---|
1322 | n/a | return self.tk.getint(self.tk.call(self._w, "index", item)) |
---|
1323 | n/a | |
---|
1324 | n/a | |
---|
1325 | n/a | def insert(self, parent, index, iid=None, **kw): |
---|
1326 | n/a | """Creates a new item and return the item identifier of the newly |
---|
1327 | n/a | created item. |
---|
1328 | n/a | |
---|
1329 | n/a | parent is the item ID of the parent item, or the empty string |
---|
1330 | n/a | to create a new top-level item. index is an integer, or the value |
---|
1331 | n/a | end, specifying where in the list of parent's children to insert |
---|
1332 | n/a | the new item. If index is less than or equal to zero, the new node |
---|
1333 | n/a | is inserted at the beginning, if index is greater than or equal to |
---|
1334 | n/a | the current number of children, it is inserted at the end. If iid |
---|
1335 | n/a | is specified, it is used as the item identifier, iid must not |
---|
1336 | n/a | already exist in the tree. Otherwise, a new unique identifier |
---|
1337 | n/a | is generated.""" |
---|
1338 | n/a | opts = _format_optdict(kw) |
---|
1339 | n/a | if iid: |
---|
1340 | n/a | res = self.tk.call(self._w, "insert", parent, index, |
---|
1341 | n/a | "-id", iid, *opts) |
---|
1342 | n/a | else: |
---|
1343 | n/a | res = self.tk.call(self._w, "insert", parent, index, *opts) |
---|
1344 | n/a | |
---|
1345 | n/a | return res |
---|
1346 | n/a | |
---|
1347 | n/a | |
---|
1348 | n/a | def item(self, item, option=None, **kw): |
---|
1349 | n/a | """Query or modify the options for the specified item. |
---|
1350 | n/a | |
---|
1351 | n/a | If no options are given, a dict with options/values for the item |
---|
1352 | n/a | is returned. If option is specified then the value for that option |
---|
1353 | n/a | is returned. Otherwise, sets the options to the corresponding |
---|
1354 | n/a | values as given by kw.""" |
---|
1355 | n/a | if option is not None: |
---|
1356 | n/a | kw[option] = None |
---|
1357 | n/a | return _val_or_dict(self.tk, kw, self._w, "item", item) |
---|
1358 | n/a | |
---|
1359 | n/a | |
---|
1360 | n/a | def move(self, item, parent, index): |
---|
1361 | n/a | """Moves item to position index in parent's list of children. |
---|
1362 | n/a | |
---|
1363 | n/a | It is illegal to move an item under one of its descendants. If |
---|
1364 | n/a | index is less than or equal to zero, item is moved to the |
---|
1365 | n/a | beginning, if greater than or equal to the number of children, |
---|
1366 | n/a | it is moved to the end. If item was detached it is reattached.""" |
---|
1367 | n/a | self.tk.call(self._w, "move", item, parent, index) |
---|
1368 | n/a | |
---|
1369 | n/a | reattach = move # A sensible method name for reattaching detached items |
---|
1370 | n/a | |
---|
1371 | n/a | |
---|
1372 | n/a | def next(self, item): |
---|
1373 | n/a | """Returns the identifier of item's next sibling, or '' if item |
---|
1374 | n/a | is the last child of its parent.""" |
---|
1375 | n/a | return self.tk.call(self._w, "next", item) |
---|
1376 | n/a | |
---|
1377 | n/a | |
---|
1378 | n/a | def parent(self, item): |
---|
1379 | n/a | """Returns the ID of the parent of item, or '' if item is at the |
---|
1380 | n/a | top level of the hierarchy.""" |
---|
1381 | n/a | return self.tk.call(self._w, "parent", item) |
---|
1382 | n/a | |
---|
1383 | n/a | |
---|
1384 | n/a | def prev(self, item): |
---|
1385 | n/a | """Returns the identifier of item's previous sibling, or '' if |
---|
1386 | n/a | item is the first child of its parent.""" |
---|
1387 | n/a | return self.tk.call(self._w, "prev", item) |
---|
1388 | n/a | |
---|
1389 | n/a | |
---|
1390 | n/a | def see(self, item): |
---|
1391 | n/a | """Ensure that item is visible. |
---|
1392 | n/a | |
---|
1393 | n/a | Sets all of item's ancestors open option to True, and scrolls |
---|
1394 | n/a | the widget if necessary so that item is within the visible |
---|
1395 | n/a | portion of the tree.""" |
---|
1396 | n/a | self.tk.call(self._w, "see", item) |
---|
1397 | n/a | |
---|
1398 | n/a | |
---|
1399 | n/a | def selection(self, selop=_sentinel, items=None): |
---|
1400 | n/a | """Returns the tuple of selected items.""" |
---|
1401 | n/a | if selop is _sentinel: |
---|
1402 | n/a | selop = None |
---|
1403 | n/a | elif selop is None: |
---|
1404 | n/a | import warnings |
---|
1405 | n/a | warnings.warn( |
---|
1406 | n/a | "The selop=None argument of selection() is deprecated " |
---|
1407 | n/a | "and will be removed in Python 3.7", |
---|
1408 | n/a | DeprecationWarning, 3) |
---|
1409 | n/a | elif selop in ('set', 'add', 'remove', 'toggle'): |
---|
1410 | n/a | import warnings |
---|
1411 | n/a | warnings.warn( |
---|
1412 | n/a | "The selop argument of selection() is deprecated " |
---|
1413 | n/a | "and will be removed in Python 3.7, " |
---|
1414 | n/a | "use selection_%s() instead" % (selop,), |
---|
1415 | n/a | DeprecationWarning, 3) |
---|
1416 | n/a | else: |
---|
1417 | n/a | raise TypeError('Unsupported operation') |
---|
1418 | n/a | return self.tk.splitlist(self.tk.call(self._w, "selection", selop, items)) |
---|
1419 | n/a | |
---|
1420 | n/a | |
---|
1421 | n/a | def _selection(self, selop, items): |
---|
1422 | n/a | if len(items) == 1 and isinstance(items[0], (tuple, list)): |
---|
1423 | n/a | items = items[0] |
---|
1424 | n/a | |
---|
1425 | n/a | self.tk.call(self._w, "selection", selop, items) |
---|
1426 | n/a | |
---|
1427 | n/a | |
---|
1428 | n/a | def selection_set(self, *items): |
---|
1429 | n/a | """The specified items becomes the new selection.""" |
---|
1430 | n/a | self._selection("set", items) |
---|
1431 | n/a | |
---|
1432 | n/a | |
---|
1433 | n/a | def selection_add(self, *items): |
---|
1434 | n/a | """Add all of the specified items to the selection.""" |
---|
1435 | n/a | self._selection("add", items) |
---|
1436 | n/a | |
---|
1437 | n/a | |
---|
1438 | n/a | def selection_remove(self, *items): |
---|
1439 | n/a | """Remove all of the specified items from the selection.""" |
---|
1440 | n/a | self._selection("remove", items) |
---|
1441 | n/a | |
---|
1442 | n/a | |
---|
1443 | n/a | def selection_toggle(self, *items): |
---|
1444 | n/a | """Toggle the selection state of each specified item.""" |
---|
1445 | n/a | self._selection("toggle", items) |
---|
1446 | n/a | |
---|
1447 | n/a | |
---|
1448 | n/a | def set(self, item, column=None, value=None): |
---|
1449 | n/a | """Query or set the value of given item. |
---|
1450 | n/a | |
---|
1451 | n/a | With one argument, return a dictionary of column/value pairs |
---|
1452 | n/a | for the specified item. With two arguments, return the current |
---|
1453 | n/a | value of the specified column. With three arguments, set the |
---|
1454 | n/a | value of given column in given item to the specified value.""" |
---|
1455 | n/a | res = self.tk.call(self._w, "set", item, column, value) |
---|
1456 | n/a | if column is None and value is None: |
---|
1457 | n/a | return _splitdict(self.tk, res, |
---|
1458 | n/a | cut_minus=False, conv=_tclobj_to_py) |
---|
1459 | n/a | else: |
---|
1460 | n/a | return res |
---|
1461 | n/a | |
---|
1462 | n/a | |
---|
1463 | n/a | def tag_bind(self, tagname, sequence=None, callback=None): |
---|
1464 | n/a | """Bind a callback for the given event sequence to the tag tagname. |
---|
1465 | n/a | When an event is delivered to an item, the callbacks for each |
---|
1466 | n/a | of the item's tags option are called.""" |
---|
1467 | n/a | self._bind((self._w, "tag", "bind", tagname), sequence, callback, add=0) |
---|
1468 | n/a | |
---|
1469 | n/a | |
---|
1470 | n/a | def tag_configure(self, tagname, option=None, **kw): |
---|
1471 | n/a | """Query or modify the options for the specified tagname. |
---|
1472 | n/a | |
---|
1473 | n/a | If kw is not given, returns a dict of the option settings for tagname. |
---|
1474 | n/a | If option is specified, returns the value for that option for the |
---|
1475 | n/a | specified tagname. Otherwise, sets the options to the corresponding |
---|
1476 | n/a | values for the given tagname.""" |
---|
1477 | n/a | if option is not None: |
---|
1478 | n/a | kw[option] = None |
---|
1479 | n/a | return _val_or_dict(self.tk, kw, self._w, "tag", "configure", |
---|
1480 | n/a | tagname) |
---|
1481 | n/a | |
---|
1482 | n/a | |
---|
1483 | n/a | def tag_has(self, tagname, item=None): |
---|
1484 | n/a | """If item is specified, returns 1 or 0 depending on whether the |
---|
1485 | n/a | specified item has the given tagname. Otherwise, returns a list of |
---|
1486 | n/a | all items which have the specified tag. |
---|
1487 | n/a | |
---|
1488 | n/a | * Availability: Tk 8.6""" |
---|
1489 | n/a | if item is None: |
---|
1490 | n/a | return self.tk.splitlist( |
---|
1491 | n/a | self.tk.call(self._w, "tag", "has", tagname)) |
---|
1492 | n/a | else: |
---|
1493 | n/a | return self.tk.getboolean( |
---|
1494 | n/a | self.tk.call(self._w, "tag", "has", tagname, item)) |
---|
1495 | n/a | |
---|
1496 | n/a | |
---|
1497 | n/a | # Extensions |
---|
1498 | n/a | |
---|
1499 | n/a | class LabeledScale(Frame): |
---|
1500 | n/a | """A Ttk Scale widget with a Ttk Label widget indicating its |
---|
1501 | n/a | current value. |
---|
1502 | n/a | |
---|
1503 | n/a | The Ttk Scale can be accessed through instance.scale, and Ttk Label |
---|
1504 | n/a | can be accessed through instance.label""" |
---|
1505 | n/a | |
---|
1506 | n/a | def __init__(self, master=None, variable=None, from_=0, to=10, **kw): |
---|
1507 | n/a | """Construct a horizontal LabeledScale with parent master, a |
---|
1508 | n/a | variable to be associated with the Ttk Scale widget and its range. |
---|
1509 | n/a | If variable is not specified, a tkinter.IntVar is created. |
---|
1510 | n/a | |
---|
1511 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
1512 | n/a | |
---|
1513 | n/a | compound: 'top' or 'bottom' |
---|
1514 | n/a | Specifies how to display the label relative to the scale. |
---|
1515 | n/a | Defaults to 'top'. |
---|
1516 | n/a | """ |
---|
1517 | n/a | self._label_top = kw.pop('compound', 'top') == 'top' |
---|
1518 | n/a | |
---|
1519 | n/a | Frame.__init__(self, master, **kw) |
---|
1520 | n/a | self._variable = variable or tkinter.IntVar(master) |
---|
1521 | n/a | self._variable.set(from_) |
---|
1522 | n/a | self._last_valid = from_ |
---|
1523 | n/a | |
---|
1524 | n/a | self.label = Label(self) |
---|
1525 | n/a | self.scale = Scale(self, variable=self._variable, from_=from_, to=to) |
---|
1526 | n/a | self.scale.bind('<<RangeChanged>>', self._adjust) |
---|
1527 | n/a | |
---|
1528 | n/a | # position scale and label according to the compound option |
---|
1529 | n/a | scale_side = 'bottom' if self._label_top else 'top' |
---|
1530 | n/a | label_side = 'top' if scale_side == 'bottom' else 'bottom' |
---|
1531 | n/a | self.scale.pack(side=scale_side, fill='x') |
---|
1532 | n/a | tmp = Label(self).pack(side=label_side) # place holder |
---|
1533 | n/a | self.label.place(anchor='n' if label_side == 'top' else 's') |
---|
1534 | n/a | |
---|
1535 | n/a | # update the label as scale or variable changes |
---|
1536 | n/a | self.__tracecb = self._variable.trace_variable('w', self._adjust) |
---|
1537 | n/a | self.bind('<Configure>', self._adjust) |
---|
1538 | n/a | self.bind('<Map>', self._adjust) |
---|
1539 | n/a | |
---|
1540 | n/a | |
---|
1541 | n/a | def destroy(self): |
---|
1542 | n/a | """Destroy this widget and possibly its associated variable.""" |
---|
1543 | n/a | try: |
---|
1544 | n/a | self._variable.trace_vdelete('w', self.__tracecb) |
---|
1545 | n/a | except AttributeError: |
---|
1546 | n/a | # widget has been destroyed already |
---|
1547 | n/a | pass |
---|
1548 | n/a | else: |
---|
1549 | n/a | del self._variable |
---|
1550 | n/a | Frame.destroy(self) |
---|
1551 | n/a | |
---|
1552 | n/a | |
---|
1553 | n/a | def _adjust(self, *args): |
---|
1554 | n/a | """Adjust the label position according to the scale.""" |
---|
1555 | n/a | def adjust_label(): |
---|
1556 | n/a | self.update_idletasks() # "force" scale redraw |
---|
1557 | n/a | |
---|
1558 | n/a | x, y = self.scale.coords() |
---|
1559 | n/a | if self._label_top: |
---|
1560 | n/a | y = self.scale.winfo_y() - self.label.winfo_reqheight() |
---|
1561 | n/a | else: |
---|
1562 | n/a | y = self.scale.winfo_reqheight() + self.label.winfo_reqheight() |
---|
1563 | n/a | |
---|
1564 | n/a | self.label.place_configure(x=x, y=y) |
---|
1565 | n/a | |
---|
1566 | n/a | from_ = _to_number(self.scale['from']) |
---|
1567 | n/a | to = _to_number(self.scale['to']) |
---|
1568 | n/a | if to < from_: |
---|
1569 | n/a | from_, to = to, from_ |
---|
1570 | n/a | newval = self._variable.get() |
---|
1571 | n/a | if not from_ <= newval <= to: |
---|
1572 | n/a | # value outside range, set value back to the last valid one |
---|
1573 | n/a | self.value = self._last_valid |
---|
1574 | n/a | return |
---|
1575 | n/a | |
---|
1576 | n/a | self._last_valid = newval |
---|
1577 | n/a | self.label['text'] = newval |
---|
1578 | n/a | self.after_idle(adjust_label) |
---|
1579 | n/a | |
---|
1580 | n/a | |
---|
1581 | n/a | def _get_value(self): |
---|
1582 | n/a | """Return current scale value.""" |
---|
1583 | n/a | return self._variable.get() |
---|
1584 | n/a | |
---|
1585 | n/a | |
---|
1586 | n/a | def _set_value(self, val): |
---|
1587 | n/a | """Set new scale value.""" |
---|
1588 | n/a | self._variable.set(val) |
---|
1589 | n/a | |
---|
1590 | n/a | |
---|
1591 | n/a | value = property(_get_value, _set_value) |
---|
1592 | n/a | |
---|
1593 | n/a | |
---|
1594 | n/a | class OptionMenu(Menubutton): |
---|
1595 | n/a | """Themed OptionMenu, based after tkinter's OptionMenu, which allows |
---|
1596 | n/a | the user to select a value from a menu.""" |
---|
1597 | n/a | |
---|
1598 | n/a | def __init__(self, master, variable, default=None, *values, **kwargs): |
---|
1599 | n/a | """Construct a themed OptionMenu widget with master as the parent, |
---|
1600 | n/a | the resource textvariable set to variable, the initially selected |
---|
1601 | n/a | value specified by the default parameter, the menu values given by |
---|
1602 | n/a | *values and additional keywords. |
---|
1603 | n/a | |
---|
1604 | n/a | WIDGET-SPECIFIC OPTIONS |
---|
1605 | n/a | |
---|
1606 | n/a | style: stylename |
---|
1607 | n/a | Menubutton style. |
---|
1608 | n/a | direction: 'above', 'below', 'left', 'right', or 'flush' |
---|
1609 | n/a | Menubutton direction. |
---|
1610 | n/a | command: callback |
---|
1611 | n/a | A callback that will be invoked after selecting an item. |
---|
1612 | n/a | """ |
---|
1613 | n/a | kw = {'textvariable': variable, 'style': kwargs.pop('style', None), |
---|
1614 | n/a | 'direction': kwargs.pop('direction', None)} |
---|
1615 | n/a | Menubutton.__init__(self, master, **kw) |
---|
1616 | n/a | self['menu'] = tkinter.Menu(self, tearoff=False) |
---|
1617 | n/a | |
---|
1618 | n/a | self._variable = variable |
---|
1619 | n/a | self._callback = kwargs.pop('command', None) |
---|
1620 | n/a | if kwargs: |
---|
1621 | n/a | raise tkinter.TclError('unknown option -%s' % ( |
---|
1622 | n/a | next(iter(kwargs.keys())))) |
---|
1623 | n/a | |
---|
1624 | n/a | self.set_menu(default, *values) |
---|
1625 | n/a | |
---|
1626 | n/a | |
---|
1627 | n/a | def __getitem__(self, item): |
---|
1628 | n/a | if item == 'menu': |
---|
1629 | n/a | return self.nametowidget(Menubutton.__getitem__(self, item)) |
---|
1630 | n/a | |
---|
1631 | n/a | return Menubutton.__getitem__(self, item) |
---|
1632 | n/a | |
---|
1633 | n/a | |
---|
1634 | n/a | def set_menu(self, default=None, *values): |
---|
1635 | n/a | """Build a new menu of radiobuttons with *values and optionally |
---|
1636 | n/a | a default value.""" |
---|
1637 | n/a | menu = self['menu'] |
---|
1638 | n/a | menu.delete(0, 'end') |
---|
1639 | n/a | for val in values: |
---|
1640 | n/a | menu.add_radiobutton(label=val, |
---|
1641 | n/a | command=tkinter._setit(self._variable, val, self._callback)) |
---|
1642 | n/a | |
---|
1643 | n/a | if default: |
---|
1644 | n/a | self._variable.set(default) |
---|
1645 | n/a | |
---|
1646 | n/a | |
---|
1647 | n/a | def destroy(self): |
---|
1648 | n/a | """Destroy this widget and its associated variable.""" |
---|
1649 | n/a | del self._variable |
---|
1650 | n/a | Menubutton.destroy(self) |
---|