python - 如何摧毁这个。 Python 知识库

标签 python python-3.x tkinter

我有关于这个的问题。 我是 Python tk 的新学习者。通常它使用

class myApp :
def __init__(self, gui,) :
self.root = gui

然后通过

销毁它
self.root.destroy()

但是我得到了这个代码,我怎么能破坏这个窗口

from travelToolbox import readItems, readTransactions
import tkinter as tk

class myApp(tk.Tk): 
    def __init__(self, itemRecords) :
        tk.Tk.__init__(self)
        self.itemRecords = itemRecords
        self.grid()
        self.entry = tk.Entry(self)
        self.entry.grid(column=1,row=0)
        button1 = tk.Button(self,text="Show",command=self.showStockItem)
        button1.grid(column=2,row=0)
        button2 = tk.Button(self,text="Quit",command=self.quitit)
        button2.grid(column=1,row=1)
        label = tk.Label(self, anchor="w", text='Item ID')
        label.grid(column=0,row=0)

我尝试销毁,但我回来了

AttributeError: '_tkinter.tkapp' object has no attribute 'root'

请帮我解决这个问题。太感谢了。

最佳答案

由于 MyApp 继承自 tk.Tk,因此您可以对 self 调用 destroy()
也许是这样的:

import tkinter as tk


class MyApp(tk.Tk):

    def __init__(self):
        super().__init__()
        tk.Button(self, text='quit', command=self.quit).pack()
        self.mainloop()

    def quit(self):
        self.destroy()


if __name__ == '__main__':

    MyApp()

关于python - 如何摧毁这个。 Python 知识库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49962476/

相关文章:

python - pybind11 - 识别并删除 C++ 包装器中的内存泄漏

python - 如何从整数列表中提取单个数字?

python-3.x - 如何在 Python 中创建和导入自定义模块

Python3 & Tkinter,list.sort 由 StringVar 组成的对象列表

python - 停止 Tkinter 菜单自动运行命令

Python - tkinter 小部件数组随着单选按钮的点击而变化

python - 列出 Django 中的所有静态 Assets

python - 如何使用 Tensorflow 数据集进行 CNN 模型训练

python - pip install wheel 随机失败

python - 尝试创建位图 : _tkinter. TclError: 位图 "pyimage2"未定义时