Python Tkinter:“str”对象在尝试关闭时没有属性“children”

Python Tkinter:“str”对象在尝试关闭时没有属性“children”,python,tkinter,frames,Python,Tkinter,Frames,我正在尝试学习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="1

我正在尝试学习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'

处理此问题的一种方法是使用操作绑定密钥:

def quit(event):
    print "you pressed control c"
    root.quit()

root = tk.Tk()
root.bind('<Control-c>', quit)
root.mainloop()

问题是您传递了一个字符串作为主参数的值。该参数必须是小部件,而不是字符串

mainframe1=marco(master=root)
还应将该参数传递给_init__方法:

严格地说,这个特定的代码不需要它,因为主窗口的默认值是根窗口。但是,如果要创建Frame的子类,则应始终在构造函数中包含master,以便小部件可以在根窗口以外的位置使用

试试镜框,主人?
mainframe1=marco(master=root)
Frame.__init__(self, master)