1

我有一个“退出”按钮,它创建一个顶层窗口来询问用户是否确定要退出。如果单击“否”按钮,则窗口被破坏,但“退出”按钮不再起作用。呃,“退出”虚拟事件按键(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如果你运行这个片段,你会明白我的意思,以防我没有清楚地描述这个问题。塔...

4

1 回答 1

0

事实证明,归根结底,我的问题是一个实例变量的拼写错误:“byWindow”而不是“byeWindow”,它阻塞了我的 self.bye_bye() 函数中的 if 语句。更正拼写解决了问题。非常感谢 Bryan Oakley 的深刻见解和明智的建议。这是完美运行的程序...

from Tkinter import *

class ButtonTestClass(Frame):
    def __init__(self):

        self.root=Tk()
        self.root.title("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='Wanna see a "Quit" button working flawlessly?', 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

    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=self.bye_bye
        )
        self.quitBtn.place(
            x=pbx, 
            y=pby, 
            width=pbw, 
            height=pbh
        )
        pbx += pbw + pbp

    def dontQuit(self, tlref):
        tlref.destroy()
        self.byeWindow = None

    def bye_bye(self):
        if self.byeWindow is not None:
            return
        self.byeWindow=Toplevel()
        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()

好吧,它本身并不是一个真正的程序,而是从一个存在“按钮不起作用”问题的程序中提取的几行代码。干杯!

于 2015-09-15T08:27:45.833 回答