关闭tkinter窗口并使用按钮编程

时间:2019-06-28 18:46:00

标签: python tkinter

我正在构建一个GUI,我想有一个关闭窗口并退出程序的按钮。

我知道.destroy应该会关闭窗口。当我执行此代码时,程序将挂起,并且窗口已打开,但不起作用(旋转光标死亡)。

from tkinter import *
from tkinter import ttk
from tkinter import filedialog

class Root(Tk):
    def __init__(self):
        super(Root, self).__init__()
        self.title("Title")
        self.minsize(640, 400)
        self.closeFrame = ttk.LabelFrame(self, text = "Close Program")
        self.closeFrame.grid(column = 2, row = 1, padx = 20, pady = 20)        

        self.buttonclose()

    def buttonclose(self):
        self.buttonclose = ttk.Button(self.closeFrame, text = "Click to Close",command = self.destroy)
        self.buttonclose.grid(column = 1, row = 1)


root = Root()
root.mainloop()

我必须用上面的代码强制退出窗口。

如果我在exit()的末尾添加buttonclose(self),则窗口关闭,但是我得到以下警告:

ERROR:root:Invalid alias: The name clear can't be aliased because it is another magic command.
ERROR:root:Invalid alias: The name more can't be aliased because it is another magic command.
ERROR:root:Invalid alias: The name less can't be aliased because it is another magic command.
ERROR:root:Invalid alias: The name man can't be aliased because it is another magic command.

我想要的只是一个关闭窗口并停止程序的按钮。那应该很简单,对吧?

0 个答案:

没有答案