Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Doc/library/tkinter.font.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,4 +100,4 @@ The different font weights and slants are:

.. function:: nametofont(name)

Return a :class:`Font` representation of a tk named font.
Return a :class:`Font` representation of a tk named font.
5 changes: 5 additions & 0 deletions Lib/tkinter/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,)
Expand Down
12 changes: 12 additions & 0 deletions Lib/tkinter/test/test_tkinter/test_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :meth:`equal` method to tkinter font class