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

stewart at midtoad.homelinux.org stewart at midtoad.homelinux.org
Sat Sep 11 00:07:30 CEST 2004


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?

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

----

title = 'FontSize demonstration'

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

import Tkinter
import tkFont
import Pmw

class Demo:
    def __init__(self, parent):

        myVar = Tkinter.StringVar()
        self.myVar = myVar
        myVar.set("('Helvetica', 10, 'normal')")
        s = myVar.get()
        print 'myVar is ', str(s)
        self.myVar = myVar
        #CallbackName = self.myVar.trace_variable('w', callbackFunc)

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

        fontTiny = tkFont.Font(family="Helvetica", size=6)
        fontSmall = tkFont.Font(family="Helvetica", size=8)
        fontAverage = tkFont.Font(family="Helvetica", size=10)
        fontBig = tkFont.Font(family="Helvetica", size=12)
        fontHuge = tkFont.Font(family="Helvetica", size=14)
        fontMonstrous = tkFont.Font(family="Helvetica", size=18)
        self.fontTiny = fontTiny
        self.fontSmall = fontSmall
        self.fontAverage = fontAverage
        self.fontBig = fontBig
        self.fontHuge = fontHuge
        self.fontMonstrous = fontMonstrous
        
       
	# 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)

	menuBar.addmenuitem('Options', 'command', 'Change font size',
		command = lambda: self.chgFont(),
        font=self.font,
		label = 'Change 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=eval(self.myVar.get()),
		background = 'white',
		foreground = 'black',
		padx = 30,
		pady = 30)
	self.mainPart.pack(fill = 'both', expand = 1)

    def setFont(self, size):
        print 'current font: ',self.font
        #('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')}
        '''
        newFont = {'tiny': self.fontTiny,
                    'small': self.fontSmall,
                    'average': self.fontAverage,
                    'big': self.fontBig,
                    'huge': self.fontHuge,
                    'monstrous': self.fontMonstrous}
        '''
        self.font = newFont[size]
        print 'new font size: ', size, ' ', str(self.font)
        self.menuBar.update_idletasks
        
        self.myVar.set("('Helvetica', 6, 'normal')")
        s = self.myVar.get()
        print 'myVar is now ', s
        #self.mainPart.configure(font=self.font)
        root.update_idletasks()

    def chgFont(self):
        print 'current font: ',self.font
        #('tiny', 'small', 'average', 'big', 'huge')
        
        self.myVar.set("('Helvetica', 6, 'normal')")
        s = self.myVar.get()
        print 'myVar is now ', s
        root.update_idletasks()

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