summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Doc/library/tkinter.font.rst7
-rw-r--r--Lib/tkinter/font.py4
-rw-r--r--Lib/tkinter/test/test_tkinter/test_font.py15
-rw-r--r--Misc/NEWS.d/next/Library/2020-12-21-23-34-57.bpo-35728.9m-azF.rst1
4 files changed, 23 insertions, 4 deletions
diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst
index b0f4505..c7c2b7b 100644
--- a/Doc/library/tkinter.font.rst
+++ b/Doc/library/tkinter.font.rst
@@ -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. \ No newline at end of file
+ Return a :class:`Font` representation of a tk named font.
+
+ .. versionchanged:: 3.10
+ The *root* parameter was added.
diff --git a/Lib/tkinter/font.py b/Lib/tkinter/font.py
index 7f6ba5a..06ed01b 100644
--- a/Lib/tkinter/font.py
+++ b/Lib/tkinter/font.py
@@ -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:
diff --git a/Lib/tkinter/test/test_tkinter/test_font.py b/Lib/tkinter/test/test_tkinter/test_font.py
index 3f71209..0354c5f 100644
--- a/Lib/tkinter/test/test_tkinter/test_font.py
+++ b/Lib/tkinter/test/test_tkinter/test_font.py
@@ -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)
diff --git a/Misc/NEWS.d/next/Library/2020-12-21-23-34-57.bpo-35728.9m-azF.rst b/Misc/NEWS.d/next/Library/2020-12-21-23-34-57.bpo-35728.9m-azF.rst
new file mode 100644
index 0000000..aa57e27
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-12-21-23-34-57.bpo-35728.9m-azF.rst
@@ -0,0 +1 @@
+Added a root parameter to :func:`tkinter.font.nametofont`.