R
Ron Provost
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
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