python - tkinter 退出崩溃

标签 python tkinter

我对 Tkinter 很陌生。我在 Tkinter 中制作了这个类似“Hello World”的 GUI 程序。但是,每次我单击退出按钮时,程序都会崩溃。提前致谢!

from Tkinter import *
import sys
class Application(Frame):

def __init__(self,master=None):

    Frame.__init__(self,master=None)
    self.grid()
    self.createWidgets()

def createWidgets(self):
    self.quitButton = Button(text='Quit',command=self.quit)#Problem here
    self.quitButton.grid()
app = Application()
app.master.title("Sample application")
app.mainloop()

最佳答案

在 Tkinter 中,根元素是一个 Tk 对象。 Application 应该是 Tk 的子类,而不是 Frame:

from Tkinter import *
import sys

class Application(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.grid()
        self.createWidgets()
    def createWidgets(self):
        self.quitButton = Button(text='Quit',command=self.destroy) # Use destroy instead of quit
        self.quitButton.grid()

app = Application()
app.title("Sample application")
app.mainloop()

关于python - tkinter 退出崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15323546/

相关文章:

python - 解析制表符分隔的文件

python - 从字典键的子集创建命名元组

python 检查列表中是否存在值

python - NameError:名称 'Frame' 未定义(Python)

python - 如何从两个数据帧创建所有列组合的子图

python - 当我一开始就没有使用 xrange 时,为什么没有定义 xrange?

python - Tkinter:按钮打开另一个窗口(并关闭当前窗口)

Python/tKinter/ttk - 单击按钮时传递对象

android - Tkinter 到 android 的翻译

python - 使用 Python 3 在 Mac 上没有名为 '_tkinter' 的模块