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

(yzsuai) #1

else:
showinfo('No', 'Quit has been cancelled')


errmsg = 'Sorry, no Spam allowed!'
Button(text='Quit', command=callback).pack(fill=X)
Button(text='Spam', command=(lambda: showerror('Spam', errmsg))).pack(fill=X)
mainloop()


A lambda anonymous function is used here to wrap the call to showerror so that it is
passed two hardcoded arguments (remember, button-press callbacks get no arguments
from tkinter itself). When run, this script creates the main window in Figure 8-5.


Figure 8-5. dlg1 main window: buttons to trigger pop ups


When you press this window’s Quit button, the dialog in Figure 8-6 is popped up by
calling the standard askyesno function in the tkinter package’s messagebox module. This
looks different on Unix and Macintosh systems, but it looks like you’d expect when
run on Windows (and in fact varies its appearance even across different versions and
configurations of Windows—using my default Window 7 setup, it looks slightly dif-
ferent than it did on Windows XP in the prior edition).


The dialog in Figure 8-6 blocks the program until the user clicks one of its buttons; if
the dialog’s Yes button is clicked (or the Enter key is pressed), the dialog call returns
with a true value and the script pops up the standard dialog in Figure 8-7 by calling
showwarning.


Figure 8-6. dlg1 askyesno dialog (Windows 7)


Dialogs | 427
Free download pdf