Can't get the value of a radio-button in Python/Tkinter

Question:

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. 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()
Asked By: vitharia

||

Answers:

Move the if/else check into the selected function so the conditions can be checked each time the selection changes

def selected():
    selection = radio_state.get()
    if selection == "pk":
        button.config(command=lbs_kgs)
    elif selection == "mk":
        button.config(command=mil_km)
Answered By: JRiggles

In line 29 should be radio_state = StringVar(window, '1'). Without this when executed both radiobutton are on, but that not right.

It better to use Walrus Python 3.8.

def selected():
   if (selection := radio_state.get()) == "pk":
       button.config(command=lbs_kgs)
   elif selection == "mk":
        button.config(command=mil_km)

Output:

enter image description here

Output pound to Kilograms:

enter image description here

Output Miles to Kilometers:

enter image description here

Answered By: toyota Supra
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.