[英]AttributeError: 'NoneType' object has no attribute 'children' in __init__.py destroy
I just started out on tkinter and stumbeled over a problem I could not solve by my own: 我刚开始使用tkinter时遇到了自己无法解决的问题:
I wanted to write a widget myself, that I call pocket. 我想自己写一个小部件,称为口袋。 Which effectivly is part in my window that can be slid open or hidden.
实际上,这是我窗口中可以滑动打开或隐藏的部分。
class pocket(tk.Frame):
def __init__(self, parent, master=None, expand_Bool=True):
tk.Frame.__init__(self, parent)
self.master = master
self.Content = tk.Frame(self)
self.Button_on = tk.Button(self,text='Open',command=lambda: self.expand())
self.Button_hide = tk.Button(self,text='Hide',command=lambda: self.hide())
self.Content.grid(row=1,column=0)
self.Button_on.grid(row=0,column=0)
self.Button_hide.grid(row=0,column=0)
if expand_Bool:
self.expand()
else:
self.hide()
def expand(self):
self.Button_on.grid_remove()
self.Button_hide.grid()
self.Content.grid()
def hide(self):
self.Button_on.grid()
self.Button_hide.grid_remove()
self.Content.grid_remove()
def get_contentframe(self):
return self.Content
if __name__=='__main__':
root=tk.Tk()
pocket1 =pocket(root)
pocket1.grid(row=0,column=0)
label1=tk.Label(pocket1.get_contentframe(),text='Text')
label1.grid(row=1,column=1)
root.mainloop()
The "widget" itself works quiet fine. “小部件”本身运行正常。 but when I try to close the window I get an error message:
但是当我尝试关闭窗口时,出现错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\...\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\...\tkinter\__init__.py", line 2055, in destroy
for c in list(self.children.values()): c.destroy()
File "C:\...\tkinter\__init__.py", line 2300, in destroy
if self._name in self.master.children:
AttributeError: 'NoneType' object has no attribute 'children'
How can I evoid that error ? 如何避免该错误? or is there a even better way to do this?
还是有更好的方法来做到这一点?
Thanks in advance. 提前致谢。
Greetings 问候
Problem is 问题是
self.master = master
Frankly, when you execute 坦白说,当您执行
tk.Frame.__init__(self, parent)
then it automatically creates variable self.master
and assigns parent
. 然后它会自动创建变量
self.master
并分配parent
。
self.master = parent
You don't have to do it. 您不必这样做。
In your code master
is None
so finally you get 在您的代码
master
为None
所以最终您得到
self.master = None
so it loose access to parent
and it has problem when it tries to close children. 因此它失去了与
parent
并且在尝试关闭孩子时遇到了问题。
Full code 完整代码
import tkinter as tk
class Pocket(tk.Frame):
def __init__(self, master=None, expand_bool=True):
tk.Frame.__init__(self, master)
self.content = tk.Frame(self)
self.button_on = tk.Button(self, text='Open', command=self.expand)
self.button_hide = tk.Button(self, text='Hide', command=self.hide)
self.content.grid(row=1, column=0)
self.button_on.grid(row=0, column=0)
self.button_hide.grid(row=0, column=0)
if expand_bool:
self.expand()
else:
self.hide()
def expand(self):
self.button_on.grid_remove()
self.button_hide.grid()
self.content.grid()
def hide(self):
self.button_on.grid()
self.button_hide.grid_remove()
self.content.grid_remove()
def get_contentframe(self):
return self.content
if __name__=='__main__':
root = tk.Tk()
pocket1 = Pocket(root)
pocket1.grid(row=0, column=0)
label1 = tk.Label(pocket1.get_contentframe(), text='Text')
label1.grid(row=1, column=1)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.