-2
之前引用時我的腳本執行except
塊,我得到一個錯誤:UnboundLocalError:局部變量「用戶名」分配
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
return self.func(*args)
File "/home/cali/PycharmProjects/untitled1/test2.py", line 68, in create_user
self.status_label.config(text='Failed to add user %s' % username)
UnboundLocalError: local variable 'username' referenced before assignment
在我看來,這username
是本地唯一的try塊。
def create_user(self):
try:
username = self.add_user(self.username_entry2.get(), self.password_entry2.get())
self.status_label.config(text='Added user %s' % username)
except Exception:
self.status_label.config(text='Failed to add user %s' % username)
我該如何解決這個問題?
如果未指定'except'塊,則不能使用'username'。 – erip
如果'self.add_user()'或兩個'.get()'調用中的任何一個調用引發異常,'username'沒有設置。 –