我有一个“退出”按钮,它创建一个顶层窗口来询问用户是否确定要退出。如果单击“否”按钮,则窗口被破坏,但“退出”按钮不再起作用。呃,“退出”虚拟事件按键(CTRL-Q、Q 和 CTRL-C)也停止运行。这是为什么?
from Tkinter import *
class ButtonTestClass(Frame):
def __init__(self):
self.root=Tk()
self.root.title("No Button Tester!")
self.root.option_add('*font', ('arial', 14, 'bold'))
self.frame=Frame(self.root, height=120, width=600, \
borderwidth=2, relief=GROOVE, background='steelblue'
)
self.frame.pack(fill=X, expand=False)
self.frame.pack_propagate(False)
self.label = Label(self.frame, \
text='Why does the "Quit" button stop working\nafter the "No" button is clicked?', bg='steelblue')
self.label.pack(padx=25, pady=15, side=TOP)
self.root.event_add('<<ev_quit>>', '<Control-c>', '<Control-q>', '<q>')
self.root.bind('<<ev_quit>>', lambda e: self.bye_bye())
self.byeWindow = None
print "In constructor self.byeWindow - value: '%r' type: '%r'" % (self.byeWindow, type(self.byeWindow)) # <<< DEBUG >>>
def createQuitButton(self):
pbx=250; pby=70; pbw=75; pbh=25; pbp=10
self.quitBtn=Button(self.frame, text='Quit', activebackground='steelblue3', font='arial 10 bold', bg='steelblue', command=lambda: self.bye_bye())
self.quitBtn.place(x=pbx, y=pby, width=pbw, height=pbh)
pbx += pbw + pbp
def dontQuit(self, tlref):
self.byeWindow = None # <<<--- There was a 'misspelling' here; grrrr!
tlref.destroy()
print "In dontQuit(). self.byeWindow - value: '%r' type: '%r'" % (self.byeWindow, type(self.byeWindow)) # <<< DEBUG >>>
def bye_bye(self):
if self.byeWindow is not None:
return
self.byeWindow=Toplevel()
print "In bye_bye(), self.byeWindow - value: '%r' type: '%r'" % (self.byeWindow, type(self.byeWindow)) # <<< DEBUG >>>
self.byeWindow.title("Really quit?")
self.byeWindow.config(bg='steelblue', height=40, width=80)
sureMsgLabel=Label(self.byeWindow, text="Are you sure you want to quit?", font='arial 11 bold', bg='steelblue')
sureMsgLabel.pack(side=TOP, padx=10, pady=15)
yesButton=Button(self.byeWindow, text=" Yes. ", font='arial 10 bold', bg='steelblue', activebackground='steelblue3', command=lambda: quit())
yesButton.pack(side=LEFT, anchor=N, padx=40, pady=20)
yesButton.bind('<Key-Return>', lambda: yesButton.invoke())
noButton = Button(self.byeWindow, text=" No. ", activebackground='steelblue3', font='arial 10 bold', bg='steelblue', command=lambda: self.dontQuit(self.byeWindow))
noButton.focus_force()
noButton.pack(side=RIGHT, anchor=N, padx=40, pady=20)
noButton.bind('<Key-Return>', lambda: noButton.invoke())
bT = ButtonTestClass()
bT.createQuitButton()
if __name__ == '__main__':
bT.root.mainloop()
PS如果你运行这个片段,你会明白我的意思,以防我没有清楚地描述这个问题。塔...