bpo-42328: Fix tkinter.ttk.Style.map(). (GH-23300)

The function accepts now the representation of the default state as
empty sequence (as returned by Style.map()).
The structure of the result is now the same on all platform
and does not depend on the value of wantobjects.
(cherry picked from commit dd844a2916)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2020-11-23 00:51:37 -08:00 committed by GitHub
parent 5aa6c99da1
commit 3e53301308
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 111 additions and 28 deletions

View file

@ -137,6 +137,9 @@ class InternalFunctionsTest(unittest.TestCase):
result = ttk._format_mapdict(opts)
self.assertEqual(result, ('-üñíćódè', 'á vãl'))
self.assertEqual(ttk._format_mapdict({'opt': [('value',)]}),
('-opt', '{} value'))
# empty states
valid = {'opt': [('', '', 'hi')]}
self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{ } hi'))
@ -159,10 +162,6 @@ class InternalFunctionsTest(unittest.TestCase):
opts = {'a': None}
self.assertRaises(TypeError, ttk._format_mapdict, opts)
# items in the value must have size >= 2
self.assertRaises(IndexError, ttk._format_mapdict,
{'a': [('invalid', )]})
def test_format_elemcreate(self):
self.assertTrue(ttk._format_elemcreate(None), (None, ()))

View file

@ -1,11 +1,22 @@
import unittest
import tkinter
from tkinter import ttk
from test import support
from test.support import requires, run_unittest
from tkinter.test.support import AbstractTkTest
requires('gui')
CLASS_NAMES = [
'.', 'ComboboxPopdownFrame', 'Heading',
'Horizontal.TProgressbar', 'Horizontal.TScale', 'Item', 'Sash',
'TButton', 'TCheckbutton', 'TCombobox', 'TEntry',
'TLabelframe', 'TLabelframe.Label', 'TMenubutton',
'TNotebook', 'TNotebook.Tab', 'Toolbutton', 'TProgressbar',
'TRadiobutton', 'Treeview', 'TScale', 'TScrollbar', 'TSpinbox',
'Vertical.TProgressbar', 'Vertical.TScale'
]
class StyleTest(AbstractTkTest, unittest.TestCase):
def setUp(self):
@ -23,11 +34,36 @@ class StyleTest(AbstractTkTest, unittest.TestCase):
def test_map(self):
style = self.style
style.map('TButton', background=[('active', 'background', 'blue')])
self.assertEqual(style.map('TButton', 'background'),
[('active', 'background', 'blue')] if self.wantobjects else
[('active background', 'blue')])
self.assertIsInstance(style.map('TButton'), dict)
# Single state
for states in ['active'], [('active',)]:
with self.subTest(states=states):
style.map('TButton', background=[(*states, 'white')])
expected = [('active', 'white')]
self.assertEqual(style.map('TButton', 'background'), expected)
m = style.map('TButton')
self.assertIsInstance(m, dict)
self.assertEqual(m['background'], expected)
# Multiple states
for states in ['pressed', '!disabled'], ['pressed !disabled'], [('pressed', '!disabled')]:
with self.subTest(states=states):
style.map('TButton', background=[(*states, 'black')])
expected = [('pressed', '!disabled', 'black')]
self.assertEqual(style.map('TButton', 'background'), expected)
m = style.map('TButton')
self.assertIsInstance(m, dict)
self.assertEqual(m['background'], expected)
# Default state
for states in [], [''], [()]:
with self.subTest(states=states):
style.map('TButton', background=[(*states, 'grey')])
expected = [('grey',)]
self.assertEqual(style.map('TButton', 'background'), expected)
m = style.map('TButton')
self.assertIsInstance(m, dict)
self.assertEqual(m['background'], expected)
def test_lookup(self):
@ -86,6 +122,50 @@ class StyleTest(AbstractTkTest, unittest.TestCase):
self.style.theme_use(curr_theme)
def test_configure_custom_copy(self):
style = self.style
curr_theme = self.style.theme_use()
self.addCleanup(self.style.theme_use, curr_theme)
for theme in self.style.theme_names():
self.style.theme_use(theme)
for name in CLASS_NAMES:
default = style.configure(name)
if not default:
continue
with self.subTest(theme=theme, name=name):
if support.verbose >= 2:
print('configure', theme, name, default)
newname = f'C.{name}'
self.assertEqual(style.configure(newname), None)
style.configure(newname, **default)
self.assertEqual(style.configure(newname), default)
for key, value in default.items():
self.assertEqual(style.configure(newname, key), value)
def test_map_custom_copy(self):
style = self.style
curr_theme = self.style.theme_use()
self.addCleanup(self.style.theme_use, curr_theme)
for theme in self.style.theme_names():
self.style.theme_use(theme)
for name in CLASS_NAMES:
default = style.map(name)
if not default:
continue
with self.subTest(theme=theme, name=name):
if support.verbose >= 2:
print('map', theme, name, default)
newname = f'C.{name}'
self.assertEqual(style.map(newname), {})
style.map(newname, **default)
self.assertEqual(style.map(newname), default)
for key, value in default.items():
self.assertEqual(style.map(newname, key), value)
tests_gui = (StyleTest, )
if __name__ == "__main__":