python - Tkinter:“str”对象在尝试关闭时没有属性“children”
问题描述
我正在尝试在 python 3.7 中学习 tkinter gui,我有以下代码:
from tkinter import *
# Configuración de la ventana principal
root=Tk()
root.title("Cath Config")
#Definición de clases
#Frames
class marco(Frame):
def __init__(self, master=None, color="#F3F3F3", ancho="1024", alto="680", borde="5", tipoborde="groove"):
Frame.__init__(self)
self.master=master
self.config(bg=color,width=ancho,height=alto,bd=borde,relief=tipoborde)
self.pack()
#Configuración del widget frame
mainframe1=marco(master="root")
#Ejecución de la ventana principal
root.mainloop()
问题是代码“有效”,当我运行该代码时,它显示主框架的根没有问题,但是当我尝试关闭根时,它不会关闭并抛出此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\konoe\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\konoe\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2061, in destroy
for c in list(self.children.values()): c.destroy()
File "C:\Users\konoe\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2306, in destroy
if self._name in self.master.children:
AttributeError: 'str' object has no attribute 'children'
解决方案
问题是您传递了一个字符串作为master
参数的值。该参数必须是一个小部件,而不是一个字符串。
mainframe1=marco(master=root)
您还应该将该参数传递给该__init__
方法:
Frame.__init__(self, master)
严格来说,这个特定的代码没有必要,因为主窗口的默认值是根窗口。但是,如果您要创建一个子类,Frame
则应始终在构造函数中包含主类,以便小部件可以在根窗口以外的地方使用。
推荐阅读
- java - 如何执行退出程序的输入循环?
- jestjs - settimeout 内的代码没有被笑话所覆盖
- webdatarocks - 单击字段工具栏中的应用后,Webdatarocks 内容消失,然后在字段禁用中应用按钮
- javascript - 使用 Vanilla JavaScript 从输入文本中获取更新的值
- html - 如何在 MacOS 和 iOS 上使用下载的字体?
- string - 参数类型“TabBar”不能分配给参数类型“String”
- python - aiohttp session.post() 发送一个名称不可读的文件
- java - 在 Hibernate 中将自定义对象属性存储为列
- docker - Docker 容器不同步卷
- ios - BLE - swift 中前面字节的总和