Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Tkinter or Python issue?

Reply
Thread Tools

Tkinter or Python issue?

 
 
Ron Provost
Guest
Posts: n/a
 
      10-19-2005
Hello,

I'm using python 2.4.2 on Win XP Pro. I'm trying to understand a behavior
I'm seeing in some Tkinter code I have. I've reduced my question to a small
piece of code:


#####BEGIN CODE
#################
import Tkinter as Tk
import tkFont

sampleText = """Here is a test string. This is more text
Here is a second line of text. How much
more can I type. I can't think of anything else to type.
"""

root = Tk.Tk( )
t = Tk.Text( root )
t.pack( )

t.insert( Tk.END, sampleText )

t.tag_config( 'AB', font=tkFont.Font( family='ariel', size=24,
weight=tkFont.BOLD ) )
t.tag_config( 'TBU', font=tkFont.Font( family='times', size=10,
weight=tkFont.BOLD, underline=1 ) )

t.tag_add( 'AB', '1.8', '1.15' )
t.tag_add( 'TBU', '2.10', '2.30' )

root.mainloop( )
#################
#######END CODE

Now when I run this I expect to see a small bit of the sampleText in ariel
bold and another bit in times bold underline, instead I see both bits in the
later style. Interestingly, if I create the Font objects before calling
t.tag_config() (i.e. replace the two t.tag_config( ) lines with the
following):


f1 = font=tkFont.Font( family='ariel', size=24, weight=tkFont.BOLD )
f2 = font=tkFont.Font( family='times', size=10, weight=tkFont.BOLD,
underline=1 )
t.tag_config( 'AB', font=f1 )
t.tag_config( 'TBU', font=f2 )


In rerunning the code, I see each bit of text now styled differently. This
is the behavior that I both expect and want. Does anybody know why the two
bits of code result in different behavior? Is it a Python thing or a
Tkinter thing?

Thanks for your feedback.

Ron


 
Reply With Quote
 
 
 
 
Eugene Druker
Guest
Posts: n/a
 
      10-19-2005

"Ron Provost" <(E-Mail Removed)> wrote in message
news:98i5f.3686$vS1.1306@dukeread03...
> [snip]
> t.insert( Tk.END, sampleText )
>
> t.tag_config( 'AB', font=tkFont.Font( family='ariel', size=24,
> weight=tkFont.BOLD ) )
> t.tag_config( 'TBU', font=tkFont.Font( family='times', size=10,
> weight=tkFont.BOLD, underline=1 ) )
>
> t.tag_add( 'AB', '1.8', '1.15' )
> t.tag_add( 'TBU', '2.10', '2.30' )
>
> root.mainloop( )
> [snip]


tkFont.Font(...) is a class instance, while you need font description.
Try:
t.tag.config( 'TBU', font=('times', 12, 'bold','underline') )
t.tag_config( 'AB', font=('arial',24,'bold') )

Eugene


 
Reply With Quote
 
 
 
 
Eric Brunel
Guest
Posts: n/a
 
      10-19-2005
On Tue, 18 Oct 2005 22:30:33 -0400, Ron Provost <(E-Mail Removed)> wrote:

> Hello,
>
> I'm using python 2.4.2 on Win XP Pro. I'm trying to understand a behavior
> I'm seeing in some Tkinter code I have. I've reduced my question to a small
> piece of code:
>
>
> #####BEGIN CODE
> #################
> import Tkinter as Tk
> import tkFont
>
> sampleText = """Here is a test string. This is more text
> Here is a second line of text. How much
> more can I type. I can't think of anything else to type.
> """
>
> root = Tk.Tk( )
> t = Tk.Text( root )
> t.pack( )
>
> t.insert( Tk.END, sampleText )
>
> t.tag_config( 'AB', font=tkFont.Font( family='ariel', size=24,
> weight=tkFont.BOLD ) )
> t.tag_config( 'TBU', font=tkFont.Font( family='times', size=10,
> weight=tkFont.BOLD, underline=1 ) )


