PyCharm:StringVar的未解析参考

时间:2018-06-30 11:03:45

标签: python tkinter tkinter-entry

Python 3.6

我正在使用tkinter对游戏Mastermind进行编程。但是,由于在函数number1中对我的Check的引用未解决,所以我很努力。

def Check(event):
    number1 = number1.get()
    number2 = str(number1)

def Enter(event):
    number1 = StringVar()
    entry_1 = Entry(window, textvariable = number1)
    entry_1.grid(row=8, column=1, padx=10, pady=10)
    entry_1.focus()
    entry_1.bind("<Return>", Check)

这是我的错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/Colbach/Google Drive/Charlotte/Informatique/Projet/Mastermind.py", line 39, in Check
    number1 = number1.get()
UnboundLocalError: local variable 'number1' referenced before assignment

1 个答案:

答案 0 :(得分:0)

我认为您可能混淆了两个变量名:

number1 = number1.get()可能应该是:number1 = entry1.get()