预成型x=x+;使用def add()在Tkinter中输入1:

预成型x=x+;使用def add()在Tkinter中输入1:,tkinter,Tkinter,我每次尝试运行此程序时都会出错 from Tkinter import * def returnCodeno(): print 'no' choice = choice + 1 textEnter('no') root = Tk() choice = 0 Yes = Button(root,text = 'Yes',width = 15, command = returnCodeno) Yes.pack() root.mainloop() 每次尝试单击“是”时都会出现此

我每次尝试运行此程序时都会出错

from Tkinter import *
def returnCodeno():
    print 'no'
    choice = choice + 1
    textEnter('no')
root = Tk()
choice = 0
Yes = Button(root,text = 'Yes',width = 15, command = returnCodeno)
Yes.pack()
root.mainloop()
每次尝试单击“是”时都会出现此错误

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Users\Stephen Tafoya\Desktop\Stanley parable text based\stanley2.py", line 6, in returnCodeno
choice = choice + 1
UnboundLocalError: local variable 'choice' referenced before assignment

非常感谢您的帮助。

我想您希望访问
returnCodeno
中的模块级变量
choice
,而不是创建一个新的本地变量,因此您必须使用
global
语句

示例:

from Tkinter import *
def returnCodeno():
    global choice
    choice += 1
    print choice

root = Tk()
choice = 0
Yes = Button(root,text = 'Yes',width = 15, command = returnCodeno)
Yes.pack()
root.mainloop()

我猜您希望访问
returnCodeno
中的模块级变量
choice
,而不是创建一个新的本地变量,因此必须使用
global
语句

示例:

from Tkinter import *
def returnCodeno():
    global choice
    choice += 1
    print choice

root = Tk()
choice = 0
Yes = Button(root,text = 'Yes',width = 15, command = returnCodeno)
Yes.pack()
root.mainloop()

你到底想达到什么目的?问题标题不同,内容也不同……如果您确实想做出
选择
全局选择,请在
添加
方法中使用
全局选择
。另外,
textEnter
应该做什么?你到底想实现什么?问题标题不同,内容也不同……如果您确实想做出
选择
全局选择,请在
添加
方法中使用
全局选择
。另外,
textEnter
应该做什么?