diff --git a/Doc/library/tkinter.font.rst b/Doc/library/tkinter.font.rst index b0f4505e9e3c692..0f828259c19d1a3 100644 --- a/Doc/library/tkinter.font.rst +++ b/Doc/library/tkinter.font.rst @@ -61,6 +61,13 @@ The different font weights and slants are: Return new instance of the current font. + .. method:: equal(other) + + Compare the font to another and check if they represent the same font (in + contrast to :meth:`__eq__`, which checks if they are the same Tk object). + + .. versionadded:: 3.10 + .. method:: measure(text, displayof=None) Return amount of space the text would occupy on the specified display @@ -93,4 +100,4 @@ The different font weights and slants are: .. function:: nametofont(name) - 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. diff --git a/Lib/tkinter/font.py b/Lib/tkinter/font.py index 15ad7ab4b63a815..874257e0246f114 100644 --- a/Lib/tkinter/font.py +++ b/Lib/tkinter/font.py @@ -149,6 +149,11 @@ def config(self, **options): configure = config + def equal(self, other): + if not isinstance(other, Font): + raise TypeError("Value for comparison must also be a Font") + return self.actual() == other.actual() + def measure(self, text, displayof=None): "Return text width" args = (text,) diff --git a/Lib/tkinter/test/test_tkinter/test_font.py b/Lib/tkinter/test/test_tkinter/test_font.py index a021ea336807bb8..a2cb5e8773c5d2f 100644 --- a/Lib/tkinter/test/test_tkinter/test_font.py +++ b/Lib/tkinter/test/test_tkinter/test_font.py @@ -72,6 +72,18 @@ def test_eq(self): self.assertNotEqual(font1, 0) self.assertEqual(font1, ALWAYS_EQ) + def test_equal(self): + font1 = font.Font(root=self.root, size=32) + font2 = font1.copy() + self.assertTrue(font1.equal(font2)) + self.assertTrue(font2.equal(font1)) + font2['size'] = 30 + self.assertFalse(font1.equal(font2)) + font3 = font.Font(root=self.root, size=30, underline=True) + self.assertFalse(font2.equal(font3)) + font2['underline'] = True + self.assertTrue(font2.equal(font3)) + def test_measure(self): self.assertIsInstance(self.font.measure('abc'), int) diff --git a/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst b/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst new file mode 100644 index 000000000000000..d258381389f4784 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-09-24-11-29-21.bpo-41851.JpgrR4.rst @@ -0,0 +1 @@ +Add :meth:`equal` method to tkinter font class