[Tkinter-discuss] Change the font style (bold, italic, stikethru, normal) of widgets without explictly knowing font?

Wayne Werner waynejwerner at gmail.com
Mon Nov 22 22:39:40 CET 2010


On Mon, Nov 22, 2010 at 3:21 PM, <python at 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:

mylabel.config(font=mylabel.config()['font'][4][:2] + ('normal',))
mylabel.config(font=mylabel.config()['font'][4][:2] + ('bold', 'italics'))

And that should work.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20101122/64f25345/attachment-0001.html>


More information about the Tkinter-discuss mailing list