[Tkinter-discuss] Re: [TkinterHow to change font sizes in a Tkinter
app?
stewart at midtoad.homelinux.org
stewart at midtoad.homelinux.org
Fri Sep 10 17:56:25 CEST 2004
I'd like to be able to change font size in my Tkinter app. I've attached a
sample app below that allow for this. Problem is, the method is run when the
menus are drawn, but doesn't run afterward. How do I stop the font sizes from
changing when the app is started up? One way might be a lambda function, but is
there any other cleaner solution? Thanks!
cheers
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 = self.setFont(size),
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
root.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