[Tkinter-discuss] tkFont issue; what patch will fly?

Russell E. Owen rowen at cesmail.net
Wed Mar 29 19:50:50 CEST 2006


Tkinter's support for tk named fonts has problems.

tk named fonts allow one to change the font of a set of widgets all at 
the same time, e.g. to support user-specified fonts. One can create a tk 
named font, add an entry to the tk option database, and then new widgets 
of the appropriate type will automatically use that named font (by 
default). Very handy for user preferences!

The Tkinter interface to tk named fonts is the tkFont.Font object. 
Originally tkFont.Font objects could create tk named fonts, but not visa 
versa (e.g. given a widget, you could not get a tkFont.Font 
representation of the named font it used), which was clumsy.

I submitted a patch awhile ago that was *partly* implemented to fix 
this. Unfortunately, the part left out resulted in a mess that I'd like 
to clean up.

The problem is garbage collection. At present all tkFont.Font objects 
delete their associated tk named font when they are garbage-collected. 
This was already a pain when the tkFont.Font object created the tk named 
font (since one had to keep the tkFont.Font object around even if one 
had no more use for it) but it's a disaster when the tkFont.Font is just 
a copy of an existing tk named font.

My originally proposed solution was to never have tkFont.Font objects 
delete tk named fonts. I still think this is the best thing to do 
because one can create a tk named font and add it to the tk options 
database, then throw it away. To manipulate it later, just create a 
widget of the appropriate type, get a tkFont.Font object for its font, 
change that and throw it away again.

However, that does mean a change in Tkinter behavior and a possible 
memory leak if somebody was mad enough to create hordes of named fonts, 
only to want to get rid of them later (I can't imagine a sane reason for 
doing that).

An alternative is to have tkFont.Font only auto-delete the named font if 
the tkFont.Font was not a copy of an existing named font. Then at least 
one can safely get that copy and throw it away without hosing one's 
ability to manipulate widgets.

I'll happily submit either patch (or some other if folks feel another 
alternative is better).

What do you think? What will fly?

-- Russell



More information about the Tkinter-discuss mailing list