Home > OS >  Getting Radio Button's Input to a Variable [Tkinter]
Getting Radio Button's Input to a Variable [Tkinter]

Time:04-01

I have several radio buttons which have different values, and are tight to one variable. Even though the variable should take button's value when selected, it doesn't. Here is the code I'm using.

mimeType = StringVar()

wave = StringVar(value="audio/wav")
mp3 = StringVar(value='audio/mp3')
mp4 = StringVar(value='audio/mp4')
mp2 = StringVar(value='audio/mp2')
flac = StringVar(value='audio/flac')
m4a = StringVar(value='audio/m4a')

MP3 = Radiobutton(window, text = "MP3 Format", variable = mimeType,
                  height=5,
                  width = 20, value=mp3,bg = "#36DEE5", activebackground= "#36DEE5",
                 
MP4 = Radiobutton(window, text = "MP4 Format", variable = mimeType,
                  height=5,
                  width = 20,value=mp4,bg = "#36DEE5", activebackground= "#36DEE5")

MP2 = Radiobutton(window, text = "MP2 Format", variable = mimeType,
                  height=5,
                  width = 20,value=mp2,bg = "#36DEE5", activebackground= "#36DEE5")
WAV = Radiobutton(window, text = "WAV Format", variable = mimeType,
                  height=5,
                  width = 20, value=wave,bg = "#36DEE5", activebackground= "#36DEE5")
M4A = Radiobutton(window, text = "M4A Format", variable = mimeType,
                  height=5,
                  width = 20,value=m4a,bg = "#36DEE5", activebackground= "#36DEE5",
                 

print(mimeType.get()) # This doesn't return anything either

Special note: print(mimeType.get()) doesn't return anything!

So what I want is, the variable mimeType should take the value of Radio Buttons when selected.

CodePudding user response:

The value parameter doesn't take an IntVar() or a StringVar() object. You can directly assign a string or integer to it.

print(mimeType.get()) would then print the value stored in mimeType.

The second problem is that your print statement is executed even before the user can click on a radiobutton.

There are two solutions to the above problem.

  1. Create a button that displays the chosen value when the user clicks on the button.
  2. Use the command parameter to call a function (which displays the value chosen) when the user clicks on a radiobutton.

Sample code for the second solution:

MP4 = Radiobutton(window, text = "MP4 Format", variable = mimeType, height=5, width = 20,value = 'audio/mp4', bg = "#36DEE5", activebackground= "#36DEE5", command = lambda : print(mimeType.get()))

Add command = lambda : print(mimeType.get()) to all the radiobuttons to print out the value as soon as the user clicks on a radiobutton.

CodePudding user response:

Following is a runnable answer.

from tkinter import *

window = Tk()
mimeType = StringVar()  # is a string variable but in the original code mimeType was being set an object.
mimeType.set('Initialize')  # So the later print statement will print something

# original code value = was being set to objects.
wave = StringVar(value="audio/wav")  # These are objects no longer needed?
mp3 = StringVar(value='audio/mp3')  # These are objects no longer needed?
mp4 = StringVar(value='audio/mp4')  # These are objects no longer needed?
mp2 = StringVar(value='audio/mp2')  # These are objects no longer needed?
flac = StringVar(value='audio/flac')  # These are objects no longer needed?
m4a = StringVar(value='audio/m4a')  # These are objects no longer needed?


def display_current_mimetype():
    print(mimeType.get())


# Remove assignment of Radiobutton from original code unless the created
# objects are needed later.

# Modify code to value = string not value = object
Radiobutton(window, text="MP3 Format", variable=mimeType,
            height=5,
            width=20, value='audio/mp3', bg="#36DEE5",
            activebackground="#36DEE5", command=display_current_mimetype).pack(
    anchor=W)

Radiobutton(window, text="MP4 Format", variable=mimeType,
            height=5,
            width=20, value='audio/mp4', bg="#36DEE5",
            activebackground="#36DEE5", command=display_current_mimetype).pack(
    anchor=W)

Radiobutton(window, text="MP2 Format", variable=mimeType,
            height=5,
            width=20, value='audio/mp2', bg="#36DEE5",
            activebackground="#36DEE5", command=display_current_mimetype).pack(
    anchor=W)
Radiobutton(window, text="WAV Format", variable=mimeType,
            height=5,
            width=20, value="audio/wav", bg="#36DEE5",
            activebackground="#36DEE5", command=display_current_mimetype).pack(
    anchor=W)
Radiobutton(window, text="M4A Format", variable=mimeType,
            height=5,
            width=20, value='audio/m4a', bg="#36DEE5",
            activebackground="#36DEE5", command=display_current_mimetype).pack(
    anchor=W)

print(mimeType.get())  # Will print the initial value

mainloop()

CodePudding user response:

I think you're setting up the Radiobuttons incorrectly. Setting their var= option to a StringVar isn't right, you should set it to the value you want assigned to the mimeType StringVar when the Radiobutton is selected.

Here's a runnable example of what I mean. Note I have renamed several of your variable and reformatted the code to more closely follow the screenshot

  • Related