473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.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
Oct 19 '05 #1
4 2673

"Ron Provost" <ro****@cox.net> 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
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.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+-'])"
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(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>

Oct 19 '05 #4
In article <op**************@eb.pragmadev>,
"Eric Brunel" <er*********@despammed.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
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
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,...
5
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...
7
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...
44
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...
0
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
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...
4
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...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.