Hi,

Thus spoketh Wayne Werner <waynejwer...@gmail.com> 
unto us on Mon, 22 Nov 2010 15:39:40 -0600:

> On Mon, Nov 22, 2010 at 3:21 PM, <pyt...@bdurham.com> wrote:
> 
> > Is there a simple way to change the font style of widgets without
> > explictly knowing the widget's font?
> >
> > For example: if I want to toggle the bold state of a widget's text,
> > do I need to determine its font, then build an equivalent font
> > object/expression with the font style I need, and pass that full font
> > description to widget.config() or can I use a shortcut technique that
> > allows me to pass 'bold' or 'normal' to control a widget's font.
> >
> 
> Label(root, text='Font fun', font=('Arial', 100, 'bold')).config()
> ['font'] returns a tuple - and since you can't modify a tuple, you'll
> have to determine the current font. There may be a shorter way, but
> here's what you can do:

If you don't explicitely define the label's font, the font is a string
(at least here, on linux):

>>> from Tkinter import *
>>> l=Label(text='foo')
>>> l.pack()
>>> l.cget('font')
'Helvetica -12 bold'

so you can do:

def swapfont():
    if 'bold' in l.cget('font'):
            l.config(font=l.cget('font').replace('bold', 'normal'))
    elif 'normal' in l.cget('font'):
            l.config(font=l.cget('font').replace('normal', 'bold'))
    else:
            l.config(font=l.cget('font')+' bold')
 
which is not too nice, either.
If you want to do fancy things with fonts, I'd try tkFont, with a
tkFont.Font() you can easily configure all kinds of font attributes.

Regards

Michael



.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

The more complex the mind, the greater the need for the simplicity of
play.
                -- Kirk, "Shore Leave", stardate 3025.8
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to