为什么"退出"按钮停止工作"否"按钮是否被点击?

时间:2015-09-14 18:50:38

标签: python python-2.7 tkinter destroy toplevel

我有一个" Quit"创建Toplevel窗口的按钮,询问用户是否确定要退出。如果"否"单击按钮,窗口被破坏,但是"退出"按钮不再有效。呃," quitter"虚拟事件击键(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()

P.S。如果您运行此代码段,您将看到我的意思,以防我没有清楚地描述该问题。 Ta ...

1 个答案:

答案 0 :(得分: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()

嗯,这本身并不是一个真正的程序,而是从一个程序中提取的一些“按钮无效”问题。干杯!