This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: tkinter: add font equal methods
Type: enhancement Stage: patch review
Components: Documentation, Tkinter Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, epaine, gpolo, serhiy.storchaka, taleinat
Priority: normal Keywords: patch

Created on 2020-09-24 11:27 by epaine, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 22396 closed epaine, 2020-09-24 11:27
PR 22434 open epaine, 2020-09-28 16:25
Messages (7)
msg377446 - (view) Author: E. Paine (epaine) * Date: 2020-09-24 11:27
I think it would be helpful to add these methods to the tkinter font class.

Starting with the equal method, I have found it very useful to be able to compare tkinter fonts based on their value rather than just name. I used this, for example, in #28694 to ensure the -font attribute had been changed correctly (though there I compared each of the attributes explicitly rather than using `.actual()` as I have in this patch).

The neg method, I think, would be very useful to allow easy conversion between points and pixels. For example:

points = Font(size=16)
pixels = -points
msg377528 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-09-26 13:15
Why would negating the font size convert it from points to pixels?? It seems to be coming straight from Tk; is this documented anywhere?

I'd greatly prefer that we use a properly named property instead, e.g. .pixel_size.
msg377529 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-09-26 13:22
As for an equal() method, I think that adding equal() which is different than the existing __eq__() would be a source of confusion. I also think using it wouldn't add anything, and would be less clear, compared to font1.actual() == font2.actual().

I'm definitely not happy with the existing __eq__() though, and think it would be worth discussing changing __eq__() to use self.actual() == other.actual() rather than just comparing names. That should be a separate issue, though - you're welcome to create one!
msg377530 - (view) Author: E. Paine (epaine) * Date: 2020-09-26 15:07
> Why would negating the font size convert it from points to pixels??

Oh... I apologise: I clearly didn't think this through. It doesn't "convert" in any sense, it just negates the font size so 16 points becomes 16 pixels. Therefore, it is practically useless (besides, a new font object would not help the user a converted value anyway) - I therefore do not wish to proceed this this part of the issue (I have removed it from the title and will remove it from the PR).

> I also think using [.equal] wouldn't add anything

I disagree and will stand by this one! While the user could easily write it themselves, I think it would be helpful to have a convenience method to check if two fonts represent the same thing that is displayed to the user. When first creating this issue, I didn't want to touch __eq__ for two reasons:
1. I didn't know how much code changing this would break (it is very hard to check for code usage of dunder methods - if you know any ways please let me know!)
2. Checking to see if the fonts are the same Tk object is also very helpful (in other, granted fewer, contexts)

For example, something like this could be used in IDLE so that if the user sets their font back to the equivalent of the default ("TkFixedFont"), the value isn't kept in the user's local config (similar to how all the other options do).

Therefore, while I would like this somehow added, I agree that adding an `equal` method that does not do the same thing as `==` is confusing (you just have to look at Java and all the confusion over using `==` on strings - a 'gotcha' for every new Java learner). However, as I said above, I am reluctant to touch __eq__. Any ideas?
msg377588 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-09-28 08:14
> > I also think using [.equal] wouldn't add anything

> I disagree and will stand by this one!

Please do note that the full quote was "I also think using it wouldn't add anything, and would be less clear, compared to font1.actual() == font2.actual()".

> I think it would be helpful to have a convenience method to check if two fonts represent the same thing that is displayed to the user.

More convenient than font1.actual() == font2.actual() ?

Since this will be used rather rarely, IMO the more significant problem is discoverability - I've done a *lot* of work with Tkinter and had never heard of the .actual() method. However, adding another comparison method won't help with that by itself.

> I am reluctant to touch __eq__. Any ideas?

You wrote that "Checking to see if the fonts are the same Tk object is also very helpful (in other, granted fewer, contexts)": Could you elaborate on that?

I still think we should consider overriding __eq__. But I need to check how consistent Tktinter is regarding __eq__ checking whether two objects have the same name.


[goes to check the docs, code, and do some interactive investigation...]


Okay, so the Font class's .name attribute is just a name the user gives to a certain font configuration, which is supposed to be unique. Our docs are pretty clear about this:

"The Font class represents a named font. Font instances are given unique names and can be specified by their family, size, and style configuration. Named fonts are Tk's method of creating and identifying fonts as a single object, rather than specifying a font by its attributes with each occurrence." So in this case, tkinter is letting a particularly confusing Tk detail show through.

Note that providing a name isn't required, and how confusing the result is when not providing font names:

>>> Font(family='DejaVu Sans', size=12) == Font(family='DejaVu Sans', size=12)
False

Also, if you happen to try to create a font with a name which has already been used:

>>> Font(name='default font', family='DejaVu Sans', size=12)
<tkinter.font.Font object at 0x7f96ebbf5e60>
>>> Font(name='default font', family='DejaVu Sans', size=12)
Traceback [...]
_tkinter.TclError: named font "default font" already exists

One can avoid this error by passing exists=True, but using that when the font doesn't exist raises an exception:

>>> Font(name='other font', family='DejaVu Sans', size=10, exists=True)
Traceback [...]
_tkinter.TclError: named font other font does not already exist


My point is: Using named fonts in tkinter seems incredibly annoying. Using fonts without names seems to work better. But without using names, font comparison is meaningless.

It seems to me that we could change __eq__ to:
1. Keep the current behavior if both font objects have explicitly set names or if their automatically generated names are equal (i.e. `self is other`).
2. Otherwise, compare the the results of calling their .actual() methods.

This would require adding a simple internal flag in __init__ to remember whether the name was set explicitly, since __init__ sets a unique name if not provided with one.



P.S. The "name" constructor parameter is confusing! Here even Terry J. Reedy got it wrong in IDLE font configuration dialog's code:
https://github.com/python/cpython/blob/d9ab95ff1fe81efdf70e545d536d9f6927d1ba81/Lib/idlelib/config.py#L745)
msg377589 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-09-28 08:18
P.P.S. Re-reading that piece of code for IDLE's font config dialog, that actually looks to be intentional and correct.
msg377602 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-09-28 15:23
I concur with Tal that negating size would be useless feature, and that we do not need an alternate equal() method. When we want to test that two strings are equal ignoring case we do not call a special method equalignorecase(), we just test that s1.casefold() == s2.casefold().

But I disagree that Font.__eq__() should be changed. The Font object is just a reference to the Tk font as the Path object is a reference to a file on filesystem. When we compare two Path objects we do not compare file sizes, modification times, permission bits and content. We just compare two full names. If two Path objects has the same full name, they are equal. If they full names are different, they are different, even if the corresponding files has the same content. The same is with Font objects.
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86017
2020-09-28 20:31:39taleinatsetassignee: docs@python

components: + Documentation
nosy: + docs@python
2020-09-28 16:25:18epainesetpull_requests: + pull_request21465
2020-09-28 15:23:53serhiy.storchakasetmessages: + msg377602
2020-09-28 08:18:09taleinatsetmessages: + msg377589
2020-09-28 08:14:07taleinatsetmessages: + msg377588
2020-09-26 15:07:20epainesetmessages: + msg377530
title: tkinter: add font neg and equal methods -> tkinter: add font equal methods
2020-09-26 13:22:28taleinatsetmessages: + msg377529
2020-09-26 13:15:51taleinatsetnosy: + taleinat
messages: + msg377528
2020-09-24 11:27:38epainesetkeywords: + patch
stage: patch review
pull_requests: + pull_request21436
2020-09-24 11:27:19epainecreate