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

stewart at midtoad.homelinux.org stewart at midtoad.homelinux.org
Tue Sep 14 17:10:48 CEST 2004


Quoting Martin Franklin <mfranklin1 at gatwick.westerngeco.slb.com>:

> 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

Now this gets curioser and curioser.  When I add your code in, the top-level
menu entries (File, Options) no longer change fonts. (Maybe I copied or indented
your code wrong?).  If I leave my test app as-is, all of the menu items' fonts
change.  I'll attach my app as an attachment so it retains its formatting,
though a text version is below, for those that don't get attachments.  My code
for the update method comes first, with yours as a new method update2; switching
the names will run your version instead of mine.   Do you get the same results
as I do when you run this app?

BTW I'm running Win2k and Python 2.3.4, nearly the same configuration as yours.

cheers
S
-------------- next part --------------
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

    def update2(self, widget):
        try:
            # for each menu item try setting the font
            for i in range(widget.index("end") + 1):
                t = widget.type(i)
                ## unless it's a tearoff or separator
                if t in ("tearoff", "separator"):
                    continue
                widget.entryconfigure(i, font=self.newFont[self.size])
        except:
            pass

    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 update2(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()


More information about the Tkinter-discuss mailing list