[Tkinter-discuss] Re: How to change font sizes in a Tkinter app?

Michael Lange klappnase at freenet.de
Sat Sep 11 02:37:05 CEST 2004


On Fri, 10 Sep 2004 16:07:30 -0600
stewart at midtoad.homelinux.org wrote:

> hi again:
> 
> Giving further thought to the problem of how to have a live change of font sizes
> in a Tkinter app, I tried using a Tkinter.StringVar.  The modified test app is
> below.  It works, as least insofar as it correctly gets and sets the var, but I
> still see any visible changes. Somehow I need to force a re-draw of the entire
> app. any ideas?
> 

Hi Stewart,

maybe the easiest would be to put all the widgets that use the font in a list and then do something like:

for widget in widgetlist:
    widget.configure(font=newFont)

when the user changes the font.

> I guess if this is not possible, I could simply write out the new font size to a
> .ini file, ask the user to restart the app, and read that .ini file when I start
> up. Seems kinda kludgy, though.
> 
> S
> 
>
I think the usual way to do this is to use an option database which allows to override default tk options
like fonts and colors (in case you want to change the default font for the whole app). Entries in an option database
look like this:

*font : Helvetica -12
*Entry*background : white
*Listbox*background : white
*Listbox*exportSelection : 0
*selectBackground : #a0a
*selectForeground : white
*Scrollbar*Width : 12

If you store this in a file "optionDB" in the application's directory you can access the options with option_readfile():

root = Tk()
root.option_readfile(sys.path[0]+"/optionDB")

The options apply to all children of root that are created after option_readfile() is called, however
I don't think it's possible to apply changes to the option database to widgets that already exist this way.

I hope this helps

Michael


More information about the Tkinter-discuss mailing list