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

(yzsuai) #1

To avoid this problem, we can either be careful to fetch before destroying, or use tkinter
variables, the subject of the next section.


tkinter “Variables” and Form Layout Alternatives


Entry widgets (among others) support the notion of an associated variable—changing
the associated variable changes the text displayed in the Entry, and changing the text
in the Entry changes the value of the variable. These aren’t normal Python variable
names, though. Variables tied to widgets are instances of variable classes in the tkinter
module library. These classes are named StringVar, IntVar, DoubleVar, and Boolean
Var; you pick one based on the context in which it is to be used. For example, a String
Var class instance can be associated with an Entry field, as demonstrated in
Example 8-20.


Example 8-20. PP4E\Gui\Tour\entry3.py


"""
use StringVar variables
lay out by columns: this might not align horizontally everywhere (see entry2)
"""


from tkinter import *
from quitter import Quitter
fields = 'Name', 'Job', 'Pay'


def fetch(variables):
for variable in variables:
print('Input => "%s"' % variable.get()) # get from var


Figure 8-25. entry2-modal (and entry3-modal) displays


454 | Chapter 8: A tkinter Tour, Part 1

Free download pdf