Debian Codegraph @

Lib/test/test_ttk/test_extensions.py

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