【问题标题】:Function to close the window in Tkinter在 Tkinter 中关闭窗口的函数
【发布时间】:2020-11-23 20:23:08
【问题描述】:
import tkinter


class App():
   def __init__(self):
       self.root = Tkinter.Tk()
       button = Tkinter.Button(self.root, text = 'root quit', command=self.quit)
       button.pack()
       self.root.mainloop()

   def quit(self):
       self.root.destroy 

app = App()

如何让我的quit 函数关闭窗口?

【问题讨论】:

    标签: python tkinter


    【解决方案1】:
    def quit(self):
        self.root.destroy()
    

    destroy后面加括号,调用该方法。

    当您使用command=self.root.destroy 时,您将方法传递给Tkinter.Button 没有括号,因为您希望Tkinter.Button 存储该方法以供将来调用,而不是在按钮时立即调用它已创建。

    但是当你定义quit方法时,你需要在方法体中调用self.root.destroy(),因为此时方法已经被调用了。

    【讨论】:

    • +1 括号解释,正是我想要的!
    【解决方案2】:
    class App():
        def __init__(self):
            self.root = Tkinter.Tk()
            button = Tkinter.Button(self.root, text = 'root quit', command=self.quit)
            button.pack()
            self.root.mainloop()
    
        def quit(self):
            self.root.destroy()
    
    app = App()
    

    【讨论】:

      【解决方案3】:
      def exit(self):
          self.frame.destroy()
      exit_btn=Button(self.frame,text='Exit',command=self.exit,activebackground='grey',activeforeground='#AB78F1',bg='#58F0AB',highlightcolor='red',padx='10px',pady='3px')
      exit_btn.place(relx=0.45,rely=0.35)
      

      这对我来说可以在单击退出按钮时破坏我的 Tkinter 框架。

      【讨论】:

        【解决方案4】:
        class App():
            def __init__(self):
                self.root = Tkinter.Tk()
                button = Tkinter.Button(self.root, text = 'root quit', command=self.quit)
                button.pack()
                self.root.mainloop()
        
            def quit(self):
                self.root.destroy()
        
        app = App()
        

        【讨论】:

        • 欢迎来到 StackOverflow。虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请编辑您的答案以添加解释并说明适用的限制和假设。
        猜你喜欢
        • 2017-09-04
        • 2016-09-23
        • 1970-01-01
        • 1970-01-01
        • 2023-02-26
        • 2021-06-05
        • 1970-01-01
        • 2010-09-11
        • 2019-09-27
        相关资源
        最近更新 更多