【问题标题】:Why Does the "Quit" Button Stop Working After the "No" Button is Clicked?为什么单击“否”按钮后“退出”按钮停止工作?
【发布时间】:2015-12-10 20:54:13
【问题描述】:

我有一个“退出”按钮,它创建一个顶层窗口来询问用户是否确定要退出。如果单击“否”按钮,则窗口被破坏,但“退出”按钮不再起作用。呃,“退出”虚拟事件按键(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()

附:如果你运行这个 sn-p,你会明白我的意思,以防我没有清楚地描述这个问题。塔...

【问题讨论】:

  • 这个问题太罗嗦了,考虑到实际问题归结为“我有一个按钮连接到一个破坏窗口的函数,但它不会破坏窗口。为什么会这样?” .
  • 你说得对,奥克利先生。过失!以后我会更简洁。
  • 我拼错了我的实例变量名称(byWindow,而不是 byeWindow),这导致我的 if 条件为 True,导致返回,这意味着顶层窗口没有被创建。纠正拼写错误,解决问题。感谢 Bryan Oakley,感谢您提供有见地的提示和 cmets。我会努力“付出”!
  • 在给出了几个答案后,您完全改变了您的问题。这让来这里寻求答案的人非常困惑。
  • 嗯,你说的太罗嗦了,我就“简述”一下吧!我只是应该忽略你的断言吗?

标签: python python-2.7 tkinter destroy toplevel


【解决方案1】:

事实证明,归根结底,我的问题是一个实例变量的拼写错误:“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()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-20
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多