2009-01-28 10:41:10 -04:00
|
|
|
import unittest
|
|
|
|
import tkinter
|
|
|
|
from tkinter import ttk
|
|
|
|
from test.support import requires, run_unittest
|
2014-08-24 03:07:47 -03:00
|
|
|
from tkinter.test.support import AbstractTkTest
|
2009-01-28 10:41:10 -04:00
|
|
|
|
|
|
|
requires('gui')
|
|
|
|
|
2014-08-24 03:07:47 -03:00
|
|
|
class StyleTest(AbstractTkTest, unittest.TestCase):
|
2009-01-28 10:41:10 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
2014-08-24 03:07:47 -03:00
|
|
|
super().setUp()
|
|
|
|
self.style = ttk.Style(self.root)
|
2009-01-28 10:41:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_configure(self):
|
|
|
|
style = self.style
|
|
|
|
style.configure('TButton', background='yellow')
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertEqual(style.configure('TButton', 'background'),
|
2009-01-28 10:41:10 -04:00
|
|
|
'yellow')
|
2013-11-16 18:42:25 -04:00
|
|
|
self.assertIsInstance(style.configure('TButton'), dict)
|
2009-01-28 10:41:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_map(self):
|
|
|
|
style = self.style
|
|
|
|
style.map('TButton', background=[('active', 'background', 'blue')])
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertEqual(style.map('TButton', 'background'),
|
2014-08-24 03:07:47 -03:00
|
|
|
[('active', 'background', 'blue')] if self.wantobjects else
|
2014-01-07 13:27:42 -04:00
|
|
|
[('active background', 'blue')])
|
2013-11-16 18:42:25 -04:00
|
|
|
self.assertIsInstance(style.map('TButton'), dict)
|
2009-01-28 10:41:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_lookup(self):
|
|
|
|
style = self.style
|
|
|
|
style.configure('TButton', background='yellow')
|
|
|
|
style.map('TButton', background=[('active', 'background', 'blue')])
|
|
|
|
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertEqual(style.lookup('TButton', 'background'), 'yellow')
|
|
|
|
self.assertEqual(style.lookup('TButton', 'background',
|
2009-01-28 10:41:10 -04:00
|
|
|
['active', 'background']), 'blue')
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertEqual(style.lookup('TButton', 'optionnotdefined',
|
2009-01-28 10:41:10 -04:00
|
|
|
default='iknewit'), 'iknewit')
|
|
|
|
|
|
|
|
|
|
|
|
def test_layout(self):
|
|
|
|
style = self.style
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertRaises(tkinter.TclError, style.layout, 'NotALayout')
|
2009-01-28 10:41:10 -04:00
|
|
|
tv_style = style.layout('Treeview')
|
|
|
|
|
|
|
|
# "erase" Treeview layout
|
|
|
|
style.layout('Treeview', '')
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertEqual(style.layout('Treeview'),
|
2009-01-28 10:41:10 -04:00
|
|
|
[('null', {'sticky': 'nswe'})]
|
|
|
|
)
|
|
|
|
|
|
|
|
# restore layout
|
|
|
|
style.layout('Treeview', tv_style)
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertEqual(style.layout('Treeview'), tv_style)
|
2009-01-28 10:41:10 -04:00
|
|
|
|
|
|
|
# should return a list
|
2013-11-16 18:42:25 -04:00
|
|
|
self.assertIsInstance(style.layout('TButton'), list)
|
2009-01-28 10:41:10 -04:00
|
|
|
|
|
|
|
# correct layout, but "option" doesn't exist as option
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertRaises(tkinter.TclError, style.layout, 'Treeview',
|
2009-02-21 16:59:32 -04:00
|
|
|
[('name', {'option': 'inexistent'})])
|
2009-01-28 10:41:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_theme_use(self):
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertRaises(tkinter.TclError, self.style.theme_use,
|
2009-01-28 10:41:10 -04:00
|
|
|
'nonexistingname')
|
|
|
|
|
|
|
|
curr_theme = self.style.theme_use()
|
|
|
|
new_theme = None
|
|
|
|
for theme in self.style.theme_names():
|
|
|
|
if theme != curr_theme:
|
|
|
|
new_theme = theme
|
|
|
|
self.style.theme_use(theme)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# just one theme available, can't go on with tests
|
|
|
|
return
|
|
|
|
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertFalse(curr_theme == new_theme)
|
|
|
|
self.assertFalse(new_theme != self.style.theme_use())
|
2009-01-28 10:41:10 -04:00
|
|
|
|
|
|
|
self.style.theme_use(curr_theme)
|
|
|
|
|
|
|
|
|
|
|
|
tests_gui = (StyleTest, )
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
run_unittest(*tests_gui)
|