0

I am trying to figure out how to use tkinter radio-buttons properly.

I have used this question as a guideline: Radio button values in Python Tkinter

For some reason I can't figure out how to return a variable that is indicative of what the user selected.

Code:

 def quit_loop():
    global selection
    selection = option.get()
    root.quit()
    return selection



def createWindow():
    root = Tk()
    root.geometry=('400x400')
    option = StringVar()
    option.set('none')
    R1 = Radiobutton(root, text='Compile', value = 'Compile', var=option)
    R2 = Radiobutton(root, text='Create', value = 'Create', var=option)
    button = Button(root, text='ok', command=quit_loop)
    R1.pack()
    R2.pack()
    button.pack()
    root.mainloop()

when I call createWindow() I would expect the radio-button box to pop up, and after making my selection and pressing 'ok' I expected it to return me a variable selection which relates to the selected button. Any advice? Tkinter stuff is particularly challenging to me because it seems so temperamental.

MaxB
  • 428
  • 1
  • 8
  • 24
  • Where would you expect the value to be returned *to*? You aren't calling `quit_loop()` yourself, you have no access to its return value. You need to do whatever needs to be done with that value from within the function. – jasonharper Oct 22 '19 at 14:49
  • @jasonharper this is where I am confused and have no clue what to do.. The link I posted had it set up the exact same way so I am confused. I assumed when the button was pressed it would then call the `quit_loop()` function which would return the value of what was selected – MaxB Oct 22 '19 at 14:52

2 Answers2

1

You need to make option global if you want to access outside of createWindow

Here's an example of your code that will print out the value of the selected radiobutton and then quit when you click the button. I simply had to declare root and options as global:

from tkinter import *

def quit_loop():
    global selection
    selection = option.get()
    root.quit()
    return selection

def createWindow():
    global option, root
    root = Tk()
    root.geometry=('400x400')
    option = StringVar()
    option.set('none')
    R1 = Radiobutton(root, text='Compile', value = 'Compile', var=option)
    R2 = Radiobutton(root, text='Create', value = 'Create', var=option)
    button = Button(root, text='ok', command=quit_loop)
    R1.pack()
    R2.pack()
    button.pack()
    root.mainloop()

createWindow()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
-1

As far as I know one needs to do two things to communicate with tkinter widgets: pass a variable, and pass a command. When user interacts with widgets, tkinter will do two things: update value of variable and call the function passed in as command. It is up to us to access the value of the variable inside the command function.

import tkinter as tk
from tkinter import StringVar, Radiobutton

def handle_radio():
    print(option.get())

root = tk.Tk()

option = StringVar()
option.set('none')
R1 = Radiobutton(root, text='Compile', value = 'Compile', var=option, command=handle_radio)
R2 = Radiobutton(root, text='Create', value = 'Create', var=option, comman=handle_radio)

R1.pack()
R2.pack()

root.mainloop()

The code prints 'Create' and 'Compile' when user selects the appropriate radio option.

Hope this helps.

Regards,

Prasanth

Prasanth
  • 412
  • 2
  • 4