2017-11-28 7 views
0

방금 ​​tkinter를 시작했고 스스로 해결할 수없는 문제를 해결했습니다.
위젯을 직접 작성하고 싶습니다. 나는 주머니라고 부른다. 어느 쪽이 효과적으로 열리거나 숨길 수있는 나의 창에 속한다.AttributeError : 'NoneType'객체에 __init__.py의 'children'속성이 없습니다.

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' 

어떻게 그 오류를 피할 수 있습니까? 아니면 이것을하는 더 좋은 방법이 있습니까?
미리 감사드립니다. 만약

tk.Frame.__init__(self, parent) 

후 자동 self.master 변수를 생성하고 할당 parent 실행할 때
인사

+0

당신이'tkinter.after()'또는'threads'을 사용합니까? 프로그램을 종료하기 전에 프로그램을 중지해야 할 수도 있습니다. – furas

+0

BWT :'command = lambda : self.expand()'대신'lambda'와'()'없이'command = self.expand' 할 수 있습니다. – furas

+0

BTW : [PEP 8 스타일 가이드 ] (https://www.python.org/dev/peps/pep-0008/) – furas

답변

0

문제 솔직히

self.master = master 

이다.

self.master = parent 

당신은하지 않아도됩니다. 코드 master에서


그래서 결국 당신이 그렇게 느슨한 액세스 parent

self.master = None 

을 얻을하고 아이들을 종료하려고 할 때 문제가 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() 
관련 문제