473,543 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter or Python issue?

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.Fon t( family='ariel', size=24,
weight=tkFont.B OLD ) )
t.tag_config( 'TBU', font=tkFont.Fon t( family='times', size=10,
weight=tkFont.B OLD, 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.Fon t( family='ariel', size=24, weight=tkFont.B OLD )
f2 = font=tkFont.Fon t( family='times', size=10, weight=tkFont.B OLD,
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
Oct 19 '05 #1
4 2677

"Ron Provost" <ro****@cox.net > wrote in message
news:98i5f.3686 $vS1.1306@duker ead03...
[snip]
t.insert( Tk.END, sampleText )

t.tag_config( 'AB', font=tkFont.Fon t( family='ariel', size=24,
weight=tkFont.B OLD ) )
t.tag_config( 'TBU', font=tkFont.Fon t( family='times', size=10,
weight=tkFont.B OLD, 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','underli ne') )
t.tag_config( 'AB', font=('arial',2 4,'bold') )

Eugene
Oct 19 '05 #2
On Tue, 18 Oct 2005 22:30:33 -0400, Ron Provost <ro****@cox.net > 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.Fon t( family='ariel', size=24,
weight=tkFont.B OLD ) )
t.tag_config( 'TBU', font=tkFont.Fon t( family='times', size=10,
weight=tkFont.B OLD, 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.Fon t( family='ariel', size=24, weight=tkFont.B OLD )
f2 = font=tkFont.Fon t( family='times', size=10, weight=tkFont.B OLD,
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.Fon t( family='ariel', size=24, weight=tkFont.B OLD )
t.tag_config( 'AB', font=f1 )
f2 = font=tkFont.Fon t( family='times', size=10, weight=tkFont.B OLD, 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.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"
Oct 19 '05 #3
Eugene Druker wrote:
tkFont.Font(... ) is a class instance, while you need font description.
Font instances are font descriptors.
f = tkFont.Font(fam ily="ariel", size=24, weight=tkFont.B OLD)
f <tkFont.Font instance at 0x00A3AC60> print f

font10726496
t.tag.config( 'TBU', font=('times', 12, 'bold','underli ne') )
t.tag_config( 'AB', font=('arial',2 4,'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>

Oct 19 '05 #4
In article <op************ **@eb.pragmadev >,
"Eric Brunel" <er*********@de spammed.com> 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
Oct 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3339
by: Tom Locke | last post by:
Hi All, I'm having trouble with the python shell within emacs. It's hanging when I use tkinter. Setup is: Windows XP emacs 21.3 py-mode 4.6 Recipe:
1
2003
by: jhujsak | last post by:
Hi, Can anyone point me to any good examples of how to get started developing new Tkinter widgets in Tcl/Tk? I have read a number of the popular books on Python and Tkinter: - Grayson, J.E., Python and Tkinter Programming, Manning Publications, 2000 - Lutz, M., Programming Python, O'Reilly, 1996.
5
3542
by: Ben Kovitz | last post by:
Hi, I just tried to run Tkinter on OS X 10.3.9 under Python 2.4.3, and I'm getting a bus error as soon as I call Tk(). Googling has turned up info other Tkinter bus errors, but not this one that occurs right away, before doing anything fancy. Tk/Tcl is definitely installed on my computer, as verified by running "wish" and seeing the window...
7
1937
by: krishnakant Mane | last post by:
hello all, I seam to have noticed this a bit late but it appears to me that tkinter is being used very widely for gui development on all platform? is that right? since fredric lundh has written a very good introduction to tkinter (was that just an intro?), I have got keen interest to know the following. may be fredric himself might put some...
44
4955
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file processed by this script, where the concerned tmp file is only actually written to when PythonWin is closed. In other words, after I run this script, one of...
0
1526
by: wolfonenet | last post by:
Hi All, My setup is: WinXP Python 2.5.1 TKinter version: $Revision: 50704 $ Tcl: 8.4 Debugger: WinPdb
8
3267
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to my pc. But, it is not getting installed and is failing by throwing the below errors. Should i need to configure / install any specific files for...
4
4964
by: njwilson23 | last post by:
I'm having trouble with tkinter on a new installation of Python (2.6), built with the framework option from source that was downloaded from python.org. I'm running OS 10.4 on a PowerPC G4. The problem first arose when I tried to run matplotlib - it couldn't find tcl/tk because it was searching for 8.5, and I had 8.4. I found and built...
0
1785
by: Guilherme Polo | last post by:
On 10/29/08, Olrik Lenstra <o.lenstra@gmail.comwrote: It will be a combination of commands, not a single one. Initially I considered this as "probably without solution", since tcl acquired a yield command just in the 8.6a3 release, but then I looked at wx.SafeYield code and apparently it is possible to replicate it. Here is an initial...
0
7412
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5892
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3395
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3395
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1830
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
979
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.