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

stewart at midtoad.homelinux.org stewart at midtoad.homelinux.org
Fri Sep 10 19:57:04 CEST 2004


I found out how to stop my font-changing method to be called when I draw the
menus.  I simply created a lambda function to pass the new font size to
font-changing method.  

I've also confirmed that the method does actually change the default font size
for my application.  

Now my only problem is how to get everything to be redrawn, so that the new font
size becomes visible.  Any help is appreciated.  The latest version of my test
app is below.

thanks
Stewart

---
title = 'FontSize demonstration'

# Import Pmw from this directory tree.
import sys
sys.path[:0] = ['../../..']

import Tkinter
import Pmw

class Demo:
    def __init__(self, parent):

        #Define a base font for use in all widgets
        #this can be changed later in the Options menu
        self.font=('Helvetica', 10, 'normal')


	# 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')

#		command = PrintOne('Action: exit'),

	menuBar.addmenu('Options', 'Set user preferences', font=self.font)
	menuBar.addcascademenu('Options', 'Size',
		'Set some other preferences', traverseSpec = 'z', tearoff = 1, font=self.font)
	for size in ('tiny', 'small', 'average', 'big', 'huge', 'monstrous'):
	    menuBar.addmenuitem('Size', 'command', 'Set size to ' + size,
                    command = lambda ss=size: self.setFont(ss),
                    font=self.font,
                    label = size)

	# 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)

    def setFont(self, size):
        print 'current font size: ',self.font[1]
        #('tiny', 'small', 'average', 'big', 'huge')
        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.font = newFont[size]
        print 'new font size: ', size, self.font
        self.menuBar.update

class PrintOne:
    def __init__(self, text):
        self.text = text

    def __call__(self):
        print self.text

######################################################################

# 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