Example 7-8. PP4E\Gui\Intro\gui1g.py—config and titles
from tkinter import *
root = Tk()
widget = Label(root)
widget.config(text='Hello GUI world!')
widget.pack(side=TOP, expand=YES, fill=BOTH)
root.title('gui1g.py')
root.mainloop()
The config method (which can also be called by its synonym, configure) can be called
at any time after construction to change the appearance of a widget on the fly. For
instance, we could call this label’s config method again later in the script to change the
text that it displays; watch for such dynamic reconfigurations in later examples in this
part of the book.
Notice that this version also calls a root.title method; this call sets the label that
appears at the top of the window, as pictured in Figure 7-4. In general terms, top-level
windows like the Tk root here export window-manager interfaces—i.e., things that
have to do with the border around the window, not its contents.
Figure 7-4. gui1g with expansion and a window title
Just for fun, this version also centers the label upon resizes by setting the expand and
fill pack options. In fact, this version makes just about everything explicit and is more
representative of how labels are often coded in full-blown interfaces; their parents,
expansion policies, and attachments are usually spelled out rather than defaulted.
One More for Old Times’ Sake
Finally, if you are a minimalist and you’re nostalgic for old Python coding styles, you
can also program this “Hello World” example as in Example 7-9.
Example 7-9. PP4E\Gui\Intro\gui1-old.py—dictionary calls
from tkinter import *
Label(None, {'text': 'Hello GUI world!', Pack: {'side': 'top'}}).mainloop()
376 | Chapter 7: Graphical User Interfaces