呼唤单选按钮文本python

时间:2014-07-11 07:14:37

标签: python

我想叫出选项的名称,比如"你选择了G"但似乎有错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
    return self.func(*args)
  File "C:\Users\Xavier_\Downloads\gui.py.py", line 21, in sel
    selection = "You have selected " + str(text.get())
NameError: global name 'text' is not defined


def sel():
   selection = "You have selected " + str(text.get())
   label.config(text = selection)


var = IntVar()
R1 = Radiobutton(root, text="G", variable=var, value=1,
                  command=sel)

1 个答案:

答案 0 :(得分:0)

代码中的

text不是变量,它是Radiobutton()的一个选项,你得到的值是你的var变量,它是一个Int,而不是一个文本。 / p>

我想你想做这样的事情:

def sel():
   selection = "You have selected " + str(v.get())
   label.config(text= selection)


v = StringVar()
R1 = Radiobutton(root, text="G", variable=v, value="G",
                  command=sel)

我已经更改了var名称,因为我不喜欢使用它,它在一些语言中保留。