[Tkinter-discuss] Re: How to change font sizes in a Tkinter app?
Martin Franklin
mfranklin1 at gatwick.westerngeco.slb.com
Sat Sep 11 16:47:46 CEST 2004
On Fri, 10 Sep 2004 21:53:33 -0600, <stewart at midtoad.homelinux.org> wrote:
> Quoting Michael Lange <klappnase at freenet.de>:
>
>> 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 want to change all my menu entries to use the new font. But,I don't
> see how
> to get the right name for those items to use in the list since each menu
> entry
> doesn't have a discrete object name, like say a button would
> (btn=Tkinter.Button, so you could have widgetlist=[btn,lbl,btn2]. I read
> something about using component() to achieve this, but the exact
> implementation
> escapes me - the example I saw related to notebook tabs rather than menu
> entries.
>
>> 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:
>
> thanks for this useful detail. I'll give it a try on Monday. I did
> try a
> similar idea, putting the current font name into another module which I
> called
> basefont.py. I built a test app and was able to change the font this
> way (the
> user has to quit and restart the app, but that's okay). The strange
> thing is,
> when I copy the methods into my real app, it doesn't work due to a
> scoping
> problem in the lambda function I use. I'll post the details on that on
> Monday.
>
You could try this little snippet...:
from Tkinter import *
class WidgetWalker:
def __init__(self, parent):
self.parent = parent
def __call__(self):
self.walk(self.parent)
def update(self, widget):
try:
widget["font"] = "Helvetica 18"
except:
pass
try:
widget["fg"] = "pink"
except:
pass
def walk(self, top):
self.update(top)
for child in top.children.values():
self.walk(child)
if __name__=="__main__":
root = Tk()
f = Frame(root)
l = Label(f, text="Label")
l.pack()
b = Button(f, text="Button", command=WidgetWalker(root))
b.pack()
inf = Frame(f)
l2 = Label(inf, text="Label 2")
l2.pack()
inf2 = Frame(inf)
l3 = Label(inf2, text="Label 3")
l3.pack()
inf2.pack()
inf.pack()
f.pack()
root.mainloop()
It relys on the children dictionary every Tkinter widget has.
Cheers
Martin
More information about the Tkinter-discuss
mailing list