Python 属性错误:';非类型';对象没有属性';儿童';在uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
我刚从tkinter开始,就被一个我自己解决不了的问题绊倒了:Python 属性错误:';非类型';对象没有属性';儿童';在uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,python,tkinter,parent,children,attributeerror,Python,Tkinter,Parent,Children,Attributeerror,我刚从tkinter开始,就被一个我自己解决不了的问题绊倒了: 我想自己写一个小部件,我称之为pocket。在我的窗口中,哪一部分是有效的,可以滑动打开或隐藏 class pocket(tk.Frame): def __init__(self, parent, master=None, expand_Bool=True): tk.Frame.__init__(self, parent) self.master = master self.C
我想自己写一个小部件,我称之为pocket。在我的窗口中,哪一部分是有效的,可以滑动打开或隐藏
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()
“小部件”本身工作得很好。但当我试图关闭窗口时,会收到一条错误消息:
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'
我怎样才能避免那个错误呢?或者有更好的方法吗?提前感谢。
问候语问题是
self.master = master
坦白说,当你执行
tk.Frame.__init__(self, parent)
然后它自动创建变量self.master
,并分配parent
self.master = parent
你不必这么做
在你的代码中,
master
是None
,所以最后你得到了
self.master = None
因此,它失去了对父项的访问权限,并且在尝试关闭子项时出现问题
完整代码
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()
问题是
self.master = master
坦白说,当你执行
tk.Frame.__init__(self, parent)
然后它自动创建变量self.master
,并分配parent
self.master = parent
你不必这么做
在你的代码中,master
是None
,所以最后你得到了
self.master = None
因此,它失去了对父项的访问权限,并且在尝试关闭子项时出现问题
完整代码
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()
您是否使用tkinter.after()
或threads
?您可能必须在关闭程序之前停止它们。BWT:相反,command=lambda:self.expand()
您可以执行command=self.expand
(没有lambda
和()
)顺便说一句,self.master
确实是无的,那么,为什么您希望它有一个名为children
的属性呢?您是使用tkinter.after()
还是threads
?您可能必须在关闭程序之前停止它们。BWT:相反,command=lambda:self.expand()
您可以执行command=self.expand
(没有lambda
和()
)顺便说一句,self.master
确实是None
,那么您为什么希望它有一个名为children的属性呢?