[英]Can't get the value of a radio-button in Python/Tkinter
I'm writing a simple unit converter where the user can pick which units they want to convert from two options.我正在编写一个简单的单位转换器,用户可以在其中从两个选项中选择他们想要转换的单位。 I'm using radio-buttons for the choice, but can't seem to get the value of the chosen one to work in the conditions at the bottom of the program.我正在使用单选按钮进行选择,但似乎无法让所选按钮的值在程序底部的条件下工作。
I tried several solutions suggested here on stack overflow, but none of them worked.我尝试了这里关于堆栈溢出的几种解决方案,但都没有奏效。 At one point, I got the selected() to print the value of the button, but it still didn't work in the condition.有一次,我让 selected() 打印按钮的值,但在这种情况下它仍然不起作用。 Am I missing something obvious here?我在这里遗漏了一些明显的东西吗? Please, note, the converter is not finished yet, there is still some more polishing to do after this issue is solved.请注意,转换器尚未完成,在解决此问题后,还有更多工作要做。
from tkinter import *
window = Tk()
window.title("Unit converter")
window.minsize(width=300, height=300)
window.config(padx=50, pady=50)
def lbs_kgs():
user_input = float(unit_A1.get())
result = round((user_input / 2.2046), 2)
unit_B1.config(text= f"{result}")
def mil_km():
user_input = float(unit_A1.get())
result = round((user_input * 1.6), 2)
unit_B1.config(text= result)
def selected():
return radio_state.get()
intro_label = Label(text = "What units would you like to convert?")
intro_label.grid(column=0, row=0, columnspan=4, pady=10)
radio_state = StringVar()
radiobutton1 = Radiobutton(text="Pounds to kilograms", value="pk", variable=radio_state, command=selected)
radiobutton2 = Radiobutton(text="Miles to kilometers", value="mk", variable=radio_state, command=selected)
radiobutton1.grid(column=0, row=1, columnspan=4)
radiobutton2.grid(column=0, row=2, columnspan=4)
instructions_label = Label(text = "Enter the number:")
instructions_label.grid(column=0, row=3, columnspan=4, pady=10)
unit_A1 = Entry(width=5)
unit_A1.grid(column=1, row=4, sticky="e")
unit_A1_label = Label(text = "unit A1")
unit_A1_label.grid(column=2, row=4, sticky="w")
equal_label = Label(text = "is equal to")
equal_label.grid(column=1, row=5, sticky="e")
unit_B1 = Label(text = "0")
unit_B1.grid(column=2, row=5, sticky="w")
unit_B1_label = Label(text = "result unit")
unit_B1_label.grid(column=3, row=5, sticky="w")
button = Button(text="Calculate")
button.grid(column=0, row=6, columnspan=4, pady=10)
if selected() == "pk":
button.config(command=lbs_kgs)
elif selected() == "mk":
button.config(command=mil_km)
window.mainloop()
Move the if/else
check into the selected
function so the conditions can be checked each time the selection changes将if/else
检查移动到selected
的 function 中,以便每次选择更改时都可以检查条件
def selected():
selection = radio_state.get()
if selection == "pk":
button.config(command=lbs_kgs)
elif selection == "mk":
button.config(command=mil_km)
In line 29 should be radio_state = StringVar(window, '1')
.第 29 行应该是radio_state = StringVar(window, '1')
。 Without this when executed both radiobutton
are on, but that not right.如果没有这个,当执行时两个radiobutton
都打开,但那是不对的。
def selected():
if (selection := radio_state.get()) == "pk":
button.config(command=lbs_kgs)
elif selection == "mk":
button.config(command=mil_km)
Output: Output:
Output pound to Kilograms: Output 磅 自 公斤:
Output Miles to Kilometers: Output 英里为公里:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.