bpo-35728: Add root parameter to tkinter.font.nametofont() (GH-23885)

This commit is contained in:
Desmond Cheong 2020-12-26 05:18:06 +08:00 committed by GitHub
parent 675c97eb6c
commit 36a779e64c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 4 deletions

View File

@ -91,6 +91,9 @@ The different font weights and slants are:
Return the names of defined fonts.
.. function:: nametofont(name)
.. function:: nametofont(name, root=None)
Return a :class:`Font` representation of a tk named font.
.. versionchanged:: 3.10
The *root* parameter was added.

View File

@ -17,10 +17,10 @@ BOLD = "bold"
ITALIC = "italic"
def nametofont(name):
def nametofont(name, root=None):
"""Given the name of a tk named font, returns a Font representation.
"""
return Font(name=name, exists=True)
return Font(name=name, exists=True, root=root)
class Font:

View File

@ -101,6 +101,11 @@ class FontTest(AbstractTkTest, unittest.TestCase):
self.assertTrue(name)
self.assertIn(fontname, names)
def test_nametofont(self):
testfont = font.nametofont(fontname, root=self.root)
self.assertIsInstance(testfont, font.Font)
self.assertEqual(testfont.name, fontname)
def test_repr(self):
self.assertEqual(
repr(self.font), f'<tkinter.font.Font object {fontname!r}>'
@ -136,6 +141,16 @@ class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase):
tkinter.NoDefaultRoot()
self.assertRaises(RuntimeError, font.names)
def test_nametofont(self):
self.assertRaises(RuntimeError, font.nametofont, fontname)
root = tkinter.Tk()
testfont = font.nametofont(fontname)
self.assertIsInstance(testfont, font.Font)
self.assertEqual(testfont.name, fontname)
root.destroy()
tkinter.NoDefaultRoot()
self.assertRaises(RuntimeError, font.nametofont, fontname)
tests_gui = (FontTest, DefaultRootTest)

View File

@ -0,0 +1 @@
Added a root parameter to :func:`tkinter.font.nametofont`.