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