Debian Codegraph @

lib-python/3/tkinter/test/test_ttk/test_widgets.py

raw symbols SCIP
1 import unittest
2 import tkinter
3 from tkinter import ttk, TclError
4 from test.support import requires, gc_collect
5 import sys
6
7 from test.test_ttk_textonly import MockTclObj
8 from tkinter.test.support import (AbstractTkTest, tk_version, get_tk_patchlevel,
9 simulate_mouse_click, AbstractDefaultRootTest)
10 from tkinter.test.widget_tests import (add_standard_options,
11 AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests,
12 setUpModule)
13
14 requires('gui')
15
16
17 class StandardTtkOptionsTests(StandardOptionsTests):
18
19 def test_configure_class(self):
20 widget = self.create()
21 self.assertEqual(widget['class'], '')
22 errmsg='attempt to change read-only option'
23 if get_tk_patchlevel(self.root) < (8, 6, 0, 'beta', 3):
24 errmsg='Attempt to change read-only option'
25 self.checkInvalidParam(widget, 'class', 'Foo', errmsg=errmsg)
26 widget2 = self.create(class_='Foo')
27 self.assertEqual(widget2['class'], 'Foo')
28
29 def test_configure_padding(self):
30 widget = self.create()
31 if get_tk_patchlevel(self.root) < (8, 6, 14):
32 def padding_conv(value):
33 self.assertIsInstance(value, tuple)
34 return tuple(map(str, value))
35 else:
36 padding_conv = None
37 self.checkParam(widget, 'padding', 0, expected=(0,), conv=padding_conv)
38 self.checkParam(widget, 'padding', 5, expected=(5,), conv=padding_conv)
39 self.checkParam(widget, 'padding', (5, 6),
40 expected=(5, 6), conv=padding_conv)
41 self.checkParam(widget, 'padding', (5, 6, 7),
42 expected=(5, 6, 7), conv=padding_conv)
43 self.checkParam(widget, 'padding', (5, 6, 7, 8),
44 expected=(5, 6, 7, 8), conv=padding_conv)
45 self.checkParam(widget, 'padding', ('5p', '6p', '7p', '8p'))
46 self.checkParam(widget, 'padding', (), expected='')
47
48 def test_configure_style(self):
49 widget = self.create()
50 self.assertEqual(widget['style'], '')
51 errmsg = 'Layout Foo not found'
52 if hasattr(self, 'default_orient'):
53 errmsg = ('Layout %s.Foo not found' %
54 getattr(self, 'default_orient').title())
55 self.checkInvalidParam(widget, 'style', 'Foo',
56 errmsg=errmsg)
57 widget2 = self.create(class_='Foo')
58 self.assertEqual(widget2['class'], 'Foo')
59 # XXX
60 pass
61
62
63 class WidgetTest(AbstractTkTest, unittest.TestCase):
64 """Tests methods available in every ttk widget."""
65
66 def setUp(self):
67 super().setUp()
68 self.widget = ttk.Button(self.root, width=0, text="Text")
69 self.widget.pack()
70
71 def test_identify(self):
72 self.widget.update()
73 self.assertEqual(self.widget.identify(
74 int(self.widget.winfo_width() / 2),
75 int(self.widget.winfo_height() / 2)
76 ), "label")
77 self.assertEqual(self.widget.identify(-1, -1), "")
78
79 self.assertRaises(tkinter.TclError, self.widget.identify, None, 5)
80 self.assertRaises(tkinter.TclError, self.widget.identify, 5, None)
81 self.assertRaises(tkinter.TclError, self.widget.identify, 5, '')
82
83 def test_widget_state(self):
84 # XXX not sure about the portability of all these tests
85 self.assertEqual(self.widget.state(), ())
86 self.assertEqual(self.widget.instate(['!disabled']), True)
87
88 # changing from !disabled to disabled
89 self.assertEqual(self.widget.state(['disabled']), ('!disabled', ))
90 # no state change
91 self.assertEqual(self.widget.state(['disabled']), ())
92 # change back to !disable but also active
93 self.assertEqual(self.widget.state(['!disabled', 'active']),
94 ('!active', 'disabled'))
95 # no state changes, again
96 self.assertEqual(self.widget.state(['!disabled', 'active']), ())
97 self.assertEqual(self.widget.state(['active', '!disabled']), ())
98
99 def test_cb(arg1, **kw):
100 return arg1, kw
101 self.assertEqual(self.widget.instate(['!disabled'],
102 test_cb, "hi", **{"msg": "there"}),
103 ('hi', {'msg': 'there'}))
104
105 # attempt to set invalid statespec
106 currstate = self.widget.state()
107 self.assertRaises(tkinter.TclError, self.widget.instate,
108 ['badstate'])
109 self.assertRaises(tkinter.TclError, self.widget.instate,
110 ['disabled', 'badstate'])
111 # verify that widget didn't change its state
112 self.assertEqual(currstate, self.widget.state())
113
114 # ensuring that passing None as state doesn't modify current state
115 self.widget.state(['active', '!disabled'])
116 self.assertEqual(self.widget.state(), ('active', ))
117
118
119 class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests):
120 _conv_pixels = False
121
122
123 @add_standard_options(StandardTtkOptionsTests)
124 class FrameTest(AbstractToplevelTest, unittest.TestCase):
125 OPTIONS = (
126 'borderwidth', 'class', 'cursor', 'height',
127 'padding', 'relief', 'style', 'takefocus',
128 'width',
129 )
130
131 def create(self, **kwargs):
132 return ttk.Frame(self.root, **kwargs)
133
134
135 @add_standard_options(StandardTtkOptionsTests)
136 class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
137 OPTIONS = (
138 'borderwidth', 'class', 'cursor', 'height',
139 'labelanchor', 'labelwidget',
140 'padding', 'relief', 'style', 'takefocus',
141 'text', 'underline', 'width',
142 )
143
144 def create(self, **kwargs):
145 return ttk.LabelFrame(self.root, **kwargs)
146
147 def test_configure_labelanchor(self):
148 widget = self.create()
149 self.checkEnumParam(widget, 'labelanchor',
150 'e', 'en', 'es', 'n', 'ne', 'nw', 's', 'se', 'sw', 'w', 'wn', 'ws',
151 errmsg='Bad label anchor specification {}')
152 self.checkInvalidParam(widget, 'labelanchor', 'center')
153
154 def test_configure_labelwidget(self):
155 widget = self.create()
156 label = ttk.Label(self.root, text='Mupp', name='foo')
157 self.checkParam(widget, 'labelwidget', label, expected='.foo')
158 label.destroy()
159
160
161 class AbstractLabelTest(AbstractWidgetTest):
162
163 def checkImageParam(self, widget, name):
164 image = tkinter.PhotoImage(master=self.root, name='image1')
165 image2 = tkinter.PhotoImage(master=self.root, name='image2')
166 self.checkParam(widget, name, image, expected=('image1',))
167 self.checkParam(widget, name, 'image1', expected=('image1',))
168 self.checkParam(widget, name, (image,), expected=('image1',))
169 self.checkParam(widget, name, (image, 'active', image2),
170 expected=('image1', 'active', 'image2'))
171 self.checkParam(widget, name, 'image1 active image2',
172 expected=('image1', 'active', 'image2'))
173 self.checkInvalidParam(widget, name, 'spam',
174 errmsg='image "spam" doesn\'t exist')
175
176 def test_configure_compound(self):
177 options = 'none text image center top bottom left right'.split()
178 errmsg = (
179 'bad compound "{}": must be'
180 f' {", ".join(options[:-1])}, or {options[-1]}'
181 )
182 widget = self.create()
183 self.checkEnumParam(widget, 'compound', *options, errmsg=errmsg)
184
185 def test_configure_state(self):
186 widget = self.create()
187 self.checkParams(widget, 'state', 'active', 'disabled', 'normal')
188
189 def test_configure_width(self):
190 widget = self.create()
191 self.checkParams(widget, 'width', 402, -402, 0)
192
193
194 @add_standard_options(StandardTtkOptionsTests)
195 class LabelTest(AbstractLabelTest, unittest.TestCase):
196 OPTIONS = (
197 'anchor', 'background', 'borderwidth',
198 'class', 'compound', 'cursor', 'font', 'foreground',
199 'image', 'justify', 'padding', 'relief', 'state', 'style',
200 'takefocus', 'text', 'textvariable',
201 'underline', 'width', 'wraplength',
202 )
203 _conv_pixels = False
204
205 def create(self, **kwargs):
206 return ttk.Label(self.root, **kwargs)
207
208 def test_configure_font(self):
209 widget = self.create()
210 self.checkParam(widget, 'font',
211 '-Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*')
212
213
214 @add_standard_options(StandardTtkOptionsTests)
215 class ButtonTest(AbstractLabelTest, unittest.TestCase):
216 OPTIONS = (
217 'class', 'command', 'compound', 'cursor', 'default',
218 'image', 'padding', 'state', 'style',
219 'takefocus', 'text', 'textvariable',
220 'underline', 'width',
221 )
222
223 def create(self, **kwargs):
224 return ttk.Button(self.root, **kwargs)
225
226 def test_configure_default(self):
227 widget = self.create()
228 self.checkEnumParam(widget, 'default', 'normal', 'active', 'disabled')
229
230 def test_invoke(self):
231 success = []
232 btn = ttk.Button(self.root, command=lambda: success.append(1))
233 btn.invoke()
234 self.assertTrue(success)
235
236
237 @add_standard_options(StandardTtkOptionsTests)
238 class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
239 OPTIONS = (
240 'class', 'command', 'compound', 'cursor',
241 'image',
242 'offvalue', 'onvalue',
243 'padding', 'state', 'style',
244 'takefocus', 'text', 'textvariable',
245 'underline', 'variable', 'width',
246 )
247
248 def create(self, **kwargs):
249 return ttk.Checkbutton(self.root, **kwargs)
250
251 def test_configure_offvalue(self):
252 widget = self.create()
253 self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string')
254
255 def test_configure_onvalue(self):
256 widget = self.create()
257 self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string')
258
259 def test_invoke(self):
260 success = []
261 def cb_test():
262 success.append(1)
263 return "cb test called"
264
265 cbtn = ttk.Checkbutton(self.root, command=cb_test)
266 # the variable automatically created by ttk.Checkbutton is actually
267 # undefined till we invoke the Checkbutton
268 self.assertEqual(cbtn.state(), ('alternate', ))
269 self.assertRaises(tkinter.TclError, cbtn.tk.globalgetvar,
270 cbtn['variable'])
271
272 res = cbtn.invoke()
273 self.assertEqual(res, "cb test called")
274 self.assertEqual(cbtn['onvalue'],
275 cbtn.tk.globalgetvar(cbtn['variable']))
276 self.assertTrue(success)
277
278 cbtn['command'] = ''
279 res = cbtn.invoke()
280 self.assertFalse(str(res))
281 self.assertLessEqual(len(success), 1)
282 self.assertEqual(cbtn['offvalue'],
283 cbtn.tk.globalgetvar(cbtn['variable']))
284
285 def test_unique_variables(self):
286 frames = []
287 buttons = []
288 for i in range(2):
289 f = ttk.Frame(self.root)
290 f.pack()
291 frames.append(f)
292 for j in 'AB':
293 b = ttk.Checkbutton(f, text=j)
294 b.pack()
295 buttons.append(b)
296 variables = [str(b['variable']) for b in buttons]
297 self.assertEqual(len(set(variables)), 4, variables)
298
299 def test_unique_variables2(self):
300 buttons = []
301 f = ttk.Frame(self.root)
302 f.pack()
303 f = ttk.Frame(self.root)
304 f.pack()
305 for j in 'AB':
306 b = tkinter.Checkbutton(f, text=j)
307 b.pack()
308 buttons.append(b)
309 # Should be larger than the number of all previously created
310 # tkinter.Checkbutton widgets:
311 for j in range(100):
312 b = ttk.Checkbutton(f, text=str(j))
313 b.pack()
314 buttons.append(b)
315 names = [str(b) for b in buttons]
316 self.assertEqual(len(set(names)), len(buttons), names)
317 variables = [str(b['variable']) for b in buttons]
318 self.assertEqual(len(set(variables)), len(buttons), variables)
319
320
321 @add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
322 class EntryTest(AbstractWidgetTest, unittest.TestCase):
323 OPTIONS = (
324 'background', 'class', 'cursor',
325 'exportselection', 'font', 'foreground',
326 'invalidcommand', 'justify',
327 'show', 'state', 'style', 'takefocus', 'textvariable',
328 'validate', 'validatecommand', 'width', 'xscrollcommand',
329 )
330 IDENTIFY_AS = 'Entry.field' if sys.platform == 'darwin' else 'textarea'
331
332 def setUp(self):
333 super().setUp()
334 self.entry = self.create()
335
336 def create(self, **kwargs):
337 return ttk.Entry(self.root, **kwargs)
338
339 def test_configure_invalidcommand(self):
340 widget = self.create()
341 self.checkCommandParam(widget, 'invalidcommand')
342
343 def test_configure_show(self):
344 widget = self.create()
345 self.checkParam(widget, 'show', '*')
346 self.checkParam(widget, 'show', '')
347 self.checkParam(widget, 'show', ' ')
348
349 def test_configure_state(self):
350 widget = self.create()
351 self.checkParams(widget, 'state',
352 'disabled', 'normal', 'readonly')
353
354 def test_configure_validate(self):
355 widget = self.create()
356 self.checkEnumParam(widget, 'validate',
357 'all', 'key', 'focus', 'focusin', 'focusout', 'none')
358
359 def test_configure_validatecommand(self):
360 widget = self.create()
361 self.checkCommandParam(widget, 'validatecommand')
362
363 def test_bbox(self):
364 self.assertIsBoundingBox(self.entry.bbox(0))
365 self.assertRaises(tkinter.TclError, self.entry.bbox, 'noindex')
366 self.assertRaises(tkinter.TclError, self.entry.bbox, None)
367
368 def test_identify(self):
369 self.entry.pack()
370 self.entry.update()
371
372 # bpo-27313: macOS Cocoa widget differs from X, allow either
373 self.assertEqual(self.entry.identify(5, 5), self.IDENTIFY_AS)
374 self.assertEqual(self.entry.identify(-1, -1), "")
375
376 self.assertRaises(tkinter.TclError, self.entry.identify, None, 5)
377 self.assertRaises(tkinter.TclError, self.entry.identify, 5, None)
378 self.assertRaises(tkinter.TclError, self.entry.identify, 5, '')
379
380 def test_validation_options(self):
381 success = []
382 test_invalid = lambda: success.append(True)
383
384 self.entry['validate'] = 'none'
385 self.entry['validatecommand'] = lambda: False
386
387 self.entry['invalidcommand'] = test_invalid
388 self.entry.validate()
389 self.assertTrue(success)
390
391 self.entry['invalidcommand'] = ''
392 self.entry.validate()
393 self.assertEqual(len(success), 1)
394
395 self.entry['invalidcommand'] = test_invalid
396 self.entry['validatecommand'] = lambda: True
397 self.entry.validate()
398 self.assertEqual(len(success), 1)
399
400 self.entry['validatecommand'] = ''
401 self.entry.validate()
402 self.assertEqual(len(success), 1)
403
404 self.entry['validatecommand'] = True
405 self.assertRaises(tkinter.TclError, self.entry.validate)
406
407 def test_validation(self):
408 validation = []
409 def validate(to_insert):
410 if not 'a' <= to_insert.lower() <= 'z':
411 validation.append(False)
412 return False
413 validation.append(True)
414 return True
415
416 self.entry['validate'] = 'key'
417 self.entry['validatecommand'] = self.entry.register(validate), '%S'
418
419 self.entry.insert('end', 1)
420 self.entry.insert('end', 'a')
421 self.assertEqual(validation, [False, True])
422 self.assertEqual(self.entry.get(), 'a')
423
424 def test_revalidation(self):
425 def validate(content):
426 for letter in content:
427 if not 'a' <= letter.lower() <= 'z':
428 return False
429 return True
430
431 self.entry['validatecommand'] = self.entry.register(validate), '%P'
432
433 self.entry.insert('end', 'avocado')
434 self.assertEqual(self.entry.validate(), True)
435 self.assertEqual(self.entry.state(), ())
436
437 self.entry.delete(0, 'end')
438 self.assertEqual(self.entry.get(), '')
439
440 self.entry.insert('end', 'a1b')
441 self.assertEqual(self.entry.validate(), False)
442 self.assertEqual(self.entry.state(), ('invalid', ))
443
444 self.entry.delete(1)
445 self.assertEqual(self.entry.validate(), True)
446 self.assertEqual(self.entry.state(), ())
447
448
449 @add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
450 class ComboboxTest(EntryTest, unittest.TestCase):
451 OPTIONS = (
452 'background', 'class', 'cursor', 'exportselection',
453 'font', 'foreground', 'height', 'invalidcommand',
454 'justify', 'postcommand', 'show', 'state', 'style',
455 'takefocus', 'textvariable',
456 'validate', 'validatecommand', 'values',
457 'width', 'xscrollcommand',
458 )
459 IDENTIFY_AS = 'Combobox.button' if sys.platform == 'darwin' else 'textarea'
460
461 def setUp(self):
462 super().setUp()
463 self.combo = self.create()
464
465 def create(self, **kwargs):
466 return ttk.Combobox(self.root, **kwargs)
467
468 def test_configure_height(self):
469 widget = self.create()
470 self.checkParams(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i')
471
472 def _show_drop_down_listbox(self):
473 width = self.combo.winfo_width()
474 x, y = width - 5, 5
475 if sys.platform != 'darwin': # there's no down arrow on macOS
476 self.assertRegex(self.combo.identify(x, y), r'.*downarrow\Z')
477 self.combo.event_generate('<ButtonPress-1>', x=x, y=y)
478 self.combo.event_generate('<ButtonRelease-1>', x=x, y=y)
479 self.combo.update_idletasks()
480
481 def test_virtual_event(self):
482 success = []
483
484 self.combo['values'] = [1]
485 self.combo.bind('<<ComboboxSelected>>',
486 lambda evt: success.append(True))
487 self.combo.pack()
488 self.combo.update()
489
490 height = self.combo.winfo_height()
491 self._show_drop_down_listbox()
492 self.combo.update()
493 self.combo.event_generate('<Return>')
494 self.combo.update()
495
496 self.assertTrue(success)
497
498 def test_configure_postcommand(self):
499 success = []
500
501 self.combo['postcommand'] = lambda: success.append(True)
502 self.combo.pack()
503 self.combo.update()
504
505 self._show_drop_down_listbox()
506 self.assertTrue(success)
507
508 # testing postcommand removal
509 self.combo['postcommand'] = ''
510 self._show_drop_down_listbox()
511 self.assertEqual(len(success), 1)
512
513 def test_configure_values(self):
514 def check_get_current(getval, currval):
515 self.assertEqual(self.combo.get(), getval)
516 self.assertEqual(self.combo.current(), currval)
517
518 self.assertEqual(self.combo['values'], '')
519 check_get_current('', -1)
520
521 self.checkParam(self.combo, 'values', 'mon tue wed thur',
522 expected=('mon', 'tue', 'wed', 'thur'))
523 self.checkParam(self.combo, 'values', ('mon', 'tue', 'wed', 'thur'))
524 self.checkParam(self.combo, 'values', (42, 3.14, '', 'any string'))
525 self.checkParam(self.combo, 'values', '')
526
527 self.combo['values'] = ['a', 1, 'c']
528
529 self.combo.set('c')
530 check_get_current('c', 2)
531
532 self.combo.current(0)
533 check_get_current('a', 0)
534
535 self.combo.set('d')
536 check_get_current('d', -1)
537
538 # testing values with empty string
539 self.combo.set('')
540 self.combo['values'] = (1, 2, '', 3)
541 check_get_current('', 2)
542
543 # testing values with empty string set through configure
544 self.combo.configure(values=[1, '', 2])
545 self.assertEqual(self.combo['values'],
546 ('1', '', '2') if self.wantobjects else
547 '1 {} 2')
548
549 # testing values with spaces
550 self.combo['values'] = ['a b', 'a\tb', 'a\nb']
551 self.assertEqual(self.combo['values'],
552 ('a b', 'a\tb', 'a\nb') if self.wantobjects else
553 '{a b} {a\tb} {a\nb}')
554
555 # testing values with special characters
556 self.combo['values'] = [r'a\tb', '"a"', '} {']
557 self.assertEqual(self.combo['values'],
558 (r'a\tb', '"a"', '} {') if self.wantobjects else
559 r'a\\tb {"a"} \}\ \{')
560
561 # out of range
562 self.assertRaises(tkinter.TclError, self.combo.current,
563 len(self.combo['values']))
564 # it expects an integer (or something that can be converted to int)
565 self.assertRaises(tkinter.TclError, self.combo.current, '')
566
567 # testing creating combobox with empty string in values
568 combo2 = ttk.Combobox(self.root, values=[1, 2, ''])
569 self.assertEqual(combo2['values'],
570 ('1', '2', '') if self.wantobjects else '1 2 {}')
571 combo2.destroy()
572
573
574 @add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
575 class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
576 OPTIONS = (
577 'class', 'cursor', 'height',
578 'orient', 'style', 'takefocus', 'width',
579 )
580
581 def setUp(self):
582 super().setUp()
583 self.paned = self.create()
584
585 def create(self, **kwargs):
586 return ttk.PanedWindow(self.root, **kwargs)
587
588 def test_configure_orient(self):
589 widget = self.create()
590 self.assertEqual(str(widget['orient']), 'vertical')
591 errmsg='attempt to change read-only option'
592 if get_tk_patchlevel(self.root) < (8, 6, 0, 'beta', 3):
593 errmsg='Attempt to change read-only option'
594 self.checkInvalidParam(widget, 'orient', 'horizontal',
595 errmsg=errmsg)
596 widget2 = self.create(orient='horizontal')
597 self.assertEqual(str(widget2['orient']), 'horizontal')
598
599 def test_add(self):
600 # attempt to add a child that is not a direct child of the paned window
601 label = ttk.Label(self.paned)
602 child = ttk.Label(label)
603 self.assertRaises(tkinter.TclError, self.paned.add, child)
604 label.destroy()
605 child.destroy()
606 # another attempt
607 label = ttk.Label(self.root)
608 child = ttk.Label(label)
609 self.assertRaises(tkinter.TclError, self.paned.add, child)
610 child.destroy()
611 label.destroy()
612
613 good_child = ttk.Label(self.root)
614 self.paned.add(good_child)
615 # re-adding a child is not accepted
616 self.assertRaises(tkinter.TclError, self.paned.add, good_child)
617
618 other_child = ttk.Label(self.paned)
619 self.paned.add(other_child)
620 self.assertEqual(self.paned.pane(0), self.paned.pane(1))
621 self.assertRaises(tkinter.TclError, self.paned.pane, 2)
622 good_child.destroy()
623 other_child.destroy()
624 self.assertRaises(tkinter.TclError, self.paned.pane, 0)
625
626 def test_forget(self):
627 self.assertRaises(tkinter.TclError, self.paned.forget, None)
628 self.assertRaises(tkinter.TclError, self.paned.forget, 0)
629
630 self.paned.add(ttk.Label(self.root))
631 self.paned.forget(0)
632 self.assertRaises(tkinter.TclError, self.paned.forget, 0)
633
634 def test_insert(self):
635 self.assertRaises(tkinter.TclError, self.paned.insert, None, 0)
636 self.assertRaises(tkinter.TclError, self.paned.insert, 0, None)
637 self.assertRaises(tkinter.TclError, self.paned.insert, 0, 0)
638
639 child = ttk.Label(self.root)
640 child2 = ttk.Label(self.root)
641 child3 = ttk.Label(self.root)
642
643 self.assertRaises(tkinter.TclError, self.paned.insert, 0, child)
644
645 self.paned.insert('end', child2)
646 self.paned.insert(0, child)
647 self.assertEqual(self.paned.panes(), (str(child), str(child2)))
648
649 self.paned.insert(0, child2)
650 self.assertEqual(self.paned.panes(), (str(child2), str(child)))
651
652 self.paned.insert('end', child3)
653 self.assertEqual(self.paned.panes(),
654 (str(child2), str(child), str(child3)))
655
656 # reinserting a child should move it to its current position
657 panes = self.paned.panes()
658 self.paned.insert('end', child3)
659 self.assertEqual(panes, self.paned.panes())
660
661 # moving child3 to child2 position should result in child2 ending up
662 # in previous child position and child ending up in previous child3
663 # position
664 self.paned.insert(child2, child3)
665 self.assertEqual(self.paned.panes(),
666 (str(child3), str(child2), str(child)))
667
668 def test_pane(self):
669 self.assertRaises(tkinter.TclError, self.paned.pane, 0)
670
671 child = ttk.Label(self.root)
672 self.paned.add(child)
673 self.assertIsInstance(self.paned.pane(0), dict)
674 self.assertEqual(self.paned.pane(0, weight=None),
675 0 if self.wantobjects else '0')
676 # newer form for querying a single option
677 self.assertEqual(self.paned.pane(0, 'weight'),
678 0 if self.wantobjects else '0')
679 self.assertEqual(self.paned.pane(0), self.paned.pane(str(child)))
680
681 self.assertRaises(tkinter.TclError, self.paned.pane, 0,
682 badoption='somevalue')
683
684 def test_sashpos(self):
685 self.assertRaises(tkinter.TclError, self.paned.sashpos, None)
686 self.assertRaises(tkinter.TclError, self.paned.sashpos, '')
687 self.assertRaises(tkinter.TclError, self.paned.sashpos, 0)
688
689 child = ttk.Label(self.paned, text='a')
690 self.paned.add(child, weight=1)
691 self.assertRaises(tkinter.TclError, self.paned.sashpos, 0)
692 child2 = ttk.Label(self.paned, text='b')
693 self.paned.add(child2)
694 self.assertRaises(tkinter.TclError, self.paned.sashpos, 1)
695
696 self.paned.pack(expand=True, fill='both')
697
698 curr_pos = self.paned.sashpos(0)
699 self.paned.sashpos(0, 1000)
700 self.assertNotEqual(curr_pos, self.paned.sashpos(0))
701 self.assertIsInstance(self.paned.sashpos(0), int)
702
703
704 @add_standard_options(StandardTtkOptionsTests)
705 class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
706 OPTIONS = (
707 'class', 'command', 'compound', 'cursor',
708 'image',
709 'padding', 'state', 'style',
710 'takefocus', 'text', 'textvariable',
711 'underline', 'value', 'variable', 'width',
712 )
713
714 def create(self, **kwargs):
715 return ttk.Radiobutton(self.root, **kwargs)
716
717 def test_configure_value(self):
718 widget = self.create()
719 self.checkParams(widget, 'value', 1, 2.3, '', 'any string')
720
721 def test_configure_invoke(self):
722 success = []
723 def cb_test():
724 success.append(1)
725 return "cb test called"
726
727 myvar = tkinter.IntVar(self.root)
728 cbtn = ttk.Radiobutton(self.root, command=cb_test,
729 variable=myvar, value=0)
730 cbtn2 = ttk.Radiobutton(self.root, command=cb_test,
731 variable=myvar, value=1)
732
733 if self.wantobjects:
734 conv = lambda x: x
735 else:
736 conv = int
737
738 res = cbtn.invoke()
739 self.assertEqual(res, "cb test called")
740 self.assertEqual(conv(cbtn['value']), myvar.get())
741 self.assertEqual(myvar.get(),
742 conv(cbtn.tk.globalgetvar(cbtn['variable'])))
743 self.assertTrue(success)
744
745 cbtn2['command'] = ''
746 res = cbtn2.invoke()
747 self.assertEqual(str(res), '')
748 self.assertLessEqual(len(success), 1)
749 self.assertEqual(conv(cbtn2['value']), myvar.get())
750 self.assertEqual(myvar.get(),
751 conv(cbtn.tk.globalgetvar(cbtn['variable'])))
752
753 self.assertEqual(str(cbtn['variable']), str(cbtn2['variable']))
754
755
756 class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
757 OPTIONS = (
758 'class', 'compound', 'cursor', 'direction',
759 'image', 'menu', 'padding', 'state', 'style',
760 'takefocus', 'text', 'textvariable',
761 'underline', 'width',
762 )
763
764 def create(self, **kwargs):
765 return ttk.Menubutton(self.root, **kwargs)
766
767 def test_direction(self):
768 widget = self.create()
769 self.checkEnumParam(widget, 'direction',
770 'above', 'below', 'left', 'right', 'flush')
771
772 def test_configure_menu(self):
773 widget = self.create()
774 menu = tkinter.Menu(widget, name='menu')
775 self.checkParam(widget, 'menu', menu, conv=str)
776 menu.destroy()
777
778
779 @add_standard_options(StandardTtkOptionsTests)
780 class ScaleTest(AbstractWidgetTest, unittest.TestCase):
781 OPTIONS = (
782 'class', 'command', 'cursor', 'from', 'length',
783 'orient', 'style', 'takefocus', 'to', 'value', 'variable',
784 )
785 _conv_pixels = False
786 default_orient = 'horizontal'
787
788 def setUp(self):
789 super().setUp()
790 self.scale = self.create()
791 self.scale.pack()
792 self.scale.update()
793
794 def create(self, **kwargs):
795 return ttk.Scale(self.root, **kwargs)
796
797 def test_configure_from(self):
798 widget = self.create()
799 self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=False)
800
801 def test_configure_length(self):
802 widget = self.create()
803 self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i')
804
805 def test_configure_to(self):
806 widget = self.create()
807 self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10, conv=False)
808
809 def test_configure_value(self):
810 widget = self.create()
811 self.checkFloatParam(widget, 'value', 300, 14.9, 15.1, -10, conv=False)
812
813 def test_custom_event(self):
814 failure = [1, 1, 1] # will need to be empty
815
816 funcid = self.scale.bind('<<RangeChanged>>', lambda evt: failure.pop())
817
818 self.scale['from'] = 10
819 self.scale['from_'] = 10
820 self.scale['to'] = 3
821
822 self.assertFalse(failure)
823
824 failure = [1, 1, 1]
825 self.scale.configure(from_=2, to=5)
826 self.scale.configure(from_=0, to=-2)
827 self.scale.configure(to=10)
828
829 self.assertFalse(failure)
830
831 def test_get(self):
832 if self.wantobjects:
833 conv = lambda x: x
834 else:
835 conv = float
836
837 scale_width = self.scale.winfo_width()
838 self.assertEqual(self.scale.get(scale_width, 0), self.scale['to'])
839
840 self.assertEqual(conv(self.scale.get(0, 0)), conv(self.scale['from']))
841 self.assertEqual(self.scale.get(), self.scale['value'])
842 self.scale['value'] = 30
843 self.assertEqual(self.scale.get(), self.scale['value'])
844
845 self.assertRaises(tkinter.TclError, self.scale.get, '', 0)
846 self.assertRaises(tkinter.TclError, self.scale.get, 0, '')
847
848 def test_set(self):
849 if self.wantobjects:
850 conv = lambda x: x
851 else:
852 conv = float
853
854 # set restricts the max/min values according to the current range
855 max = conv(self.scale['to'])
856 new_max = max + 10
857 self.scale.set(new_max)
858 self.assertEqual(conv(self.scale.get()), max)
859 min = conv(self.scale['from'])
860 self.scale.set(min - 1)
861 self.assertEqual(conv(self.scale.get()), min)
862
863 # changing directly the variable doesn't impose this limitation tho
864 var = tkinter.DoubleVar(self.root)
865 self.scale['variable'] = var
866 var.set(max + 5)
867 self.assertEqual(conv(self.scale.get()), var.get())
868 self.assertEqual(conv(self.scale.get()), max + 5)
869 del var
870 gc_collect() # For PyPy or other GCs.
871
872 # the same happens with the value option
873 self.scale['value'] = max + 10
874 self.assertEqual(conv(self.scale.get()), max + 10)
875 self.assertEqual(conv(self.scale.get()), conv(self.scale['value']))
876
877 # nevertheless, note that the max/min values we can get specifying
878 # x, y coords are the ones according to the current range
879 self.assertEqual(conv(self.scale.get(0, 0)), min)
880 self.assertEqual(conv(self.scale.get(self.scale.winfo_width(), 0)), max)
881
882 self.assertRaises(tkinter.TclError, self.scale.set, None)
883
884
885 @add_standard_options(StandardTtkOptionsTests)
886 class ProgressbarTest(AbstractWidgetTest, unittest.TestCase):
887 OPTIONS = (
888 'class', 'cursor', 'orient', 'length',
889 'mode', 'maximum', 'phase',
890 'style', 'takefocus', 'value', 'variable',
891 )
892 _conv_pixels = False
893 default_orient = 'horizontal'
894
895 def create(self, **kwargs):
896 return ttk.Progressbar(self.root, **kwargs)
897
898 def test_configure_length(self):
899 widget = self.create()
900 self.checkPixelsParam(widget, 'length', 100.1, 56.7, '2i')
901
902 def test_configure_maximum(self):
903 widget = self.create()
904 self.checkFloatParam(widget, 'maximum', 150.2, 77.7, 0, -10, conv=False)
905
906 def test_configure_mode(self):
907 widget = self.create()
908 self.checkEnumParam(widget, 'mode', 'determinate', 'indeterminate')
909
910 def test_configure_phase(self):
911 # XXX
912 pass
913
914 def test_configure_value(self):
915 widget = self.create()
916 self.checkFloatParam(widget, 'value', 150.2, 77.7, 0, -10,
917 conv=False)
918
919
920 @unittest.skipIf(sys.platform == 'darwin',
921 'ttk.Scrollbar is special on MacOSX')
922 @add_standard_options(StandardTtkOptionsTests)
923 class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
924 OPTIONS = (
925 'class', 'command', 'cursor', 'orient', 'style', 'takefocus',
926 )
927 default_orient = 'vertical'
928
929 def create(self, **kwargs):
930 return ttk.Scrollbar(self.root, **kwargs)
931
932
933 @add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
934 class NotebookTest(AbstractWidgetTest, unittest.TestCase):
935 OPTIONS = (
936 'class', 'cursor', 'height', 'padding', 'style', 'takefocus', 'width',
937 )
938
939 def setUp(self):
940 super().setUp()
941 self.nb = self.create(padding=0)
942 self.child1 = ttk.Label(self.root)
943 self.child2 = ttk.Label(self.root)
944 self.nb.add(self.child1, text='a')
945 self.nb.add(self.child2, text='b')
946
947 def create(self, **kwargs):
948 return ttk.Notebook(self.root, **kwargs)
949
950 def test_tab_identifiers(self):
951 self.nb.forget(0)
952 self.nb.hide(self.child2)
953 self.assertRaises(tkinter.TclError, self.nb.tab, self.child1)
954 self.assertEqual(self.nb.index('end'), 1)
955 self.nb.add(self.child2)
956 self.assertEqual(self.nb.index('end'), 1)
957 self.nb.select(self.child2)
958
959 self.assertTrue(self.nb.tab('current'))
960 self.nb.add(self.child1, text='a')
961
962 self.nb.pack()
963 self.nb.update()
964 if sys.platform == 'darwin':
965 tb_idx = "@20,5"
966 else:
967 tb_idx = "@5,5"
968 self.assertEqual(self.nb.tab(tb_idx), self.nb.tab('current'))
969
970 for i in range(5, 100, 5):
971 try:
972 if self.nb.tab('@%d, 5' % i, text=None) == 'a':
973 break
974 except tkinter.TclError:
975 pass
976
977 else:
978 self.fail("Tab with text 'a' not found")
979
980 def test_add_and_hidden(self):
981 self.assertRaises(tkinter.TclError, self.nb.hide, -1)
982 self.assertRaises(tkinter.TclError, self.nb.hide, 'hi')
983 self.assertRaises(tkinter.TclError, self.nb.hide, None)
984 self.assertRaises(tkinter.TclError, self.nb.add, None)
985 self.assertRaises(tkinter.TclError, self.nb.add, ttk.Label(self.root),
986 unknown='option')
987
988 tabs = self.nb.tabs()
989 self.nb.hide(self.child1)
990 self.nb.add(self.child1)
991 self.assertEqual(self.nb.tabs(), tabs)
992
993 child = ttk.Label(self.root)
994 self.nb.add(child, text='c')
995 tabs = self.nb.tabs()
996
997 curr = self.nb.index('current')
998 # verify that the tab gets re-added at its previous position
999 child2_index = self.nb.index(self.child2)
1000 self.nb.hide(self.child2)
1001 self.nb.add(self.child2)
1002 self.assertEqual(self.nb.tabs(), tabs)
1003 self.assertEqual(self.nb.index(self.child2), child2_index)
1004 self.assertEqual(str(self.child2), self.nb.tabs()[child2_index])
1005 # but the tab next to it (not hidden) is the one selected now
1006 self.assertEqual(self.nb.index('current'), curr + 1)
1007
1008 def test_forget(self):
1009 self.assertRaises(tkinter.TclError, self.nb.forget, -1)
1010 self.assertRaises(tkinter.TclError, self.nb.forget, 'hi')
1011 self.assertRaises(tkinter.TclError, self.nb.forget, None)
1012
1013 tabs = self.nb.tabs()
1014 child1_index = self.nb.index(self.child1)
1015 self.nb.forget(self.child1)
1016 self.assertNotIn(str(self.child1), self.nb.tabs())
1017 self.assertEqual(len(tabs) - 1, len(self.nb.tabs()))
1018
1019 self.nb.add(self.child1)
1020 self.assertEqual(self.nb.index(self.child1), 1)
1021 self.assertNotEqual(child1_index, self.nb.index(self.child1))
1022
1023 def test_index(self):
1024 self.assertRaises(tkinter.TclError, self.nb.index, -1)
1025 self.assertRaises(tkinter.TclError, self.nb.index, None)
1026
1027 self.assertIsInstance(self.nb.index('end'), int)
1028 self.assertEqual(self.nb.index(self.child1), 0)
1029 self.assertEqual(self.nb.index(self.child2), 1)
1030 self.assertEqual(self.nb.index('end'), 2)
1031
1032 def test_insert(self):
1033 # moving tabs
1034 tabs = self.nb.tabs()
1035 self.nb.insert(1, tabs[0])
1036 self.assertEqual(self.nb.tabs(), (tabs[1], tabs[0]))
1037 self.nb.insert(self.child1, self.child2)
1038 self.assertEqual(self.nb.tabs(), tabs)
1039 self.nb.insert('end', self.child1)
1040 self.assertEqual(self.nb.tabs(), (tabs[1], tabs[0]))
1041 self.nb.insert('end', 0)
1042 self.assertEqual(self.nb.tabs(), tabs)
1043 # bad moves
1044 self.assertRaises(tkinter.TclError, self.nb.insert, 2, tabs[0])
1045 self.assertRaises(tkinter.TclError, self.nb.insert, -1, tabs[0])
1046
1047 # new tab
1048 child3 = ttk.Label(self.root)
1049 self.nb.insert(1, child3)
1050 self.assertEqual(self.nb.tabs(), (tabs[0], str(child3), tabs[1]))
1051 self.nb.forget(child3)
1052 self.assertEqual(self.nb.tabs(), tabs)
1053 self.nb.insert(self.child1, child3)
1054 self.assertEqual(self.nb.tabs(), (str(child3), ) + tabs)
1055 self.nb.forget(child3)
1056 self.assertRaises(tkinter.TclError, self.nb.insert, 2, child3)
1057 self.assertRaises(tkinter.TclError, self.nb.insert, -1, child3)
1058
1059 # bad inserts
1060 self.assertRaises(tkinter.TclError, self.nb.insert, 'end', None)
1061 self.assertRaises(tkinter.TclError, self.nb.insert, None, 0)
1062 self.assertRaises(tkinter.TclError, self.nb.insert, None, None)
1063
1064 def test_select(self):
1065 self.nb.pack()
1066 self.nb.update()
1067
1068 success = []
1069 tab_changed = []
1070
1071 self.child1.bind('<Unmap>', lambda evt: success.append(True))
1072 self.nb.bind('<<NotebookTabChanged>>',
1073 lambda evt: tab_changed.append(True))
1074
1075 self.assertEqual(self.nb.select(), str(self.child1))
1076 self.nb.select(self.child2)
1077 self.assertTrue(success)
1078 self.assertEqual(self.nb.select(), str(self.child2))
1079
1080 self.nb.update()
1081 self.assertTrue(tab_changed)
1082
1083 def test_tab(self):
1084 self.assertRaises(tkinter.TclError, self.nb.tab, -1)
1085 self.assertRaises(tkinter.TclError, self.nb.tab, 'notab')
1086 self.assertRaises(tkinter.TclError, self.nb.tab, None)
1087
1088 self.assertIsInstance(self.nb.tab(self.child1), dict)
1089 self.assertEqual(self.nb.tab(self.child1, text=None), 'a')
1090 # newer form for querying a single option
1091 self.assertEqual(self.nb.tab(self.child1, 'text'), 'a')
1092 self.nb.tab(self.child1, text='abc')
1093 self.assertEqual(self.nb.tab(self.child1, text=None), 'abc')
1094 self.assertEqual(self.nb.tab(self.child1, 'text'), 'abc')
1095
1096 def test_configure_tabs(self):
1097 self.assertEqual(len(self.nb.tabs()), 2)
1098
1099 self.nb.forget(self.child1)
1100 self.nb.forget(self.child2)
1101
1102 self.assertEqual(self.nb.tabs(), ())
1103
1104 def test_traversal(self):
1105 self.nb.pack()
1106 self.nb.update()
1107
1108 self.nb.select(0)
1109
1110 focus_identify_as = 'focus' if sys.platform != 'darwin' else ''
1111 self.assertEqual(self.nb.identify(5, 5), focus_identify_as)
1112 simulate_mouse_click(self.nb, 5, 5)
1113 self.nb.focus_force()
1114 self.nb.event_generate('<Control-Tab>')
1115 self.assertEqual(self.nb.select(), str(self.child2))
1116 self.nb.focus_force()
1117 self.nb.event_generate('<Shift-Control-Tab>')
1118 self.assertEqual(self.nb.select(), str(self.child1))
1119 self.nb.focus_force()
1120 self.nb.event_generate('<Shift-Control-Tab>')
1121 self.assertEqual(self.nb.select(), str(self.child2))
1122
1123 self.nb.tab(self.child1, text='a', underline=0)
1124 self.nb.tab(self.child2, text='e', underline=0)
1125 self.nb.enable_traversal()
1126 self.nb.focus_force()
1127 self.assertEqual(self.nb.identify(5, 5), focus_identify_as)
1128 simulate_mouse_click(self.nb, 5, 5)
1129 # on macOS Emacs-style keyboard shortcuts are region-dependent;
1130 # let's use the regular arrow keys instead
1131 if sys.platform == 'darwin':
1132 begin = '<Left>'
1133 end = '<Right>'
1134 else:
1135 begin = '<Alt-a>'
1136 end = '<Alt-e>'
1137 self.nb.event_generate(begin)
1138 self.assertEqual(self.nb.select(), str(self.child1))
1139 self.nb.event_generate(end)
1140 self.assertEqual(self.nb.select(), str(self.child2))
1141
1142
1143 @add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
1144 class SpinboxTest(EntryTest, unittest.TestCase):
1145 OPTIONS = (
1146 'background', 'class', 'command', 'cursor', 'exportselection',
1147 'font', 'foreground', 'format', 'from', 'increment',
1148 'invalidcommand', 'justify', 'show', 'state', 'style',
1149 'takefocus', 'textvariable', 'to', 'validate', 'validatecommand',
1150 'values', 'width', 'wrap', 'xscrollcommand',
1151 )
1152 IDENTIFY_AS = 'Spinbox.field' if sys.platform == 'darwin' else 'textarea'
1153
1154 def setUp(self):
1155 super().setUp()
1156 self.spin = self.create()
1157 self.spin.pack()
1158
1159 def create(self, **kwargs):
1160 return ttk.Spinbox(self.root, **kwargs)
1161
1162 def _click_increment_arrow(self):
1163 width = self.spin.winfo_width()
1164 height = self.spin.winfo_height()
1165 x = width - 5
1166 y = height//2 - 5
1167 self.assertRegex(self.spin.identify(x, y), r'.*uparrow\Z')
1168 self.spin.event_generate('<ButtonPress-1>', x=x, y=y)
1169 self.spin.event_generate('<ButtonRelease-1>', x=x, y=y)
1170 self.spin.update_idletasks()
1171
1172 def _click_decrement_arrow(self):
1173 width = self.spin.winfo_width()
1174 height = self.spin.winfo_height()
1175 x = width - 5
1176 y = height//2 + 4
1177 self.assertRegex(self.spin.identify(x, y), r'.*downarrow\Z')
1178 self.spin.event_generate('<ButtonPress-1>', x=x, y=y)
1179 self.spin.event_generate('<ButtonRelease-1>', x=x, y=y)
1180 self.spin.update_idletasks()
1181
1182 def test_configure_command(self):
1183 success = []
1184
1185 self.spin['command'] = lambda: success.append(True)
1186 self.spin.update()
1187 self._click_increment_arrow()
1188 self.spin.update()
1189 self.assertTrue(success)
1190
1191 self._click_decrement_arrow()
1192 self.assertEqual(len(success), 2)
1193
1194 # testing postcommand removal
1195 self.spin['command'] = ''
1196 self.spin.update_idletasks()
1197 self._click_increment_arrow()
1198 self._click_decrement_arrow()
1199 self.spin.update()
1200 self.assertEqual(len(success), 2)
1201
1202 def test_configure_to(self):
1203 self.spin['from'] = 0
1204 self.spin['to'] = 5
1205 self.spin.set(4)
1206 self.spin.update()
1207 self._click_increment_arrow() # 5
1208
1209 self.assertEqual(self.spin.get(), '5')
1210
1211 self._click_increment_arrow() # 5
1212 self.assertEqual(self.spin.get(), '5')
1213
1214 def test_configure_from(self):
1215 self.spin['from'] = 1
1216 self.spin['to'] = 10
1217 self.spin.set(2)
1218 self.spin.update()
1219 self._click_decrement_arrow() # 1
1220 self.assertEqual(self.spin.get(), '1')
1221 self._click_decrement_arrow() # 1
1222 self.assertEqual(self.spin.get(), '1')
1223
1224 def test_configure_increment(self):
1225 self.spin['from'] = 0
1226 self.spin['to'] = 10
1227 self.spin['increment'] = 4
1228 self.spin.set(1)
1229 self.spin.update()
1230
1231 self._click_increment_arrow() # 5
1232 self.assertEqual(self.spin.get(), '5')
1233 self.spin['increment'] = 2
1234 self.spin.update()
1235 self._click_decrement_arrow() # 3
1236 self.assertEqual(self.spin.get(), '3')
1237
1238 def test_configure_format(self):
1239 self.spin.set(1)
1240 self.spin['format'] = '%10.3f'
1241 self.spin.update()
1242 self._click_increment_arrow()
1243 value = self.spin.get()
1244
1245 self.assertEqual(len(value), 10)
1246 self.assertEqual(value.index('.'), 6)
1247
1248 self.spin['format'] = ''
1249 self.spin.update()
1250 self._click_increment_arrow()
1251 value = self.spin.get()
1252 self.assertTrue('.' not in value)
1253 self.assertEqual(len(value), 1)
1254
1255 def test_configure_wrap(self):
1256 self.spin['to'] = 10
1257 self.spin['from'] = 1
1258 self.spin.set(1)
1259 self.spin['wrap'] = True
1260 self.spin.update()
1261
1262 self._click_decrement_arrow()
1263 self.assertEqual(self.spin.get(), '10')
1264
1265 self._click_increment_arrow()
1266 self.assertEqual(self.spin.get(), '1')
1267
1268 self.spin['wrap'] = False
1269 self.spin.update()
1270
1271 self._click_decrement_arrow()
1272 self.assertEqual(self.spin.get(), '1')
1273
1274 def test_configure_values(self):
1275 self.assertEqual(self.spin['values'], '')
1276 self.checkParam(self.spin, 'values', 'mon tue wed thur',
1277 expected=('mon', 'tue', 'wed', 'thur'))
1278 self.checkParam(self.spin, 'values', ('mon', 'tue', 'wed', 'thur'))
1279 self.checkParam(self.spin, 'values', (42, 3.14, '', 'any string'))
1280 self.checkParam(self.spin, 'values', '')
1281
1282 self.spin['values'] = ['a', 1, 'c']
1283
1284 # test incrementing / decrementing values
1285 self.spin.set('a')
1286 self.spin.update()
1287 self._click_increment_arrow()
1288 self.assertEqual(self.spin.get(), '1')
1289
1290 self._click_decrement_arrow()
1291 self.assertEqual(self.spin.get(), 'a')
1292
1293 # testing values with empty string set through configure
1294 self.spin.configure(values=[1, '', 2])
1295 self.assertEqual(self.spin['values'],
1296 ('1', '', '2') if self.wantobjects else
1297 '1 {} 2')
1298
1299 # testing values with spaces
1300 self.spin['values'] = ['a b', 'a\tb', 'a\nb']
1301 self.assertEqual(self.spin['values'],
1302 ('a b', 'a\tb', 'a\nb') if self.wantobjects else
1303 '{a b} {a\tb} {a\nb}')
1304
1305 # testing values with special characters
1306 self.spin['values'] = [r'a\tb', '"a"', '} {']
1307 self.assertEqual(self.spin['values'],
1308 (r'a\tb', '"a"', '} {') if self.wantobjects else
1309 r'a\\tb {"a"} \}\ \{')
1310
1311 # testing creating spinbox with empty string in values
1312 spin2 = ttk.Spinbox(self.root, values=[1, 2, ''])
1313 self.assertEqual(spin2['values'],
1314 ('1', '2', '') if self.wantobjects else '1 2 {}')
1315 spin2.destroy()
1316
1317
1318 @add_standard_options(StandardTtkOptionsTests)
1319 class TreeviewTest(AbstractWidgetTest, unittest.TestCase):
1320 OPTIONS = (
1321 'class', 'columns', 'cursor', 'displaycolumns',
1322 'height', 'padding', 'selectmode', 'show',
1323 'style', 'takefocus', 'xscrollcommand', 'yscrollcommand',
1324 )
1325
1326 def setUp(self):
1327 super().setUp()
1328 self.tv = self.create(padding=0)
1329
1330 def create(self, **kwargs):
1331 return ttk.Treeview(self.root, **kwargs)
1332
1333 def test_configure_columns(self):
1334 widget = self.create()
1335 self.checkParam(widget, 'columns', 'a b c',
1336 expected=('a', 'b', 'c'))
1337 self.checkParam(widget, 'columns', ('a', 'b', 'c'))
1338 self.checkParam(widget, 'columns', '')
1339
1340 def test_configure_displaycolumns(self):
1341 widget = self.create()
1342 widget['columns'] = ('a', 'b', 'c')
1343 self.checkParam(widget, 'displaycolumns', 'b a c',
1344 expected=('b', 'a', 'c'))
1345 self.checkParam(widget, 'displaycolumns', ('b', 'a', 'c'))
1346 self.checkParam(widget, 'displaycolumns', '#all',
1347 expected=('#all',))
1348 self.checkParam(widget, 'displaycolumns', (2, 1, 0))
1349 self.checkInvalidParam(widget, 'displaycolumns', ('a', 'b', 'd'),
1350 errmsg='Invalid column index d')
1351 self.checkInvalidParam(widget, 'displaycolumns', (1, 2, 3),
1352 errmsg='Column index 3 out of bounds')
1353 self.checkInvalidParam(widget, 'displaycolumns', (1, -2),
1354 errmsg='Column index -2 out of bounds')
1355
1356 def test_configure_height(self):
1357 widget = self.create()
1358 self.checkPixelsParam(widget, 'height', 100, -100, 0, '3c', conv=False)
1359 self.checkPixelsParam(widget, 'height', 101.2, 102.6, conv=False)
1360
1361 def test_configure_selectmode(self):
1362 widget = self.create()
1363 self.checkEnumParam(widget, 'selectmode',
1364 'none', 'browse', 'extended')
1365
1366 def test_configure_show(self):
1367 widget = self.create()
1368 self.checkParam(widget, 'show', 'tree headings',
1369 expected=('tree', 'headings'))
1370 self.checkParam(widget, 'show', ('tree', 'headings'))
1371 self.checkParam(widget, 'show', ('headings', 'tree'))
1372 self.checkParam(widget, 'show', 'tree', expected=('tree',))
1373 self.checkParam(widget, 'show', 'headings', expected=('headings',))
1374
1375 def test_bbox(self):
1376 self.tv.pack()
1377 self.assertEqual(self.tv.bbox(''), '')
1378 self.tv.update()
1379
1380 item_id = self.tv.insert('', 'end')
1381 children = self.tv.get_children()
1382 self.assertTrue(children)
1383
1384 bbox = self.tv.bbox(children[0])
1385 self.assertIsBoundingBox(bbox)
1386
1387 # compare width in bboxes
1388 self.tv['columns'] = ['test']
1389 self.tv.column('test', width=50)
1390 bbox_column0 = self.tv.bbox(children[0], 0)
1391 root_width = self.tv.column('#0', width=None)
1392 if not self.wantobjects:
1393 root_width = int(root_width)
1394 self.assertEqual(bbox_column0[0], bbox[0] + root_width)
1395
1396 # verify that bbox of a closed item is the empty string
1397 child1 = self.tv.insert(item_id, 'end')
1398 self.assertEqual(self.tv.bbox(child1), '')
1399
1400 def test_children(self):
1401 # no children yet, should get an empty tuple
1402 self.assertEqual(self.tv.get_children(), ())
1403
1404 item_id = self.tv.insert('', 'end')
1405 self.assertIsInstance(self.tv.get_children(), tuple)
1406 self.assertEqual(self.tv.get_children()[0], item_id)
1407
1408 # add item_id and child3 as children of child2
1409 child2 = self.tv.insert('', 'end')
1410 child3 = self.tv.insert('', 'end')
1411 self.tv.set_children(child2, item_id, child3)
1412 self.assertEqual(self.tv.get_children(child2), (item_id, child3))
1413
1414 # child3 has child2 as parent, thus trying to set child2 as a children
1415 # of child3 should result in an error
1416 self.assertRaises(tkinter.TclError,
1417 self.tv.set_children, child3, child2)
1418
1419 # remove child2 children
1420 self.tv.set_children(child2)
1421 self.assertEqual(self.tv.get_children(child2), ())
1422
1423 # remove root's children
1424 self.tv.set_children('')
1425 self.assertEqual(self.tv.get_children(), ())
1426
1427 def test_column(self):
1428 # return a dict with all options/values
1429 self.assertIsInstance(self.tv.column('#0'), dict)
1430 # return a single value of the given option
1431 if self.wantobjects:
1432 self.assertIsInstance(self.tv.column('#0', width=None), int)
1433 # set a new value for an option
1434 self.tv.column('#0', width=10)
1435 # testing new way to get option value
1436 self.assertEqual(self.tv.column('#0', 'width'),
1437 10 if self.wantobjects else '10')
1438 self.assertEqual(self.tv.column('#0', width=None),
1439 10 if self.wantobjects else '10')
1440 # check read-only option
1441 self.assertRaises(tkinter.TclError, self.tv.column, '#0', id='X')
1442
1443 self.assertRaises(tkinter.TclError, self.tv.column, 'invalid')
1444 invalid_kws = [
1445 {'unknown_option': 'some value'}, {'stretch': 'wrong'},
1446 {'anchor': 'wrong'}, {'width': 'wrong'}, {'minwidth': 'wrong'}
1447 ]
1448 for kw in invalid_kws:
1449 self.assertRaises(tkinter.TclError, self.tv.column, '#0',
1450 **kw)
1451
1452 def test_delete(self):
1453 self.assertRaises(tkinter.TclError, self.tv.delete, '#0')
1454
1455 item_id = self.tv.insert('', 'end')
1456 item2 = self.tv.insert(item_id, 'end')
1457 self.assertEqual(self.tv.get_children(), (item_id, ))
1458 self.assertEqual(self.tv.get_children(item_id), (item2, ))
1459
1460 self.tv.delete(item_id)
1461 self.assertFalse(self.tv.get_children())
1462
1463 # reattach should fail
1464 self.assertRaises(tkinter.TclError,
1465 self.tv.reattach, item_id, '', 'end')
1466
1467 # test multiple item delete
1468 item1 = self.tv.insert('', 'end')
1469 item2 = self.tv.insert('', 'end')
1470 self.assertEqual(self.tv.get_children(), (item1, item2))
1471
1472 self.tv.delete(item1, item2)
1473 self.assertFalse(self.tv.get_children())
1474
1475 def test_detach_reattach(self):
1476 item_id = self.tv.insert('', 'end')
1477 item2 = self.tv.insert(item_id, 'end')
1478
1479 # calling detach without items is valid, although it does nothing
1480 prev = self.tv.get_children()
1481 self.tv.detach() # this should do nothing
1482 self.assertEqual(prev, self.tv.get_children())
1483
1484 self.assertEqual(self.tv.get_children(), (item_id, ))
1485 self.assertEqual(self.tv.get_children(item_id), (item2, ))
1486
1487 # detach item with children
1488 self.tv.detach(item_id)
1489 self.assertFalse(self.tv.get_children())
1490
1491 # reattach item with children
1492 self.tv.reattach(item_id, '', 'end')
1493 self.assertEqual(self.tv.get_children(), (item_id, ))
1494 self.assertEqual(self.tv.get_children(item_id), (item2, ))
1495
1496 # move a children to the root
1497 self.tv.move(item2, '', 'end')
1498 self.assertEqual(self.tv.get_children(), (item_id, item2))
1499 self.assertEqual(self.tv.get_children(item_id), ())
1500
1501 # bad values
1502 self.assertRaises(tkinter.TclError,
1503 self.tv.reattach, 'nonexistent', '', 'end')
1504 self.assertRaises(tkinter.TclError,
1505 self.tv.detach, 'nonexistent')
1506 self.assertRaises(tkinter.TclError,
1507 self.tv.reattach, item2, 'otherparent', 'end')
1508 self.assertRaises(tkinter.TclError,
1509 self.tv.reattach, item2, '', 'invalid')
1510
1511 # multiple detach
1512 self.tv.detach(item_id, item2)
1513 self.assertEqual(self.tv.get_children(), ())
1514 self.assertEqual(self.tv.get_children(item_id), ())
1515
1516 def test_exists(self):
1517 self.assertEqual(self.tv.exists('something'), False)
1518 self.assertEqual(self.tv.exists(''), True)
1519 self.assertEqual(self.tv.exists({}), False)
1520
1521 # the following will make a tk.call equivalent to
1522 # tk.call(treeview, "exists") which should result in an error
1523 # in the tcl interpreter since tk requires an item.
1524 self.assertRaises(tkinter.TclError, self.tv.exists, None)
1525
1526 def test_focus(self):
1527 # nothing is focused right now
1528 self.assertEqual(self.tv.focus(), '')
1529
1530 item1 = self.tv.insert('', 'end')
1531 self.tv.focus(item1)
1532 self.assertEqual(self.tv.focus(), item1)
1533
1534 self.tv.delete(item1)
1535 self.assertEqual(self.tv.focus(), '')
1536
1537 # try focusing inexistent item
1538 self.assertRaises(tkinter.TclError, self.tv.focus, 'hi')
1539
1540 def test_heading(self):
1541 # check a dict is returned
1542 self.assertIsInstance(self.tv.heading('#0'), dict)
1543
1544 # check a value is returned
1545 self.tv.heading('#0', text='hi')
1546 self.assertEqual(self.tv.heading('#0', 'text'), 'hi')
1547 self.assertEqual(self.tv.heading('#0', text=None), 'hi')
1548
1549 # invalid option
1550 self.assertRaises(tkinter.TclError, self.tv.heading, '#0',
1551 background=None)
1552 # invalid value
1553 self.assertRaises(tkinter.TclError, self.tv.heading, '#0',
1554 anchor=1)
1555
1556 def test_heading_callback(self):
1557 def simulate_heading_click(x, y):
1558 if tk_version >= (8, 6):
1559 self.assertEqual(self.tv.identify_column(x), '#0')
1560 self.assertEqual(self.tv.identify_region(x, y), 'heading')
1561 simulate_mouse_click(self.tv, x, y)
1562 self.tv.update()
1563
1564 success = [] # no success for now
1565
1566 self.tv.pack()
1567 self.tv.heading('#0', command=lambda: success.append(True))
1568 self.tv.column('#0', width=100)
1569 self.tv.update()
1570
1571 # assuming that the coords (5, 5) fall into heading #0
1572 simulate_heading_click(5, 5)
1573 if not success:
1574 self.fail("The command associated to the treeview heading wasn't "
1575 "invoked.")
1576
1577 success = []
1578 commands = self.tv.master._tclCommands
1579 self.tv.heading('#0', command=str(self.tv.heading('#0', command=None)))
1580 self.assertEqual(commands, self.tv.master._tclCommands)
1581 simulate_heading_click(5, 5)
1582 if not success:
1583 self.fail("The command associated to the treeview heading wasn't "
1584 "invoked.")
1585
1586 # XXX The following raises an error in a tcl interpreter, but not in
1587 # Python
1588 #self.tv.heading('#0', command='I dont exist')
1589 #simulate_heading_click(5, 5)
1590
1591 def test_index(self):
1592 # item 'what' doesn't exist
1593 self.assertRaises(tkinter.TclError, self.tv.index, 'what')
1594
1595 self.assertEqual(self.tv.index(''), 0)
1596
1597 item1 = self.tv.insert('', 'end')
1598 item2 = self.tv.insert('', 'end')
1599 c1 = self.tv.insert(item1, 'end')
1600 c2 = self.tv.insert(item1, 'end')
1601 self.assertEqual(self.tv.index(item1), 0)
1602 self.assertEqual(self.tv.index(c1), 0)
1603 self.assertEqual(self.tv.index(c2), 1)
1604 self.assertEqual(self.tv.index(item2), 1)
1605
1606 self.tv.move(item2, '', 0)
1607 self.assertEqual(self.tv.index(item2), 0)
1608 self.assertEqual(self.tv.index(item1), 1)
1609
1610 # check that index still works even after its parent and siblings
1611 # have been detached
1612 self.tv.detach(item1)
1613 self.assertEqual(self.tv.index(c2), 1)
1614 self.tv.detach(c1)
1615 self.assertEqual(self.tv.index(c2), 0)
1616
1617 # but it fails after item has been deleted
1618 self.tv.delete(item1)
1619 self.assertRaises(tkinter.TclError, self.tv.index, c2)
1620
1621 def test_insert_item(self):
1622 # parent 'none' doesn't exist
1623 self.assertRaises(tkinter.TclError, self.tv.insert, 'none', 'end')
1624
1625 # open values
1626 self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end',
1627 open='')
1628 self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end',
1629 open='please')
1630 self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=True)))
1631 self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=False)))
1632
1633 # invalid index
1634 self.assertRaises(tkinter.TclError, self.tv.insert, '', 'middle')
1635
1636 # trying to duplicate item id is invalid
1637 itemid = self.tv.insert('', 'end', 'first-item')
1638 self.assertEqual(itemid, 'first-item')
1639 self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end',
1640 'first-item')
1641 self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end',
1642 MockTclObj('first-item'))
1643
1644 # unicode values
1645 value = '\xe1ba'
1646 item = self.tv.insert('', 'end', values=(value, ))
1647 self.assertEqual(self.tv.item(item, 'values'),
1648 (value,) if self.wantobjects else value)
1649 self.assertEqual(self.tv.item(item, values=None),
1650 (value,) if self.wantobjects else value)
1651
1652 self.tv.item(item, values=self.root.splitlist(self.tv.item(item, values=None)))
1653 self.assertEqual(self.tv.item(item, values=None),
1654 (value,) if self.wantobjects else value)
1655
1656 self.assertIsInstance(self.tv.item(item), dict)
1657
1658 # erase item values
1659 self.tv.item(item, values='')
1660 self.assertFalse(self.tv.item(item, values=None))
1661
1662 # item tags
1663 item = self.tv.insert('', 'end', tags=[1, 2, value])
1664 self.assertEqual(self.tv.item(item, tags=None),
1665 ('1', '2', value) if self.wantobjects else
1666 '1 2 %s' % value)
1667 self.tv.item(item, tags=[])
1668 self.assertFalse(self.tv.item(item, tags=None))
1669 self.tv.item(item, tags=(1, 2))
1670 self.assertEqual(self.tv.item(item, tags=None),
1671 ('1', '2') if self.wantobjects else '1 2')
1672
1673 # values with spaces
1674 item = self.tv.insert('', 'end', values=('a b c',
1675 '%s %s' % (value, value)))
1676 self.assertEqual(self.tv.item(item, values=None),
1677 ('a b c', '%s %s' % (value, value)) if self.wantobjects else
1678 '{a b c} {%s %s}' % (value, value))
1679
1680 # text
1681 self.assertEqual(self.tv.item(
1682 self.tv.insert('', 'end', text="Label here"), text=None),
1683 "Label here")
1684 self.assertEqual(self.tv.item(
1685 self.tv.insert('', 'end', text=value), text=None),
1686 value)
1687
1688 # test for values which are not None
1689 itemid = self.tv.insert('', 'end', 0)
1690 self.assertEqual(itemid, '0')
1691 itemid = self.tv.insert('', 'end', 0.0)
1692 self.assertEqual(itemid, '0.0')
1693 # this is because False resolves to 0 and element with 0 iid is already present
1694 self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', False)
1695 self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', '')
1696
1697 def test_selection(self):
1698 self.assertRaises(TypeError, self.tv.selection, 'spam')
1699 # item 'none' doesn't exist
1700 self.assertRaises(tkinter.TclError, self.tv.selection_set, 'none')
1701 self.assertRaises(tkinter.TclError, self.tv.selection_add, 'none')
1702 self.assertRaises(tkinter.TclError, self.tv.selection_remove, 'none')
1703 self.assertRaises(tkinter.TclError, self.tv.selection_toggle, 'none')
1704
1705 item1 = self.tv.insert('', 'end')
1706 item2 = self.tv.insert('', 'end')
1707 c1 = self.tv.insert(item1, 'end')
1708 c2 = self.tv.insert(item1, 'end')
1709 c3 = self.tv.insert(item1, 'end')
1710 self.assertEqual(self.tv.selection(), ())
1711
1712 self.tv.selection_set(c1, item2)
1713 self.assertEqual(self.tv.selection(), (c1, item2))
1714 self.tv.selection_set(c2)
1715 self.assertEqual(self.tv.selection(), (c2,))
1716
1717 self.tv.selection_add(c1, item2)
1718 self.assertEqual(self.tv.selection(), (c1, c2, item2))
1719 self.tv.selection_add(item1)
1720 self.assertEqual(self.tv.selection(), (item1, c1, c2, item2))
1721 self.tv.selection_add()
1722 self.assertEqual(self.tv.selection(), (item1, c1, c2, item2))
1723
1724 self.tv.selection_remove(item1, c3)
1725 self.assertEqual(self.tv.selection(), (c1, c2, item2))
1726 self.tv.selection_remove(c2)
1727 self.assertEqual(self.tv.selection(), (c1, item2))
1728 self.tv.selection_remove()
1729 self.assertEqual(self.tv.selection(), (c1, item2))
1730
1731 self.tv.selection_toggle(c1, c3)
1732 self.assertEqual(self.tv.selection(), (c3, item2))
1733 self.tv.selection_toggle(item2)
1734 self.assertEqual(self.tv.selection(), (c3,))
1735 self.tv.selection_toggle()
1736 self.assertEqual(self.tv.selection(), (c3,))
1737
1738 self.tv.insert('', 'end', id='with spaces')
1739 self.tv.selection_set('with spaces')
1740 self.assertEqual(self.tv.selection(), ('with spaces',))
1741
1742 self.tv.insert('', 'end', id='{brace')
1743 self.tv.selection_set('{brace')
1744 self.assertEqual(self.tv.selection(), ('{brace',))
1745
1746 self.tv.insert('', 'end', id='unicode\u20ac')
1747 self.tv.selection_set('unicode\u20ac')
1748 self.assertEqual(self.tv.selection(), ('unicode\u20ac',))
1749
1750 self.tv.insert('', 'end', id=b'bytes\xe2\x82\xac')
1751 self.tv.selection_set(b'bytes\xe2\x82\xac')
1752 self.assertEqual(self.tv.selection(), ('bytes\xe2\x82\xac',))
1753
1754 self.tv.selection_set()
1755 self.assertEqual(self.tv.selection(), ())
1756
1757 # Old interface
1758 self.tv.selection_set((c1, item2))
1759 self.assertEqual(self.tv.selection(), (c1, item2))
1760 self.tv.selection_add((c1, item1))
1761 self.assertEqual(self.tv.selection(), (item1, c1, item2))
1762 self.tv.selection_remove((item1, c3))
1763 self.assertEqual(self.tv.selection(), (c1, item2))
1764 self.tv.selection_toggle((c1, c3))
1765 self.assertEqual(self.tv.selection(), (c3, item2))
1766
1767 def test_set(self):
1768 self.tv['columns'] = ['A', 'B']
1769 item = self.tv.insert('', 'end', values=['a', 'b'])
1770 self.assertEqual(self.tv.set(item), {'A': 'a', 'B': 'b'})
1771
1772 self.tv.set(item, 'B', 'a')
1773 self.assertEqual(self.tv.item(item, values=None),
1774 ('a', 'a') if self.wantobjects else 'a a')
1775
1776 self.tv['columns'] = ['B']
1777 self.assertEqual(self.tv.set(item), {'B': 'a'})
1778
1779 self.tv.set(item, 'B', 'b')
1780 self.assertEqual(self.tv.set(item, column='B'), 'b')
1781 self.assertEqual(self.tv.item(item, values=None),
1782 ('b', 'a') if self.wantobjects else 'b a')
1783
1784 self.tv.set(item, 'B', 123)
1785 self.assertEqual(self.tv.set(item, 'B'),
1786 123 if self.wantobjects else '123')
1787 self.assertEqual(self.tv.item(item, values=None),
1788 (123, 'a') if self.wantobjects else '123 a')
1789 self.assertEqual(self.tv.set(item),
1790 {'B': 123} if self.wantobjects else {'B': '123'})
1791
1792 # inexistent column
1793 self.assertRaises(tkinter.TclError, self.tv.set, item, 'A')
1794 self.assertRaises(tkinter.TclError, self.tv.set, item, 'A', 'b')
1795
1796 # inexistent item
1797 self.assertRaises(tkinter.TclError, self.tv.set, 'notme')
1798
1799 def test_tag_bind(self):
1800 events = []
1801 item1 = self.tv.insert('', 'end', tags=['call'])
1802 item2 = self.tv.insert('', 'end', tags=['call'])
1803 self.tv.tag_bind('call', '<ButtonPress-1>',
1804 lambda evt: events.append(1))
1805 self.tv.tag_bind('call', '<ButtonRelease-1>',
1806 lambda evt: events.append(2))
1807
1808 self.tv.pack()
1809 self.tv.update()
1810
1811 pos_y = set()
1812 found = set()
1813 for i in range(0, 100, 10):
1814 if len(found) == 2: # item1 and item2 already found
1815 break
1816 item_id = self.tv.identify_row(i)
1817 if item_id and item_id not in found:
1818 pos_y.add(i)
1819 found.add(item_id)
1820
1821 self.assertEqual(len(pos_y), 2) # item1 and item2 y pos
1822 for y in pos_y:
1823 simulate_mouse_click(self.tv, 0, y)
1824
1825 # by now there should be 4 things in the events list, since each
1826 # item had a bind for two events that were simulated above
1827 self.assertEqual(len(events), 4)
1828 for evt in zip(events[::2], events[1::2]):
1829 self.assertEqual(evt, (1, 2))
1830
1831 def test_tag_configure(self):
1832 # Just testing parameter passing for now
1833 self.assertRaises(TypeError, self.tv.tag_configure)
1834 self.assertRaises(tkinter.TclError, self.tv.tag_configure,
1835 'test', sky='blue')
1836 self.tv.tag_configure('test', foreground='blue')
1837 self.assertEqual(str(self.tv.tag_configure('test', 'foreground')),
1838 'blue')
1839 self.assertEqual(str(self.tv.tag_configure('test', foreground=None)),
1840 'blue')
1841 self.assertIsInstance(self.tv.tag_configure('test'), dict)
1842
1843 def test_tag_has(self):
1844 item1 = self.tv.insert('', 'end', text='Item 1', tags=['tag1'])
1845 item2 = self.tv.insert('', 'end', text='Item 2', tags=['tag2'])
1846 self.assertRaises(TypeError, self.tv.tag_has)
1847 self.assertRaises(TclError, self.tv.tag_has, 'tag1', 'non-existing')
1848 self.assertTrue(self.tv.tag_has('tag1', item1))
1849 self.assertFalse(self.tv.tag_has('tag1', item2))
1850 self.assertFalse(self.tv.tag_has('tag2', item1))
1851 self.assertTrue(self.tv.tag_has('tag2', item2))
1852 self.assertFalse(self.tv.tag_has('tag3', item1))
1853 self.assertFalse(self.tv.tag_has('tag3', item2))
1854 self.assertEqual(self.tv.tag_has('tag1'), (item1,))
1855 self.assertEqual(self.tv.tag_has('tag2'), (item2,))
1856 self.assertEqual(self.tv.tag_has('tag3'), ())
1857
1858
1859 @add_standard_options(StandardTtkOptionsTests)
1860 class SeparatorTest(AbstractWidgetTest, unittest.TestCase):
1861 OPTIONS = (
1862 'class', 'cursor', 'orient', 'style', 'takefocus',
1863 # 'state'?
1864 )
1865 default_orient = 'horizontal'
1866
1867 def create(self, **kwargs):
1868 return ttk.Separator(self.root, **kwargs)
1869
1870
1871 @add_standard_options(StandardTtkOptionsTests)
1872 class SizegripTest(AbstractWidgetTest, unittest.TestCase):
1873 OPTIONS = (
1874 'class', 'cursor', 'style', 'takefocus',
1875 # 'state'?
1876 )
1877
1878 def create(self, **kwargs):
1879 return ttk.Sizegrip(self.root, **kwargs)
1880
1881
1882 class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase):
1883
1884 def test_frame(self):
1885 self._test_widget(ttk.Frame)
1886
1887 def test_label(self):
1888 self._test_widget(ttk.Label)
1889
1890
1891 tests_gui = (
1892 ButtonTest, CheckbuttonTest, ComboboxTest, EntryTest,
1893 FrameTest, LabelFrameTest, LabelTest, MenubuttonTest,
1894 NotebookTest, PanedWindowTest, ProgressbarTest,
1895 RadiobuttonTest, ScaleTest, ScrollbarTest, SeparatorTest,
1896 SizegripTest, SpinboxTest, TreeviewTest, WidgetTest, DefaultRootTest,
1897 )
1898
1899 if __name__ == "__main__":
1900 unittest.main()