Skip to Main Content
Programming Python, 3rd Edition
book

Programming Python, 3rd Edition

by Mark Lutz
August 2006
Intermediate to advanced content levelIntermediate to advanced
1600 pages
51h 46m
English
O'Reilly Media, Inc.
Content preview from Programming Python, 3rd Edition

Configuring Widget Appearance

So far, all the buttons and labels in examples have been rendered with a default look-and-feel that is standard for the underlying platform. With my machine’s color scheme, that usually means that they’re gray on Windows. Tkinter widgets can be made to look arbitrarily different, though, using a handful of widget and packer options.

Because I generally can’t resist the temptation to customize widgets in examples, I want to cover this topic early on the tour. Example 9-1 introduces some of the configuration options available in Tkinter.

Example 9-1. PP3E\Gui\Tour\config-label.py

from Tkinter import *
root = Tk( )
labelfont = ('times', 20, 'bold')                  # family, size, style
widget = Label(root, text='Hello config world')
widget.config(bg='black', fg='yellow')             # yellow text on black label
widget.config(font=labelfont)                      # use a larger font
widget.config(height=3, width=20)                  # initial size: lines,chars
widget.pack(expand=YES, fill=BOTH)
root.mainloop( )

Remember, we can call a widget’s config method to reset its options at any time, instead of passing all of them to the object’s constructor. Here, we use it to set options that produce the window in Figure 9-1.

A custom label appearance

Figure 9-1. A custom label appearance

This may not be completely obvious unless you run this script on a real computer (alas, I can’t show it in color here), but the label’s text here shows up in yellow ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Python, 3rd Edition

Learning Python, 3rd Edition

Mark Lutz

Publisher Resources

ISBN: 0596009259Supplemental ContentErrata Page