From 4da004dba4effd3e735b94663ffc7f49be968a22 Mon Sep 17 00:00:00 2001 From: Desmond Cheong Zhi Xi Date: Tue, 22 Dec 2020 00:24:14 -0500 Subject: [PATCH 1/3] bpo-35728: Add root parameter to tkinter.font.nametofont() --- Doc/library/tkinter.font.rst | 2 +- Lib/tkinter/font.py | 4 ++-- Lib/tkinter/test/test_tkinter/test_font.py | 15 +++++++++++++++ .../2020-12-21-23-34-57.bpo-35728.9m-azF.rst | 1 + 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-12-21-23-34-57.bpo-35728.9m-azF.rst diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst index b0f4505e9e3c69..c05aca950ed549 100644 --- a/Doc/library/tkinter.font.rst +++ b/Doc/library/tkinter.font.rst @@ -91,6 +91,6 @@ 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 diff --git a/Lib/tkinter/font.py b/Lib/tkinter/font.py index c051162bd29e00..ff44f42d613399 100644 --- a/Lib/tkinter/font.py +++ b/Lib/tkinter/font.py @@ -17,10 +17,10 @@ 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 3f71209064321d..0354c5f3044e7b 100644 --- a/Lib/tkinter/test/test_tkinter/test_font.py +++ b/Lib/tkinter/test/test_tkinter/test_font.py @@ -101,6 +101,11 @@ def test_names(self): 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'' @@ -136,6 +141,16 @@ def test_names(self): 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 00000000000000..aa57e274ce5c02 --- /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`. From 8657cfbda252e8e7b7cd7736b04814c1bf971dd0 Mon Sep 17 00:00:00 2001 From: Desmond Cheong Zhi Xi Date: Tue, 22 Dec 2020 13:38:55 -0500 Subject: [PATCH 2/3] Add versionchanged to tkinter.font.nametofont documentation --- Doc/library/tkinter.font.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst index c05aca950ed549..2d6f08797816ec 100644 --- a/Doc/library/tkinter.font.rst +++ b/Doc/library/tkinter.font.rst @@ -93,4 +93,7 @@ The different font weights and slants are: .. 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. From e526cb1b7c8fa36be6fb0780bdede9d8d4e60bc2 Mon Sep 17 00:00:00 2001 From: Desmond Cheong Zhi Xi Date: Thu, 24 Dec 2020 22:53:59 -0500 Subject: [PATCH 3/3] Fix indentation for documentation of tkinter.font.nametofont --- Doc/library/tkinter.font.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst index 2d6f08797816ec..c7c2b7b566cf8f 100644 --- a/Doc/library/tkinter.font.rst +++ b/Doc/library/tkinter.font.rst @@ -95,5 +95,5 @@ The different font weights and slants are: Return a :class:`Font` representation of a tk named font. - .. versionchanged:: 3.10 - The *root* parameter was added. + .. versionchanged:: 3.10 + The *root* parameter was added.