Debian Codegraph @

Lib/test/test_ttk/test_style.py

raw
1 import unittest
2 import sys
3 import tkinter
4 from tkinter import ttk
5 from tkinter import TclError
6 from test import support
7 from test.support import requires
8 from test.test_tkinter.support import setUpModule # noqa: F401
9 from test.test_tkinter.support import AbstractTkTest, get_tk_patchlevel
10
11 requires('gui')
12
13 CLASS_NAMES = [
14 '.', 'ComboboxPopdownFrame', 'Heading',
15 'Horizontal.TProgressbar', 'Horizontal.TScale', 'Item', 'Sash',
16 'TButton', 'TCheckbutton', 'TCombobox', 'TEntry',
17 'TLabelframe', 'TLabelframe.Label', 'TMenubutton',
18 'TNotebook', 'TNotebook.Tab', 'Toolbutton', 'TProgressbar',
19 'TRadiobutton', 'Treeview', 'TScale', 'TScrollbar', 'TSpinbox',
20 'Vertical.TProgressbar', 'Vertical.TScale'
21 ]
22
23 class StyleTest(AbstractTkTest, unittest.TestCase):
24
25 def setUp(self):
26 super().setUp()
27 self.style = ttk.Style(self.root)
28
29
30 def test_configure(self):
31 style = self.style
32 style.configure('TButton', background='yellow')
33 self.assertEqual(style.configure('TButton', 'background'),
34 'yellow')
35 self.assertIsInstance(style.configure('TButton'), dict)
36
37
38 def test_map(self):
39 style = self.style
40
41 # Single state
42 for states in ['active'], [('active',)]:
43 with self.subTest(states=states):
44 style.map('TButton', background=[(*states, 'white')])
45 expected = [('active', 'white')]
46 self.assertEqual(style.map('TButton', 'background'), expected)
47 m = style.map('TButton')
48 self.assertIsInstance(m, dict)
49 self.assertEqual(m['background'], expected)
50
51 # Multiple states
52 for states in ['pressed', '!disabled'], ['pressed !disabled'], [('pressed', '!disabled')]:
53 with self.subTest(states=states):
54 style.map('TButton', background=[(*states, 'black')])
55 expected = [('pressed', '!disabled', 'black')]
56 self.assertEqual(style.map('TButton', 'background'), expected)
57 m = style.map('TButton')
58 self.assertIsInstance(m, dict)
59 self.assertEqual(m['background'], expected)
60
61 # Default state
62 for states in [], [''], [()]:
63 with self.subTest(states=states):
64 style.map('TButton', background=[(*states, 'grey')])
65 expected = [('grey',)]
66 self.assertEqual(style.map('TButton', 'background'), expected)
67 m = style.map('TButton')
68 self.assertIsInstance(m, dict)
69 self.assertEqual(m['background'], expected)
70
71
72 def test_lookup(self):
73 style = self.style
74 style.configure('TButton', background='yellow')
75 style.map('TButton', background=[('active', 'background', 'blue')])
76
77 self.assertEqual(style.lookup('TButton', 'background'), 'yellow')
78 self.assertEqual(style.lookup('TButton', 'background',
79 ['active', 'background']), 'blue')
80 self.assertEqual(style.lookup('TButton', 'optionnotdefined',
81 default='iknewit'), 'iknewit')
82
83
84 def test_layout(self):
85 style = self.style
86 self.assertRaises(tkinter.TclError, style.layout, 'NotALayout')
87 tv_style = style.layout('Treeview')
88
89 # "erase" Treeview layout
90 style.layout('Treeview', '')
91 self.assertEqual(style.layout('Treeview'),
92 [('null', {'sticky': 'nswe'})]
93 )
94
95 # restore layout
96 style.layout('Treeview', tv_style)
97 self.assertEqual(style.layout('Treeview'), tv_style)
98
99 # should return a list
100 self.assertIsInstance(style.layout('TButton'), list)
101
102 # correct layout, but "option" doesn't exist as option
103 self.assertRaises(tkinter.TclError, style.layout, 'Treeview',
104 [('name', {'option': 'inexistent'})])
105
106
107 def test_theme_use(self):
108 self.assertRaises(tkinter.TclError, self.style.theme_use,
109 'nonexistingname')
110
111 curr_theme = self.style.theme_use()
112 new_theme = None
113 for theme in self.style.theme_names():
114 if theme != curr_theme:
115 new_theme = theme
116 self.style.theme_use(theme)
117 break
118 else:
119 # just one theme available, can't go on with tests
120 return
121
122 self.assertFalse(curr_theme == new_theme)
123 self.assertFalse(new_theme != self.style.theme_use())
124
125 self.style.theme_use(curr_theme)
126
127 def test_configure_custom_copy(self):
128 style = self.style
129
130 curr_theme = self.style.theme_use()
131 self.addCleanup(self.style.theme_use, curr_theme)
132 for theme in self.style.theme_names():
133 self.style.theme_use(theme)
134 for name in CLASS_NAMES:
135 default = style.configure(name)
136 if not default:
137 continue
138 with self.subTest(theme=theme, name=name):
139 if support.verbose >= 2:
140 print('configure', theme, name, default)
141 if (theme in ('vista', 'xpnative')
142 and sys.getwindowsversion()[:2] == (6, 1)):
143 # Fails on the Windows 7 buildbot
144 continue
145 newname = f'C.{name}'
146 self.assertEqual(style.configure(newname), None)
147 style.configure(newname, **default)
148 self.assertEqual(style.configure(newname), default)
149 for key, value in default.items():
150 self.assertEqual(style.configure(newname, key), value)
151
152
153 def test_map_custom_copy(self):
154 style = self.style
155
156 curr_theme = self.style.theme_use()
157 self.addCleanup(self.style.theme_use, curr_theme)
158 for theme in self.style.theme_names():
159 self.style.theme_use(theme)
160 for name in CLASS_NAMES:
161 default = style.map(name)
162 if not default:
163 continue
164 with self.subTest(theme=theme, name=name):
165 if support.verbose >= 2:
166 print('map', theme, name, default)
167 if (theme in ('vista', 'xpnative')
168 and sys.getwindowsversion()[:2] == (6, 1)):
169 # Fails on the Windows 7 buildbot
170 continue
171 newname = f'C.{name}'
172 self.assertEqual(style.map(newname), {})
173 style.map(newname, **default)
174 if theme == 'alt' and name == '.' and get_tk_patchlevel(self.root) < (8, 6, 1):
175 default['embossed'] = [('disabled', '1')]
176 self.assertEqual(style.map(newname), default)
177 for key, value in default.items():
178 self.assertEqual(style.map(newname, key), value)
179
180 def test_element_options(self):
181 style = self.style
182 element_names = style.element_names()
183 self.assertNotIsInstance(element_names, str)
184 for name in element_names:
185 self.assertIsInstance(name, str)
186 element_options = style.element_options(name)
187 self.assertNotIsInstance(element_options, str)
188 for optname in element_options:
189 self.assertIsInstance(optname, str)
190
191 def test_element_create_errors(self):
192 style = self.style
193 with self.assertRaises(TypeError):
194 style.element_create('plain.newelem')
195 with self.assertRaisesRegex(TclError, 'No such element type spam'):
196 style.element_create('plain.newelem', 'spam')
197
198 def test_element_create_from(self):
199 style = self.style
200 style.element_create('plain.background', 'from', 'default')
201 self.assertIn('plain.background', style.element_names())
202 style.element_create('plain.arrow', 'from', 'default', 'rightarrow')
203 self.assertIn('plain.arrow', style.element_names())
204
205 def test_element_create_from_errors(self):
206 style = self.style
207 with self.assertRaises(IndexError):
208 style.element_create('plain.newelem', 'from')
209 with self.assertRaisesRegex(TclError, 'theme "spam" doesn\'t exist'):
210 style.element_create('plain.newelem', 'from', 'spam')
211
212 def test_element_create_image(self):
213 style = self.style
214 image = tkinter.PhotoImage(master=self.root, width=12, height=10)
215 style.element_create('block', 'image', image)
216 self.assertIn('block', style.element_names())
217
218 style.layout('TestLabel1', [('block', {'sticky': 'news'})])
219 a = ttk.Label(self.root, style='TestLabel1')
220 a.pack(expand=True, fill='both')
221 self.assertEqual(a.winfo_reqwidth(), 12)
222 self.assertEqual(a.winfo_reqheight(), 10)
223
224 imgfile = support.findfile('python.xbm', subdir='tkinterdata')
225 img1 = tkinter.BitmapImage(master=self.root, file=imgfile,
226 foreground='yellow', background='blue')
227 img2 = tkinter.BitmapImage(master=self.root, file=imgfile,
228 foreground='blue', background='yellow')
229 img3 = tkinter.BitmapImage(master=self.root, file=imgfile,
230 foreground='white', background='black')
231 style.element_create('TestButton.button', 'image',
232 img1, ('pressed', img2), ('active', img3),
233 border=(2, 4), sticky='we')
234 self.assertIn('TestButton.button', style.element_names())
235
236 style.layout('TestButton', [('TestButton.button', {'sticky': 'news'})])
237 b = ttk.Button(self.root, style='TestButton')
238 b.pack(expand=True, fill='both')
239 self.assertEqual(b.winfo_reqwidth(), 16)
240 self.assertEqual(b.winfo_reqheight(), 16)
241
242 def test_element_create_image_errors(self):
243 style = self.style
244 image = tkinter.PhotoImage(master=self.root, width=10, height=10)
245 with self.assertRaises(IndexError):
246 style.element_create('block2', 'image')
247 with self.assertRaises(TypeError):
248 style.element_create('block2', 'image', image, 1)
249 with self.assertRaises(ValueError):
250 style.element_create('block2', 'image', image, ())
251 with self.assertRaisesRegex(TclError, 'Invalid state name'):
252 style.element_create('block2', 'image', image, ('spam', image))
253 with self.assertRaisesRegex(TclError, 'Invalid state name'):
254 style.element_create('block2', 'image', image, (1, image))
255 with self.assertRaises(TypeError):
256 style.element_create('block2', 'image', image, ('pressed', 1, image))
257 with self.assertRaises(TypeError):
258 style.element_create('block2', 'image', image, (1, 'selected', image))
259 with self.assertRaisesRegex(TclError, 'bad option'):
260 style.element_create('block2', 'image', image, spam=1)
261
262 def test_element_create_vsapi_1(self):
263 style = self.style
264 if 'xpnative' not in style.theme_names():
265 self.skipTest("requires 'xpnative' theme")
266 style.element_create('smallclose', 'vsapi', 'WINDOW', 19, [
267 ('disabled', 4),
268 ('pressed', 3),
269 ('active', 2),
270 ('', 1)])
271 style.layout('CloseButton',
272 [('CloseButton.smallclose', {'sticky': 'news'})])
273 b = ttk.Button(self.root, style='CloseButton')
274 b.pack(expand=True, fill='both')
275 self.assertEqual(b.winfo_reqwidth(), 13)
276 self.assertEqual(b.winfo_reqheight(), 13)
277
278 def test_element_create_vsapi_2(self):
279 style = self.style
280 if 'xpnative' not in style.theme_names():
281 self.skipTest("requires 'xpnative' theme")
282 style.element_create('pin', 'vsapi', 'EXPLORERBAR', 3, [
283 ('pressed', '!selected', 3),
284 ('active', '!selected', 2),
285 ('pressed', 'selected', 6),
286 ('active', 'selected', 5),
287 ('selected', 4),
288 ('', 1)])
289 style.layout('Explorer.Pin',
290 [('Explorer.Pin.pin', {'sticky': 'news'})])
291 pin = ttk.Checkbutton(self.root, style='Explorer.Pin')
292 pin.pack(expand=True, fill='both')
293 self.assertEqual(pin.winfo_reqwidth(), 16)
294 self.assertEqual(pin.winfo_reqheight(), 16)
295
296 def test_element_create_vsapi_3(self):
297 style = self.style
298 if 'xpnative' not in style.theme_names():
299 self.skipTest("requires 'xpnative' theme")
300 style.element_create('headerclose', 'vsapi', 'EXPLORERBAR', 2, [
301 ('pressed', 3),
302 ('active', 2),
303 ('', 1)])
304 style.layout('Explorer.CloseButton',
305 [('Explorer.CloseButton.headerclose', {'sticky': 'news'})])
306 b = ttk.Button(self.root, style='Explorer.CloseButton')
307 b.pack(expand=True, fill='both')
308 self.assertEqual(b.winfo_reqwidth(), 16)
309 self.assertEqual(b.winfo_reqheight(), 16)
310
311 def test_theme_create(self):
312 style = self.style
313 curr_theme = style.theme_use()
314 curr_layout = style.layout('TLabel')
315 style.theme_create('testtheme1')
316 self.assertIn('testtheme1', style.theme_names())
317
318 style.theme_create('testtheme2', settings={
319 'elem' : {'element create': ['from', 'default'],},
320 'TLabel' : {
321 'configure': {'padding': 10},
322 'layout': [('elem', {'sticky': 'we'})],
323 },
324 })
325 self.assertIn('testtheme2', style.theme_names())
326
327 style.theme_create('testtheme3', 'testtheme2')
328 self.assertIn('testtheme3', style.theme_names())
329
330 style.theme_use('testtheme1')
331 self.assertEqual(style.element_names(), ())
332 self.assertEqual(style.layout('TLabel'), curr_layout)
333
334 style.theme_use('testtheme2')
335 self.assertEqual(style.element_names(), ('elem',))
336 self.assertEqual(style.lookup('TLabel', 'padding'), '10')
337 self.assertEqual(style.layout('TLabel'), [('elem', {'sticky': 'we'})])
338
339 style.theme_use('testtheme3')
340 self.assertEqual(style.element_names(), ())
341 self.assertEqual(style.lookup('TLabel', 'padding'), '')
342 self.assertEqual(style.layout('TLabel'), [('elem', {'sticky': 'we'})])
343
344 style.theme_use(curr_theme)
345
346 def test_theme_create_image(self):
347 style = self.style
348 curr_theme = style.theme_use()
349 image = tkinter.PhotoImage(master=self.root, width=10, height=10)
350 new_theme = 'testtheme4'
351 style.theme_create(new_theme, settings={
352 'block' : {
353 'element create': ['image', image, {'width': 120, 'height': 100}],
354 },
355 'TestWidget.block2' : {
356 'element create': ['image', image],
357 },
358 'TestWidget' : {
359 'configure': {
360 'anchor': 'left',
361 'padding': (3, 0, 0, 2),
362 'foreground': 'yellow',
363 },
364 'map': {
365 'foreground': [
366 ('pressed', 'red'),
367 ('active', 'disabled', 'blue'),
368 ],
369 },
370 'layout': [
371 ('TestWidget.block', {'sticky': 'we', 'side': 'left'}),
372 ('TestWidget.border', {
373 'sticky': 'nsw',
374 'border': 1,
375 'children': [
376 ('TestWidget.block2', {'sticky': 'nswe'})
377 ]
378 })
379 ],
380 },
381 })
382
383 style.theme_use(new_theme)
384 self.assertIn('block', style.element_names())
385 self.assertEqual(style.lookup('TestWidget', 'anchor'), 'left')
386 self.assertEqual(style.lookup('TestWidget', 'padding'), '3 0 0 2')
387 self.assertEqual(style.lookup('TestWidget', 'foreground'), 'yellow')
388 self.assertEqual(style.lookup('TestWidget', 'foreground',
389 ['active']), 'yellow')
390 self.assertEqual(style.lookup('TestWidget', 'foreground',
391 ['active', 'pressed']), 'red')
392 self.assertEqual(style.lookup('TestWidget', 'foreground',
393 ['active', 'disabled']), 'blue')
394 self.assertEqual(style.layout('TestWidget'),
395 [
396 ('TestWidget.block', {'side': 'left', 'sticky': 'we'}),
397 ('TestWidget.border', {
398 'sticky': 'nsw',
399 'border': '1',
400 'children': [('TestWidget.block2', {'sticky': 'nswe'})]
401 })
402 ])
403
404 b = ttk.Label(self.root, style='TestWidget')
405 b.pack(expand=True, fill='both')
406 self.assertEqual(b.winfo_reqwidth(), 134)
407 self.assertEqual(b.winfo_reqheight(), 100)
408
409 style.theme_use(curr_theme)
410
411 def test_theme_create_vsapi(self):
412 style = self.style
413 if 'xpnative' not in style.theme_names():
414 self.skipTest("requires 'xpnative' theme")
415 curr_theme = style.theme_use()
416 new_theme = 'testtheme5'
417 style.theme_create(new_theme, settings={
418 'pin' : {
419 'element create': ['vsapi', 'EXPLORERBAR', 3, [
420 ('pressed', '!selected', 3),
421 ('active', '!selected', 2),
422 ('pressed', 'selected', 6),
423 ('active', 'selected', 5),
424 ('selected', 4),
425 ('', 1)]],
426 },
427 'Explorer.Pin' : {
428 'layout': [('Explorer.Pin.pin', {'sticky': 'news'})],
429 },
430 })
431
432 style.theme_use(new_theme)
433 self.assertIn('pin', style.element_names())
434 self.assertEqual(style.layout('Explorer.Pin'),
435 [('Explorer.Pin.pin', {'sticky': 'nswe'})])
436
437 pin = ttk.Checkbutton(self.root, style='Explorer.Pin')
438 pin.pack(expand=True, fill='both')
439 self.assertEqual(pin.winfo_reqwidth(), 16)
440 self.assertEqual(pin.winfo_reqheight(), 16)
441
442 style.theme_use(curr_theme)
443
444
445 if __name__ == "__main__":
446 unittest.main()