Here is what I think is happening:
- The first tag_config creates a font using tkFont.Font. At tcl level, this font is not known as an object as in Python, but just as a font name, which is a string. This name happens to be built using the internal identifier for the Python object.
- Once this tag_config is over, no Python variable references your tkFont.Font instance anymore. It is is fact still known at tcl level (via the font name), but Python doesn't know that. So the reference counter for your tkFont.Font instance falls to 0, and the object is discarded.
- The second tag_config seems to create a new font using tkFont.Font. Unfortunately, since the previous font has been discarded, the space occupied by this font is reused for the new font. So the new font happens to have the same internal identifier as the font object created by the first tkFont.Font. So its name is in fact the same as your previous font, and is registered as such at tcl level. So in fact, you didn't create a new font at tcl level, but modified the previous one.

All this actually happens by accident. If you add something allocating some memory between the two tag_config, you'll certainly see your code working. It works when I run it myself...

[snip]
> f1 = font=tkFont.Font( family='ariel', size=24, weight=tkFont.BOLD )
> f2 = font=tkFont.Font( family='times', size=10, weight=tkFont.BOLD,
> underline=1 )
> t.tag_config( 'AB', font=f1 )
> t.tag_config( 'TBU', font=f2 )


You should now see why it works here: your first tkFont.Font is remembered at Python level in a variable. So it is not discarded once the tag_config is over. So the second tkFont.Font is not allocated at the same location, so it doesn't have the same id, and it doesn't have the same name at tcl level. This is the general solution to the problem: keep your fonts in Python variables, so they won't be discarded and their names will never be re-used. You could have written:

f1 = font=tkFont.Font( family='ariel', size=24, weight=tkFont.BOLD )
t.tag_config( 'AB', font=f1 )
f2 = font=tkFont.Font( family='times', size=10, weight=tkFont.BOLD, underline=1 )
t.tag_config( 'TBU', font=f2 )

This should still work. The order is not important; it's just the fact that your fonts are actually known at Python level which prevents Tkinter to reuse their name.

BTW, this is a variant of a well known problem biting newbies regularly, documented here:
http://tkinter.unpythonic.net/wiki/Images

It's basically the same problem for images: it does not suffice to reference an image at tcl level; it must also be referenced at Python level or it will be discarded by Python and you won't be able to use it in your widgets.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-19-2005
Eugene Druker wrote:

> tkFont.Font(...) is a class instance, while you need font description.


Font instances are font descriptors.

>>> f = tkFont.Font(family="ariel", size=24, weight=tkFont.BOLD)
>>> f

<tkFont.Font instance at 0x00A3AC60>
>>> print f

font10726496

> t.tag.config( 'TBU', font=('times', 12, 'bold','underline') )
> t.tag_config( 'AB', font=('arial',24,'bold') )


the problem here is a variation of the old garbage collection problem (Tcl
uses names as references, so the fact that Tk uses an object isn't enough to
keep Python's GC away from it). the solution is to hold on to the objects
in Python.

</F>



 
Reply With Quote
 
Russell E. Owen
Guest
Posts: n/a
 
      10-19-2005
In article <(E-Mail Removed)>,
"Eric Brunel" <(E-Mail Removed)> wrote:

>You should now see why it works here: your first tkFont.Font is remembered at
>Python level in a variable. So it is not discarded once the tag_config is
>over. So the second tkFont.Font is not allocated at the same location, so it
>doesn't have the same id, and it doesn't have the same name at tcl level. This
>is the general solution to the problem: keep your fonts in Python variables,
>so they won't be discarded and their names will never be re-used.


Yes. I consider this dangerous behavior, by the way and submitted a
patch (that was not accepted) that would prevent this garbage collection.

tkFont is Tkinter's interface to tk named fonts. If you create a tkFont
instance for a named font and then let it disappear, the named font
disappears, even if other tkFont instances exist that map the same named
font.

-- Russell
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What is the differences between tkinter in windows and Tkinter inthe other platform? Hidekazu IWAKI Python 0 12-15-2009 05:58 AM
What is the differences between tkinter in windows and Tkinter in theother platform? Hidekazu IWAKI Python 1 12-14-2009 03:44 PM
from Tkinter import *,win = Tk() "from Tkinter import *" Pierre Dagenais Python 0 08-03-2008 10:33 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-23-2004 02:31 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-20-2004 12:07 PM



Advertisments