[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

This makes the window in just two lines, albeit arguably gruesome ones! This scheme
relies on an old coding style that was widely used until Python 1.3, which passed
configuration options in a dictionary instead of keyword arguments.# In this scheme,
packer options can be sent as values of the key Pack (a class in the tkinter module).


The dictionary call scheme still works and you may see it in old Python code, but it’s
probably best to not do this in code you type. Use keywords to pass options, and use
explicit pack method calls in your tkinter scripts instead. In fact, the only reason I didn’t
cut this example completely is that dictionaries can still be useful if you want to compute
and pass a set of options dynamically.


On the other hand, the func(*pargs, **kargs) syntax now also allows you to pass an
explicit dictionary of keyword arguments in its third argument slot:


options = {'text': 'Hello GUI world!'}
layout = {'side': 'top'}
Label(None, **options).pack(**layout) # keyword must be strings

Even in dynamic scenarios where widget options are determined at run time, there’s
no compelling reason to ever use the pre-1.3 tkinter dictionary call form.


Packing Widgets Without Saving Them


In gui1c.py (shown in Example 7-4), I started packing labels without assigning them to
names. This works, and it is an entirely valid coding style, but because it tends to
confuse beginners at first glance, I need to explain why it works in more detail here.


In tkinter, Python class objects correspond to real objects displayed on a screen; we
make the Python object to make a screen object, and we call the Python object’s meth-
ods to configure that screen object. Because of this correspondence, the lifetime of the
Python object must generally correspond to the lifetime of the corresponding object on
the screen.


Luckily, Python scripts don’t usually have to care about managing object lifetimes. In
fact, they do not normally need to maintain a reference to widget objects created along
the way at all unless they plan to reconfigure those objects later. For instance, it’s
common in tkinter programming to pack a widget immediately after creating it if no
further reference to the widget is required:


Label(text='hi').pack() # OK

This expression is evaluated left to right, as usual. It creates a new label and then im-
mediately calls the new object’s pack method to arrange it in the display. Notice,
though, that the Python Label object is temporary in this expression; because it is not


#In fact, Python’s pass-by-name keyword arguments were first introduced to help clean up tkinter calls such
as this one. Internally, keyword arguments really are passed as a dictionary (which can be collected with the
**name argument form in a def header), so the two schemes are similar in implementation. But they vary
widely in the number of characters you need to type and debug.


tkinter Coding Alternatives | 377
Free download pdf