在 Tkinter 中关闭窗口的函数 [英] Function to close the window in Tkinter

查看:119
本文介绍了在 Tkinter 中关闭窗口的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 函数关闭窗口?

How can I make my quit function to close the window?

推荐答案

def quit(self):
    self.root.destroy()

destroy后加上括号来调用方法.

Add parentheses after destroy to call the method.

当您使用 command=self.root.destroy 时,您将方法传递给 Tkinter.Button 没有 括号,因为您想要 Tkinter.Button 存储方法供以后调用,不要在按钮创建时立即调用.

When you use command=self.root.destroy you pass the method to Tkinter.Button without the parentheses because you want Tkinter.Button to store the method for future calling, not to call it immediately when the button is created.

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

But when you define the quit method, you need to call self.root.destroy() in the body of the method because by then the method has been called.

这篇关于在 Tkinter 中关闭窗口的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