]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35728: Add root parameter to tkinter.font.nametofont() (GH-23885)
authorDesmond Cheong <desmondcheongzx@gmail.com>
Fri, 25 Dec 2020 21:18:06 +0000 (05:18 +0800)
committerGitHub <noreply@github.com>
Fri, 25 Dec 2020 21:18:06 +0000 (23:18 +0200)
Doc/library/tkinter.font.rst
Lib/tkinter/font.py
Lib/tkinter/test/test_tkinter/test_font.py
Misc/NEWS.d/next/Library/2020-12-21-23-34-57.bpo-35728.9m-azF.rst [new file with mode: 0644]

index b0f4505e9e3c69280fb14816cba22f2147e572f5..c7c2b7b566cf8f00c49d1c7f6c1fd0bb6812337c 100644 (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.
\ No newline at end of file
+   Return a :class:`Font` representation of a tk named font.
+
+   .. versionchanged:: 3.10
+      The *root* parameter was added.
index 7f6ba5a80fb8e54ca57a2f07535ec58cb2bbebd9..06ed01b99cfc3f5e1b4adacd275186126551b323 100644 (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:
index 3f71209064321de778ac0b201562e3212e1a8b09..0354c5f3044e7b0e84546d8a61bffda7f0e2cbab 100644 (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)
 
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 (file)
index 0000000..aa57e27
--- /dev/null
@@ -0,0 +1 @@
+Added a root parameter to :func:`tkinter.font.nametofont`.