Python 如何获取所选单选按钮的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29736559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the value of the selected radio button?
提问by Zoxx
I would like to create 2 different groups of radio buttons. The user would select one option from either group. There would be a function that would get the values(strings) from the selected radio buttons and then print them. Here's my code but it doesn't work (i'm new to python).
我想创建 2 组不同的单选按钮。用户将从任一组中选择一个选项。会有一个函数可以从选定的单选按钮中获取值(字符串),然后打印它们。这是我的代码,但它不起作用(我是 Python 新手)。
from tkinter import *
root = Tk()
btn1 = "lol"
btn2 = "lel"
def funkcija():
n = entry1.get()
m = "null"
X = btn1.get()
Y = btn2.get()
print("%s %s je %s %s." % (n, X, m, Y))
theLabel = Label(root, text="Vnesite koli?ino in izberite prvo valuto.")
theLabel.grid(row=0, columnspan=3)
gumb1=Radiobutton(root,text="Euro",value = "euro",variable = "btn1").grid(row=2, column=1, sticky=W)
gumb2=Radiobutton(root,text="Dolar",value = "dolar",variable = "btn1").grid(row=3, column=1, sticky=W)
gumb3=Radiobutton(root,text="Funt",value = "funt",variable = "btn1").grid(row=4, column=1, sticky=W)
label3= Label(root, text="Izberite drugo valuto.")
label3.grid(row=6, columnspan=3)
label35= Label(root)
label35.grid(row=5, columnspan=3)
gumb4=Radiobutton(root,text="Euro",value = "euro",variable = "btn2").grid(row=7, column=1, sticky=W)
gumb5=Radiobutton(root,text="Dolar",value = "dolar",variable = "btn2").grid(row=8, column=1, sticky=W)
gumb6=Radiobutton(root,text="Funt",value = "funt",variable = "btn2").grid(row=9, column=1, sticky=W)
label1 = Label(root, text="Koli?ina:")
label1.grid(row=1, sticky=E)
entry1 = Entry(root)
entry1.grid(row=1, column=1, sticky=W)
go = Button(root, text="Izra?un", fg="white", bg="black", command=funkcija)
go.grid(row=10, columnspan=3)
root.mainloop()
采纳答案by Zizouz212
In your radio button, analyze the parameters that you are passing:
在您的单选按钮中,分析您传递的参数:
gumb1 = Radiobutton(root,
text = "Euro",
value = "Euro",
variable = "btn2"
The parameters value
and variable
are what stores the data of the radio button. You've set your value
option correctly. The interpreter will automatically set the variable
with the value
when the radio button is selected.
参数value
和variable
是存储单选按钮数据的内容。您已value
正确设置选项。解释器将自动设置variable
与value
被选择的单选按钮时。
But here's where your issue is:
但这就是您的问题所在:
variable = "btn2"
"btn2"
is a string. Not very useful though, is it? In fact, you're trying to perform methods on it that don't even exist. Such as here:
"btn2"
是一个字符串。不过不是很有用,是吗?事实上,您正在尝试对其执行甚至不存在的方法。比如这里:
def funkcija():
X = btn2.get()
In fact, taking this information, you almost got there!
事实上,有了这些信息,你就快到了!
At the top of your script, you need to set btn2
to Tkinter's StringVar
, like so:
在脚本的顶部,您需要设置btn2
为 Tkinter's StringVar
,如下所示:
from tkinter import *
btn1 = StringVar()
btn2 = StringVar()
Now that's done, let's change our parameters in our radio buttons.
现在完成了,让我们更改单选按钮中的参数。
gumb1 = Radiobutton(root,
text = "Euro",
value = "Euro",
variable = btn2
Now, Tkinter will automatically update the variable when it is selected. To get the value, do the same that you had done in your funkcija
.
现在,Tkinter 将在选择时自动更新变量。要获得该值,请执行您在funkcija
.
X = btn2.get()
And then the value of btn2 (which was updated by the radio buttons) will not be read, and stored into the variable X
.
然后 btn2 的值(由单选按钮更新)将不会被读取,并存储到变量中X
。