2016-08-25 5 views
0

TK 버튼을 구성 할 때 Tkinter의 오류 속성 내가 로그인 창을 구축하기 시작했을 때, 나는 속성 오류 uppon 비틀 거렸다.내가 SMTP 메일 보낸 사람을 짓고 있어요

이 예에서 첫 번째 기능은 루트이고 두 번째 기능은 문제를 일으키는 기능입니다.

class smtp_gui(tk.Tk): 
    is_logged_in = False 
    user_email = '' 
    user_passwd = '' 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     self.wm_title("SMTP Mail Sender") 
     self.resizable(False, False) 
     self.iconbitmap('at.ico') 
     self.container = tk.Frame(self) 
     self.btn_container = tk.Frame(self) 
     self.bind('<Control-s>', self.send) 
     self.bind('<Control-h>', self.get_help) 

     self.container.pack(side="top", padx=1, pady=1) 
     self.container.columnconfigure(1, weight=1) 
     self.btn_container.pack(side="bottom", padx=1, pady=1, fill='x') 

     tk.Label(self.container, text='From:').grid(row=0, column=0, sticky='e') 
     tk.Label(self.container, text='To:').grid(row=1, column=0, sticky='e') 
     tk.Label(self.container, text='Subject:').grid(row=2, column=0, sticky='e') 

     self.refresh() 

     self.To = tk.Entry(self.container) 
     self.To.grid(row=1, column=1, sticky="we", pady=3, padx=2) 

     self.Subject = tk.Entry(self.container) 
     self.Subject.grid(row=2, column=1, sticky="we", pady=2, padx=2) 

     self.Msg = tk.Text(self.container, width=40, height=5) 
     self.Msg.grid(columnspan=2, padx=3, pady=3) 

     send_btn = tk.Button(self.btn_container, text="Send", command=self.send) 
     send_btn.pack(side='right', padx=2, pady=1) 

     quit_btn = tk.Button(self.btn_container, text="Quit", command=self.quit) 
     quit_btn.pack(side='right', padx=2, pady=1) 

    def send(self, event=None): 
     print(self.Msg.get("0.0", "end")) 

    def refresh(self): 
     if self.logged_in(): 
      try: 
       self.login_btn.destroy() 
      except AttributeError: 
       pass 
      self.mailabel = tk.Label(self.container, bd=1, text=smtp_gui.user_email, relief='sunken') 
      self.mailabel.grid(row=0, column=1, pady=3, padx=2, sticky='we') 
     else: 
      try: 
       self.mailabel.destroy() 
      except AttributeError: 
       pass 
      self.login_btn = tk.Button(self.container, text="login before sending", command=self.get_login) 
      self.login_btn.grid(row=0, column=1, sticky="we", pady=3, padx=2) 

    def logged_in(self): 
     return smtp_gui.is_logged_in 

    def get_login(self): 
     login = LoginWindow() 
     self.refresh() 

    def get_help(self, event=None): 
     get_help = HelpWindow() 


class LoginWindow(tk.Toplevel): 

    def __init__(self, *args, **kwargs): 
     # Window config 
     tk.Toplevel.__init__(self, *args, **kwargs) 
     self.grab_set() 
     self.wm_title("Email login") 
     self.resizable(False, False) 
     self.iconbitmap('login.ico') 
     # Containers 
     self.container = tk.Frame(self) 
     self.btn_container = tk.Frame(self) 

     self.container.pack(padx=2, pady=2) 
     self.btn_container.pack(side="bottom", padx=1, pady=1, fill='x') 

     # Tracers 

     self.tracetest = tk.StringVar() 
     self.tracetest.trace('w', self.tracer) 

     # Window elements 
     tk.Label(self.container, text="Gmail address:").grid(row=0, column=0) 
     tk.Label(self.container, text="Password:").grid(row=1, column=0) 

     # Entries 
     self.Address = tk.Entry(self.container, width=44, textvariable=self.tracetest) 
     self.Address.grid(row=0, column=1, sticky="we", pady=3, padx=2) 
     self.Address.insert(0, "your address") 

     self.Passwd = tk.Entry(self.container, show="*", textvariable=None) 
     self.Passwd.grid(row=1, column=1, sticky="we", pady=3, padx=2) 

     # Buttons 
     self.login_btn = tk.Button(self.btn_container, text="Login", command=self.get_credentials) 
     self.login_btn.pack(side='right', padx=2, pady=3) 

     storno_btn = tk.Button(self.btn_container, text="Storno", command=self.destroy) 
     storno_btn.pack(side='right', padx=2, pady=3) 

    def get_credentials(self): 
     user_address = self.Address.get() 
     try: 
      assert(match(r'[a-zA-Z0-9][email protected]', user_address)) 
     except AssertionError: 
     messagebox.showwarning("Invalid login", "This is not a valid gmail  address!") 

    def tracer(self, *args): 
     address = match(r'[a-zA-Z0-9][email protected]', self.Address.get()) 
     passwd = '' 
     if passwd and address: 
      self.login_btn.config(state='enabled') # Attribute error here, the program says these buttons don't exist in the functions even tough I defined them above 
     else: 
      self.login_btn.config(state='disabled') 

문제가있는 기능은 매우,이 코드를 사용하여 문제를 재현 할 수 있어야합니다 (추적 기능에서)을 종료

나 '(단지 TK 및 정규식으로 Tkinter를 가져올 수) 에있다 어떤 종류의 참조 문제가 있다고 생각합니다.

EDIT - 풀 추적

Traceback (most recent call last): 
    File "C:\Users\david\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__ 
    return self.func(*args) 
    File "C:\Users\david\Documents\Git_repositories\smtp_client\main_menu.py", line 129, in tracer 
    self.login_btn.config(state='disabled') 
AttributeError: 'LoginWindow' object has no attribute 'login_btn' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\david\Documents\Git_repositories\smtp_client\main_menu.py", line 160, in <module> 
    root.mainloop() 
    File "C:\Users\david\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1131, in mainloop 
    self.tk.mainloop(n) 
    File "C:\Users\david\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1554, in __call__ 
    self.widget._report_exception() 
AttributeError: 'StringVar' object has no attribute '_report_exception' 

는 I 버튼 개시 후 트레이서 이동할 때 추적이 버튼의 생성 이전에 호출 된 때문에 추적이 발생

용액을 FOUND , 그것은 완벽하게 일했습니다, 고마워요!

+0

오류의 전체 추적을 게시하십시오. 또한 'enabled'는 유효한 상태가 아닙니다. '정상'이어야합니다. – FamousJameous

+0

추적을 추가했습니다. –

+0

이전에는 가변 추적 프로그램을 사용한 적이 없습니다. 변수의 초기 생성시 추적 콜백이 호출됩니까? – FamousJameous

답변

2

값이 변경되면 추적이 실행됩니다. 해당 위젯이 변수에 연결되어 있기 때문에 self.address.insert(0, "your address")을 수행하면 값이 변경됩니다. 이 모든 것은 self.login_btn을 정의하기 전에 발생합니다.

추적을 활성화하기 전에 또는 추적 된 변수의 값을 변경하기 전에 단추를 정의해야합니다.

+0

Exaclty 이봐, 내 결론과 똑같은 결론에 도달했습니다. 수정 된 게시물 : –

관련 문제