1 | n/a | import sys |
---|
2 | n/a | import unittest |
---|
3 | n/a | import tkinter |
---|
4 | n/a | from tkinter import ttk |
---|
5 | n/a | from test.support import requires, run_unittest, swap_attr |
---|
6 | n/a | from tkinter.test.support import AbstractTkTest, destroy_default_root |
---|
7 | n/a | |
---|
8 | n/a | requires('gui') |
---|
9 | n/a | |
---|
10 | n/a | class LabeledScaleTest(AbstractTkTest, unittest.TestCase): |
---|
11 | n/a | |
---|
12 | n/a | def tearDown(self): |
---|
13 | n/a | self.root.update_idletasks() |
---|
14 | n/a | super().tearDown() |
---|
15 | n/a | |
---|
16 | n/a | def test_widget_destroy(self): |
---|
17 | n/a | # automatically created variable |
---|
18 | n/a | x = ttk.LabeledScale(self.root) |
---|
19 | n/a | var = x._variable._name |
---|
20 | n/a | x.destroy() |
---|
21 | n/a | self.assertRaises(tkinter.TclError, x.tk.globalgetvar, var) |
---|
22 | n/a | |
---|
23 | n/a | # manually created variable |
---|
24 | n/a | myvar = tkinter.DoubleVar(self.root) |
---|
25 | n/a | name = myvar._name |
---|
26 | n/a | x = ttk.LabeledScale(self.root, variable=myvar) |
---|
27 | n/a | x.destroy() |
---|
28 | n/a | if self.wantobjects: |
---|
29 | n/a | self.assertEqual(x.tk.globalgetvar(name), myvar.get()) |
---|
30 | n/a | else: |
---|
31 | n/a | self.assertEqual(float(x.tk.globalgetvar(name)), myvar.get()) |
---|
32 | n/a | del myvar |
---|
33 | n/a | self.assertRaises(tkinter.TclError, x.tk.globalgetvar, name) |
---|
34 | n/a | |
---|
35 | n/a | # checking that the tracing callback is properly removed |
---|
36 | n/a | myvar = tkinter.IntVar(self.root) |
---|
37 | n/a | # LabeledScale will start tracing myvar |
---|
38 | n/a | x = ttk.LabeledScale(self.root, variable=myvar) |
---|
39 | n/a | x.destroy() |
---|
40 | n/a | # Unless the tracing callback was removed, creating a new |
---|
41 | n/a | # LabeledScale with the same var will cause an error now. This |
---|
42 | n/a | # happens because the variable will be set to (possibly) a new |
---|
43 | n/a | # value which causes the tracing callback to be called and then |
---|
44 | n/a | # it tries calling instance attributes not yet defined. |
---|
45 | n/a | ttk.LabeledScale(self.root, variable=myvar) |
---|
46 | n/a | if hasattr(sys, 'last_type'): |
---|
47 | n/a | self.assertNotEqual(sys.last_type, tkinter.TclError) |
---|
48 | n/a | |
---|
49 | n/a | |
---|
50 | n/a | def test_initialization_no_master(self): |
---|
51 | n/a | # no master passing |
---|
52 | n/a | with swap_attr(tkinter, '_default_root', None), \ |
---|
53 | n/a | swap_attr(tkinter, '_support_default_root', True): |
---|
54 | n/a | try: |
---|
55 | n/a | x = ttk.LabeledScale() |
---|
56 | n/a | self.assertIsNotNone(tkinter._default_root) |
---|
57 | n/a | self.assertEqual(x.master, tkinter._default_root) |
---|
58 | n/a | self.assertEqual(x.tk, tkinter._default_root.tk) |
---|
59 | n/a | x.destroy() |
---|
60 | n/a | finally: |
---|
61 | n/a | destroy_default_root() |
---|
62 | n/a | |
---|
63 | n/a | def test_initialization(self): |
---|
64 | n/a | # master passing |
---|
65 | n/a | master = tkinter.Frame(self.root) |
---|
66 | n/a | x = ttk.LabeledScale(master) |
---|
67 | n/a | self.assertEqual(x.master, master) |
---|
68 | n/a | x.destroy() |
---|
69 | n/a | |
---|
70 | n/a | # variable initialization/passing |
---|
71 | n/a | passed_expected = (('0', 0), (0, 0), (10, 10), |
---|
72 | n/a | (-1, -1), (sys.maxsize + 1, sys.maxsize + 1), |
---|
73 | n/a | (2.5, 2), ('2.5', 2)) |
---|
74 | n/a | for pair in passed_expected: |
---|
75 | n/a | x = ttk.LabeledScale(self.root, from_=pair[0]) |
---|
76 | n/a | self.assertEqual(x.value, pair[1]) |
---|
77 | n/a | x.destroy() |
---|
78 | n/a | x = ttk.LabeledScale(self.root, from_=None) |
---|
79 | n/a | self.assertRaises((ValueError, tkinter.TclError), x._variable.get) |
---|
80 | n/a | x.destroy() |
---|
81 | n/a | # variable should have its default value set to the from_ value |
---|
82 | n/a | myvar = tkinter.DoubleVar(self.root, value=20) |
---|
83 | n/a | x = ttk.LabeledScale(self.root, variable=myvar) |
---|
84 | n/a | self.assertEqual(x.value, 0) |
---|
85 | n/a | x.destroy() |
---|
86 | n/a | # check that it is really using a DoubleVar |
---|
87 | n/a | x = ttk.LabeledScale(self.root, variable=myvar, from_=0.5) |
---|
88 | n/a | self.assertEqual(x.value, 0.5) |
---|
89 | n/a | self.assertEqual(x._variable._name, myvar._name) |
---|
90 | n/a | x.destroy() |
---|
91 | n/a | |
---|
92 | n/a | # widget positionment |
---|
93 | n/a | def check_positions(scale, scale_pos, label, label_pos): |
---|
94 | n/a | self.assertEqual(scale.pack_info()['side'], scale_pos) |
---|
95 | n/a | self.assertEqual(label.place_info()['anchor'], label_pos) |
---|
96 | n/a | x = ttk.LabeledScale(self.root, compound='top') |
---|
97 | n/a | check_positions(x.scale, 'bottom', x.label, 'n') |
---|
98 | n/a | x.destroy() |
---|
99 | n/a | x = ttk.LabeledScale(self.root, compound='bottom') |
---|
100 | n/a | check_positions(x.scale, 'top', x.label, 's') |
---|
101 | n/a | x.destroy() |
---|
102 | n/a | # invert default positions |
---|
103 | n/a | x = ttk.LabeledScale(self.root, compound='unknown') |
---|
104 | n/a | check_positions(x.scale, 'top', x.label, 's') |
---|
105 | n/a | x.destroy() |
---|
106 | n/a | x = ttk.LabeledScale(self.root) # take default positions |
---|
107 | n/a | check_positions(x.scale, 'bottom', x.label, 'n') |
---|
108 | n/a | x.destroy() |
---|
109 | n/a | |
---|
110 | n/a | # extra, and invalid, kwargs |
---|
111 | n/a | self.assertRaises(tkinter.TclError, ttk.LabeledScale, master, a='b') |
---|
112 | n/a | |
---|
113 | n/a | |
---|
114 | n/a | def test_horizontal_range(self): |
---|
115 | n/a | lscale = ttk.LabeledScale(self.root, from_=0, to=10) |
---|
116 | n/a | lscale.pack() |
---|
117 | n/a | lscale.wait_visibility() |
---|
118 | n/a | lscale.update() |
---|
119 | n/a | |
---|
120 | n/a | linfo_1 = lscale.label.place_info() |
---|
121 | n/a | prev_xcoord = lscale.scale.coords()[0] |
---|
122 | n/a | self.assertEqual(prev_xcoord, int(linfo_1['x'])) |
---|
123 | n/a | # change range to: from -5 to 5. This should change the x coord of |
---|
124 | n/a | # the scale widget, since 0 is at the middle of the new |
---|
125 | n/a | # range. |
---|
126 | n/a | lscale.scale.configure(from_=-5, to=5) |
---|
127 | n/a | # The following update is needed since the test doesn't use mainloop, |
---|
128 | n/a | # at the same time this shouldn't affect test outcome |
---|
129 | n/a | lscale.update() |
---|
130 | n/a | curr_xcoord = lscale.scale.coords()[0] |
---|
131 | n/a | self.assertNotEqual(prev_xcoord, curr_xcoord) |
---|
132 | n/a | # the label widget should have been repositioned too |
---|
133 | n/a | linfo_2 = lscale.label.place_info() |
---|
134 | n/a | self.assertEqual(lscale.label['text'], 0 if self.wantobjects else '0') |
---|
135 | n/a | self.assertEqual(curr_xcoord, int(linfo_2['x'])) |
---|
136 | n/a | # change the range back |
---|
137 | n/a | lscale.scale.configure(from_=0, to=10) |
---|
138 | n/a | self.assertNotEqual(prev_xcoord, curr_xcoord) |
---|
139 | n/a | self.assertEqual(prev_xcoord, int(linfo_1['x'])) |
---|
140 | n/a | |
---|
141 | n/a | lscale.destroy() |
---|
142 | n/a | |
---|
143 | n/a | |
---|
144 | n/a | def test_variable_change(self): |
---|
145 | n/a | x = ttk.LabeledScale(self.root) |
---|
146 | n/a | x.pack() |
---|
147 | n/a | x.wait_visibility() |
---|
148 | n/a | x.update() |
---|
149 | n/a | |
---|
150 | n/a | curr_xcoord = x.scale.coords()[0] |
---|
151 | n/a | newval = x.value + 1 |
---|
152 | n/a | x.value = newval |
---|
153 | n/a | # The following update is needed since the test doesn't use mainloop, |
---|
154 | n/a | # at the same time this shouldn't affect test outcome |
---|
155 | n/a | x.update() |
---|
156 | n/a | self.assertEqual(x.value, newval) |
---|
157 | n/a | self.assertEqual(x.label['text'], |
---|
158 | n/a | newval if self.wantobjects else str(newval)) |
---|
159 | n/a | self.assertEqual(float(x.scale.get()), newval) |
---|
160 | n/a | self.assertGreater(x.scale.coords()[0], curr_xcoord) |
---|
161 | n/a | self.assertEqual(x.scale.coords()[0], |
---|
162 | n/a | int(x.label.place_info()['x'])) |
---|
163 | n/a | |
---|
164 | n/a | # value outside range |
---|
165 | n/a | if self.wantobjects: |
---|
166 | n/a | conv = lambda x: x |
---|
167 | n/a | else: |
---|
168 | n/a | conv = int |
---|
169 | n/a | x.value = conv(x.scale['to']) + 1 # no changes shouldn't happen |
---|
170 | n/a | x.update() |
---|
171 | n/a | self.assertEqual(x.value, newval) |
---|
172 | n/a | self.assertEqual(conv(x.label['text']), newval) |
---|
173 | n/a | self.assertEqual(float(x.scale.get()), newval) |
---|
174 | n/a | self.assertEqual(x.scale.coords()[0], |
---|
175 | n/a | int(x.label.place_info()['x'])) |
---|
176 | n/a | |
---|
177 | n/a | # non-integer value |
---|
178 | n/a | x.value = newval = newval + 1.5 |
---|
179 | n/a | x.update() |
---|
180 | n/a | self.assertEqual(x.value, int(newval)) |
---|
181 | n/a | self.assertEqual(conv(x.label['text']), int(newval)) |
---|
182 | n/a | self.assertEqual(float(x.scale.get()), newval) |
---|
183 | n/a | |
---|
184 | n/a | x.destroy() |
---|
185 | n/a | |
---|
186 | n/a | |
---|
187 | n/a | def test_resize(self): |
---|
188 | n/a | x = ttk.LabeledScale(self.root) |
---|
189 | n/a | x.pack(expand=True, fill='both') |
---|
190 | n/a | x.wait_visibility() |
---|
191 | n/a | x.update() |
---|
192 | n/a | |
---|
193 | n/a | width, height = x.master.winfo_width(), x.master.winfo_height() |
---|
194 | n/a | width_new, height_new = width * 2, height * 2 |
---|
195 | n/a | |
---|
196 | n/a | x.value = 3 |
---|
197 | n/a | x.update() |
---|
198 | n/a | x.master.wm_geometry("%dx%d" % (width_new, height_new)) |
---|
199 | n/a | self.assertEqual(int(x.label.place_info()['x']), |
---|
200 | n/a | x.scale.coords()[0]) |
---|
201 | n/a | |
---|
202 | n/a | # Reset geometry |
---|
203 | n/a | x.master.wm_geometry("%dx%d" % (width, height)) |
---|
204 | n/a | x.destroy() |
---|
205 | n/a | |
---|
206 | n/a | |
---|
207 | n/a | class OptionMenuTest(AbstractTkTest, unittest.TestCase): |
---|
208 | n/a | |
---|
209 | n/a | def setUp(self): |
---|
210 | n/a | super().setUp() |
---|
211 | n/a | self.textvar = tkinter.StringVar(self.root) |
---|
212 | n/a | |
---|
213 | n/a | def tearDown(self): |
---|
214 | n/a | del self.textvar |
---|
215 | n/a | super().tearDown() |
---|
216 | n/a | |
---|
217 | n/a | |
---|
218 | n/a | def test_widget_destroy(self): |
---|
219 | n/a | var = tkinter.StringVar(self.root) |
---|
220 | n/a | optmenu = ttk.OptionMenu(self.root, var) |
---|
221 | n/a | name = var._name |
---|
222 | n/a | optmenu.update_idletasks() |
---|
223 | n/a | optmenu.destroy() |
---|
224 | n/a | self.assertEqual(optmenu.tk.globalgetvar(name), var.get()) |
---|
225 | n/a | del var |
---|
226 | n/a | self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name) |
---|
227 | n/a | |
---|
228 | n/a | |
---|
229 | n/a | def test_initialization(self): |
---|
230 | n/a | self.assertRaises(tkinter.TclError, |
---|
231 | n/a | ttk.OptionMenu, self.root, self.textvar, invalid='thing') |
---|
232 | n/a | |
---|
233 | n/a | optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b') |
---|
234 | n/a | self.assertEqual(optmenu._variable.get(), 'b') |
---|
235 | n/a | |
---|
236 | n/a | self.assertTrue(optmenu['menu']) |
---|
237 | n/a | self.assertTrue(optmenu['textvariable']) |
---|
238 | n/a | |
---|
239 | n/a | optmenu.destroy() |
---|
240 | n/a | |
---|
241 | n/a | |
---|
242 | n/a | def test_menu(self): |
---|
243 | n/a | items = ('a', 'b', 'c') |
---|
244 | n/a | default = 'a' |
---|
245 | n/a | optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items) |
---|
246 | n/a | found_default = False |
---|
247 | n/a | for i in range(len(items)): |
---|
248 | n/a | value = optmenu['menu'].entrycget(i, 'value') |
---|
249 | n/a | self.assertEqual(value, items[i]) |
---|
250 | n/a | if value == default: |
---|
251 | n/a | found_default = True |
---|
252 | n/a | self.assertTrue(found_default) |
---|
253 | n/a | optmenu.destroy() |
---|
254 | n/a | |
---|
255 | n/a | # default shouldn't be in menu if it is not part of values |
---|
256 | n/a | default = 'd' |
---|
257 | n/a | optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items) |
---|
258 | n/a | curr = None |
---|
259 | n/a | i = 0 |
---|
260 | n/a | while True: |
---|
261 | n/a | last, curr = curr, optmenu['menu'].entryconfigure(i, 'value') |
---|
262 | n/a | if last == curr: |
---|
263 | n/a | # no more menu entries |
---|
264 | n/a | break |
---|
265 | n/a | self.assertNotEqual(curr, default) |
---|
266 | n/a | i += 1 |
---|
267 | n/a | self.assertEqual(i, len(items)) |
---|
268 | n/a | |
---|
269 | n/a | # check that variable is updated correctly |
---|
270 | n/a | optmenu.pack() |
---|
271 | n/a | optmenu.wait_visibility() |
---|
272 | n/a | optmenu['menu'].invoke(0) |
---|
273 | n/a | self.assertEqual(optmenu._variable.get(), items[0]) |
---|
274 | n/a | |
---|
275 | n/a | # changing to an invalid index shouldn't change the variable |
---|
276 | n/a | self.assertRaises(tkinter.TclError, optmenu['menu'].invoke, -1) |
---|
277 | n/a | self.assertEqual(optmenu._variable.get(), items[0]) |
---|
278 | n/a | |
---|
279 | n/a | optmenu.destroy() |
---|
280 | n/a | |
---|
281 | n/a | # specifying a callback |
---|
282 | n/a | success = [] |
---|
283 | n/a | def cb_test(item): |
---|
284 | n/a | self.assertEqual(item, items[1]) |
---|
285 | n/a | success.append(True) |
---|
286 | n/a | optmenu = ttk.OptionMenu(self.root, self.textvar, 'a', command=cb_test, |
---|
287 | n/a | *items) |
---|
288 | n/a | optmenu['menu'].invoke(1) |
---|
289 | n/a | if not success: |
---|
290 | n/a | self.fail("Menu callback not invoked") |
---|
291 | n/a | |
---|
292 | n/a | optmenu.destroy() |
---|
293 | n/a | |
---|
294 | n/a | |
---|
295 | n/a | tests_gui = (LabeledScaleTest, OptionMenuTest) |
---|
296 | n/a | |
---|
297 | n/a | if __name__ == "__main__": |
---|
298 | n/a | run_unittest(*tests_gui) |
---|