Return the names of defined fonts.
-.. function:: nametofont(name)
+.. function:: nametofont(name, root=None)
- Return a :class:`Font` representation of a tk named font.
\ No newline at end of file
+ Return a :class:`Font` representation of a tk named font.
+
+ .. versionchanged:: 3.10
+ The *root* parameter was added.
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:
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}>'
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)