windows than to distract the user with pop ups). But where appropriate, such pop ups
save coding time and provide a nice native look-and-feel.
A “smart” and reusable Quit button
Let’s put some of these canned dialogs to better use. Example 8-7 implements an at-
tachable Quit button that uses standard dialogs to verify the quit request. Because it’s
a class, it can be attached and reused in any application that needs a verifying Quit
button. Because it uses standard dialogs, it looks as it should on each GUI platform.
Example 8-7. PP4E\Gui\Tour\quitter.py
"""
a Quit button that verifies exit requests;
to reuse, attach an instance to other GUIs, and re-pack as desired
"""
from tkinter import * # get widget classes
from tkinter.messagebox import askokcancel # get canned std dialog
class Quitter(Frame): # subclass our GUI
def init(self, parent=None): # constructor method
Frame.init(self, parent)
self.pack()
widget = Button(self, text='Quit', command=self.quit)
widget.pack(side=LEFT, expand=YES, fill=BOTH)
def quit(self):
ans = askokcancel('Verify exit', "Really quit?")
if ans: Frame.quit(self)
if name == 'main': Quitter().mainloop()
This module is mostly meant to be used elsewhere, but it puts up the button it imple-
ments when run standalone. Figure 8-10 shows the Quit button itself in the upper left,
and the askokcancel verification dialog that pops up when Quit is pressed.
Figure 8-10. Quitter, with askokcancel dialog
Dialogs | 429