-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'没有设置。 –