[Tkinter-discuss] Re: How to change font sizes in a Tkinter app
Martin Franklin
mfranklin1 at gatwick.westerngeco.slb.com
Tue Sep 14 09:45:22 CEST 2004
On Mon, 13 Sep 2004 18:23:05 -0600, <stewart at midtoad.homelinux.org> wrote:
> I posted a question on this topic a few days, and got three interesting,
> and
> quite varied, approaches to a solution from Jeff Epler, Martin Franklin
> and
> Michael Lange. One of the approaches apparently works under Linux, but
> wouldn't
> work (for me) under Windows, at least not yet. I had the most success
> adopting
> Martin's approach, which is contained in modified form in the code
> sample below.
> Note that my sample app makes use of Pmw, which you can get from
> http://pmw.sf.net. My app allows the user to change the font face or
> the font
> colour interactively. I leave it as an exercise for the reader to be
> able to
> change the font face as well (e.g. from Helvetica to Roman or Courier).
>
> This list is invaluable for me, because I'm coming across material that
> seems to
> be absent from any of the information sources I have so far for Tkinter.
>
> thanks again!
>
> cheers
> Stewart
>
Stewart,
I got your code running good work! However it doesn't update the fonts
of the menu items.
At least not on my Windows machine - Win 2000 Python 2.3.3
For that I had to add some of Jeffs code see below
>
>
>
> ---
> #MenuBarFontApp.py
>
> title = 'FontChange demonstration'
>
> # Import Pmw from this directory tree.
> import sys
> sys.path[:0] = ['../../..']
>
> import Tkinter
> import Pmw
>
> class Demo:
> def __init__(self, parent):
>
> self.font=('Helvetica', 10, 'normal')
> print 'base font is ', self.font
> # Create and pack the MenuBar.
> menuBar = Pmw.MenuBar(parent,
> hull_relief = 'raised',
> hull_borderwidth = 1)
> menuBar.pack(fill = 'x')
> self.menuBar = menuBar
>
> # Add some buttons to the MenuBar.
> menuBar.addmenu('File', 'Close this window or exit', font=self.font)
> menuBar.addmenuitem('File', 'command', 'Close this window',
> command = PrintOne('Action: close'),
> font=self.font,
> label = 'Close')
> menuBar.addmenuitem('File', 'separator')
> menuBar.addmenuitem('File', 'command', 'Exit the application',
> command = lambda: sys.exit("Bye"),
> font=self.font,
> label = 'Exit')
>
> menuBar.addmenu('Options', 'Set general app options', font=self.font)
>
> menuBar.addcascademenu('Options', 'Font Size',
> 'Change global fonts', traverseSpec = 'z', tearoff = 1, font=self.font)
> for size in ('tiny', 'small', 'average', 'big', 'huge', 'monstrous'):
> menuBar.addmenuitem('Font Size', 'command', 'Set size to ' + size,
> command = lambda ss=size: WidgetWalker(ss),
> font=self.font,
> label = size)
>
> menuBar.addcascademenu('Options', 'Font Colour',
> 'Change global fonts', traverseSpec = 'z', tearoff = 1, font=self.font)
> for colour in ('black', 'blue', 'red', 'green', 'grey', 'brown'):
> menuBar.addmenuitem('Font Colour', 'command', 'Set size to '
> + colour,
> command = lambda ss=colour:
> WidgetWalker2(ss),
> font=self.font,
> label = colour)
>
> # Create and pack the main part of the window.
> self.mainPart = Tkinter.Label(parent,
> text = 'This is the\nmain part of\nthe window',
> font=self.font,
> background = 'white',
> foreground = 'black',
> padx = 30,
> pady = 30)
> self.mainPart.pack(fill = 'both', expand = 1)
>
> class PrintOne:
> def __init__(self, text):
> self.text = text
>
> def __call__(self):
> print self.text
>
> #set font size
> class WidgetWalker:
> def __init__(self,size):
> self.parent = root # parent
> self.size = size
> newFont = {'tiny': ('Helvetica', 6, 'normal'),
> 'small': ('Helvetica', 8, 'normal'),
> 'average': ('Helvetica', 10, 'normal'),
> 'big': ('Helvetica', 12, 'normal'),
> 'huge': ('Helvetica', 14, 'normal'),
> 'monstrous': ('Helvetica', 18, 'normal')}
> self.newFont = newFont
> self.walk(self.parent)
>
> def update(self, widget):
> try:
> widget["font"] = self.newFont[self.size]
> except:
> pass
## extra try: except: block for menu items
try:
# for each menu item try setting the font
for i in range(widget.index("end") + 1):
t = widget.type(i)
if t in ("tearoff", "separator"):
## unles it's a tearoff or separator
continue
widget.entryconfigure(i, font=self.newFont[self.size])
except:
pass
Cheers
Martin
>
> def walk(self, top):
> self.update(top)
> for child in top.children.values():
> print 'child is ', child
> self.walk(child)
>
> #set font colour
> class WidgetWalker2:
> def __init__(self,colour):
> self.parent = root # parent
> self.colour = colour
> self.walk(self.parent)
>
> def update(self, widget):
> try:
> widget["fg"] = self.colour
> except:
> pass
>
> def walk(self, top):
> self.update(top)
> for child in top.children.values():
> print 'child is ', child
> self.walk(child)
>
>
>
> ######################################################################
>
> # Create demo in root window for testing.
> if __name__ == '__main__':
> root = Tkinter.Tk()
> Pmw.initialise(root)
> root.title(title)
>
> exitButton = Tkinter.Button(root, text = 'Exit', command =
> root.destroy)
> exitButton.pack(side = 'bottom')
> widget = Demo(root)
> root.mainloop()
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
More information about the Tkinter-discuss
mailing list