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,
210 'theme "spam" (does not|doesn\'t) exist'):
211 style.element_create('plain.newelem', 'from', 'spam')
212
213 def test_element_create_image(self):
214 style = self.style
215 image = tkinter.PhotoImage(master=self.root, width=12, height=10)
216 style.element_create('block', 'image', image)
217 self.assertIn('block', style.element_names())
218
219 style.layout('TestLabel1', [('block', {'sticky': 'news'})])
220 a = ttk.Label(self.root, style='TestLabel1')
221 a.pack(expand=True, fill='both')
222 self.assertEqual(a.winfo_reqwidth(), 12)
223 self.assertEqual(a.winfo_reqheight(), 10)
224
225 imgfile = support.findfile('python.xbm', subdir='tkinterdata')
226 img1 = tkinter.BitmapImage(master=self.root, file=imgfile,
227 foreground='yellow', background='blue')
228 img2 = tkinter.BitmapImage(master=self.root, file=imgfile,
229 foreground='blue', background='yellow')
230 img3 = tkinter.BitmapImage(master=self.root, file=imgfile,
231 foreground='white', background='black')
232 style.element_create('TestButton.button', 'image',
233 img1, ('pressed', img2), ('active', img3),
234 border=(2, 4), sticky='we')
235 self.assertIn('TestButton.button', style.element_names())
236
237 style.layout('TestButton', [('TestButton.button', {'sticky': 'news'})])
238 b = ttk.Button(self.root, style='TestButton')
239 b.pack(expand=True, fill='both')
240 self.assertEqual(b.winfo_reqwidth(), 16)
241 self.assertEqual(b.winfo_reqheight(), 16)
242
243 def test_element_create_image_errors(self):
244 style = self.style
245 image = tkinter.PhotoImage(master=self.root, width=10, height=10)
246 with self.assertRaises(IndexError):
247 style.element_create('block2', 'image')
248 with self.assertRaises(TypeError):
249 style.element_create('block2', 'image', image, 1)
250 with self.assertRaises(ValueError):
251 style.element_create('block2', 'image', image, ())
252 with self.assertRaisesRegex(TclError, 'Invalid state name'):
253 style.element_create('block2', 'image', image, ('spam', image))
254 with self.assertRaisesRegex(TclError, 'Invalid state name'):
255 style.element_create('block2', 'image', image, (1, image))
256 with self.assertRaises(TypeError):
257 style.element_create('block2', 'image', image, ('pressed', 1, image))
258 with self.assertRaises(TypeError):
259 style.element_create('block2', 'image', image, (1, 'selected', image))
260 with self.assertRaisesRegex(TclError, 'bad option'):
261 style.element_create('block2', 'image', image, spam=1)
262
263 def test_element_create_vsapi_1(self):
264 style = self.style
265 if 'xpnative' not in style.theme_names():
266 self.skipTest("requires 'xpnative' theme")
267 style.element_create('smallclose', 'vsapi', 'WINDOW', 19, [
268 ('disabled', 4),
269 ('pressed', 3),
270 ('active', 2),
271 ('', 1)])
272 style.layout('CloseButton',
273 [('CloseButton.smallclose', {'sticky': 'news'})])
274 b = ttk.Button(self.root, style='CloseButton')
275 b.pack(expand=True, fill='both')
276 self.assertEqual(b.winfo_reqwidth(), 13)
277 self.assertEqual(b.winfo_reqheight(), 13)
278
279 def test_element_create_vsapi_2(self):
280 style = self.style
281 if 'xpnative' not in style.theme_names():
282 self.skipTest("requires 'xpnative' theme")
283 style.element_create('pin', 'vsapi', 'EXPLORERBAR', 3, [
284 ('pressed', '!selected', 3),
285 ('active', '!selected', 2),
286 ('pressed', 'selected', 6),
287 ('active', 'selected', 5),
288 ('selected', 4),
289 ('', 1)])
290 style.layout('Explorer.Pin',
291 [('Explorer.Pin.pin', {'sticky': 'news'})])
292 pin = ttk.Checkbutton(self.root, style='Explorer.Pin')
293 pin.pack(expand=True, fill='both')
294 self.assertEqual(pin.winfo_reqwidth(), 16)
295 self.assertEqual(pin.winfo_reqheight(), 16)
296
297 def test_element_create_vsapi_3(self):
298 style = self.style
299 if 'xpnative' not in style.theme_names():
300 self.skipTest("requires 'xpnative' theme")
301 style.element_create('headerclose', 'vsapi', 'EXPLORERBAR', 2, [
302 ('pressed', 3),
303 ('active', 2),
304 ('', 1)])
305 style.layout('Explorer.CloseButton',
306 [('Explorer.CloseButton.headerclose', {'sticky': 'news'})])
307 b = ttk.Button(self.root, style='Explorer.CloseButton')
308 b.pack(expand=True, fill='both')
309 self.assertEqual(b.winfo_reqwidth(), 16)
310 self.assertEqual(b.winfo_reqheight(), 16)
311
312 def test_theme_create(self):
313 style = self.style
314 curr_theme = style.theme_use()
315 curr_layout = style.layout('TLabel')
316 style.theme_create('testtheme1')
317 self.assertIn('testtheme1', style.theme_names())
318
319 style.theme_create('testtheme2', settings={
320 'elem' : {'element create': ['from', 'default'],},
321 'TLabel' : {
322 'configure': {'padding': 10},
323 'layout': [('elem', {'sticky': 'we'})],
324 },
325 })
326 self.assertIn('testtheme2', style.theme_names())
327
328 style.theme_create('testtheme3', 'testtheme2')
329 self.assertIn('testtheme3', style.theme_names())
330
331 style.theme_use('testtheme1')
332 self.assertEqual(style.element_names(), ())
333 self.assertEqual(style.layout('TLabel'), curr_layout)
334
335 style.theme_use('testtheme2')
336 self.assertEqual(style.element_names(), ('elem',))
337 self.assertEqual(style.lookup('TLabel', 'padding'), '10')
338 self.assertEqual(style.layout('TLabel'), [('elem', {'sticky': 'we'})])
339
340 style.theme_use('testtheme3')
341 self.assertEqual(style.element_names(), ())
342 self.assertEqual(style.lookup('TLabel', 'padding'), '')
343 self.assertEqual(style.layout('TLabel'), [('elem', {'sticky': 'we'})])
344
345 style.theme_use(curr_theme)
346
347 def test_theme_create_image(self):
348 style = self.style
349 curr_theme = style.theme_use()
350 image = tkinter.PhotoImage(master=self.root, width=10, height=10)
351 new_theme = 'testtheme4'
352 style.theme_create(new_theme, settings={
353 'block' : {
354 'element create': ['image', image, {'width': 120, 'height': 100}],
355 },
356 'TestWidget.block2' : {
357 'element create': ['image', image],
358 },
359 'TestWidget' : {
360 'configure': {
361 'anchor': 'left',
362 'padding': (3, 0, 0, 2),
363 'foreground': 'yellow',
364 },
365 'map': {
366 'foreground': [
367 ('pressed', 'red'),
368 ('active', 'disabled', 'blue'),
369 ],
370 },
371 'layout': [
372 ('TestWidget.block', {'sticky': 'we', 'side': 'left'}),
373 ('TestWidget.border', {
374 'sticky': 'nsw',
375 'border': 1,
376 'children': [
377 ('TestWidget.block2', {'sticky': 'nswe'})
378 ]
379 })
380 ],
381 },
382 })
383
384 style.theme_use(new_theme)
385 self.assertIn('block', style.element_names())
386 self.assertEqual(style.lookup('TestWidget', 'anchor'), 'left')
387 self.assertEqual(style.lookup('TestWidget', 'padding'), '3 0 0 2')
388 self.assertEqual(style.lookup('TestWidget', 'foreground'), 'yellow')
389 self.assertEqual(style.lookup('TestWidget', 'foreground',
390 ['active']), 'yellow')
391 self.assertEqual(style.lookup('TestWidget', 'foreground',
392 ['active', 'pressed']), 'red')
393 self.assertEqual(style.lookup('TestWidget', 'foreground',
394 ['active', 'disabled']), 'blue')
395 self.assertEqual(style.layout('TestWidget'),
396 [
397 ('TestWidget.block', {'side': 'left', 'sticky': 'we'}),
398 ('TestWidget.border', {
399 'sticky': 'nsw',
400 'border': '1',
401 'children': [('TestWidget.block2', {'sticky': 'nswe'})]
402 })
403 ])
404
405 b = ttk.Label(self.root, style='TestWidget')
406 b.pack(expand=True, fill='both')
407 self.assertEqual(b.winfo_reqwidth(), 134)
408 self.assertEqual(b.winfo_reqheight(), 100)
409
410 style.theme_use(curr_theme)
411
412 def test_theme_create_vsapi(self):
413 style = self.style
414 if 'xpnative' not in style.theme_names():
415 self.skipTest("requires 'xpnative' theme")
416 curr_theme = style.theme_use()
417 new_theme = 'testtheme5'
418 style.theme_create(new_theme, settings={
419 'pin' : {
420 'element create': ['vsapi', 'EXPLORERBAR', 3, [
421 ('pressed', '!selected', 3),
422 ('active', '!selected', 2),
423 ('pressed', 'selected', 6),
424 ('active', 'selected', 5),
425 ('selected', 4),
426 ('', 1)]],
427 },
428 'Explorer.Pin' : {
429 'layout': [('Explorer.Pin.pin', {'sticky': 'news'})],
430 },
431 })
432
433 style.theme_use(new_theme)
434 self.assertIn('pin', style.element_names())
435 self.assertEqual(style.layout('Explorer.Pin'),
436 [('Explorer.Pin.pin', {'sticky': 'nswe'})])
437
438 pin = ttk.Checkbutton(self.root, style='Explorer.Pin')
439 pin.pack(expand=True, fill='both')
440 self.assertEqual(pin.winfo_reqwidth(), 16)
441 self.assertEqual(pin.winfo_reqheight(), 16)
442
443 style.theme_use(curr_theme)
444
445
446 if __name__ == "__main__":
447 unittest.main()