JKR
JKR

Reputation: 99

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

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.

Upvotes: 2

Views: 2608

Answers (3)

martineau
martineau

Reputation: 123393

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 PEP 8 - Style Guide for Python Code guidelines.

from tkinter import *

window = Tk()

MP3 = 'audio/mp3'
MP4 = 'audio/mp4'
MP2 = 'audio/mp2'
WAVE = 'audio/wav'
FLAC = 'audio/flac'
M4A = 'audio/m4a'
mimeType = StringVar(value='N/A')
BUTTON_OPTS = dict(height=1, width=20, bg='#36DEE5', activebackground= '#36DEE5',
                   variable=mimeType, command=lambda: print(mimeType.get()))

mp3_btn = Radiobutton(window, text='MP3 Format', value=MP3, **BUTTON_OPTS)
mp3_btn.pack()
mp4_btn = Radiobutton(window, text='MP4 Format', value=MP4, **BUTTON_OPTS)
mp4_btn.pack()
mp2_btn = Radiobutton(window, text='MP2 Format', value=MP2, **BUTTON_OPTS)
mp2_btn.pack()
wav_btn = Radiobutton(window, text='WAV Format', value=WAVE, **BUTTON_OPTS)
wav_btn.pack()
flac_btn = Radiobutton(window, text='FLAC Format', value=FLAC, **BUTTON_OPTS)
flac_btn.pack()
m4a_btn = Radiobutton(window, text='M4A Format', value=M4A, **BUTTON_OPTS)
m4a_btn.pack()

window.mainloop()

Here's a screenshot of what it looks like running:

screenshot

Upvotes: 1

Carl_M
Carl_M

Reputation: 948

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()

Upvotes: 2

Sriram Srinivasan
Sriram Srinivasan

Reputation: 700

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.

Upvotes: 3

Related Questions