Tkinter: How to set ttk.Radiobutton activated and get its value?
Asked Answered
I

5

10

1) I need to set one of my three ttk.Radiobuttons activated by default when I start my gui app.
How do I do it?

2) I also need to check if one of my ttk.Radiobuttons was activated/clicked by the user.
How do I do it?

rb1 = ttk.Radiobutton(self.frame, text='5', variable=self.my_var, value=5)
rb2 = ttk.Radiobutton(self.frame, text='10', variable=self.my_var, value=10)
rb3 = ttk.Radiobutton(self.frame, text='15', variable=self.my_var, value=15)
self.rb1.grid(row=0)
self.rb2.grid(row=1)
self.rb3.grid(row=2)
Irrupt answered 9/9, 2013 at 22:39 Comment(1)
If any of those answers helped you, kindly mark them as answer, it will help reduce the 'unanswered questions' lineKarilynn
K
21

use self.my_var.set(1) to set the radiobutton with text='5' as the default RadioButton.

To get the selected one you have to call a function

rb1 = ttk.Radiobutton(self.frame, text='5', variable=self.my_var, value=5,command=self.selected)
rb2 = ttk.Radiobutton(self.frame, text='10', variable=self.my_var, value=10,command=self.selected)
rb3 = ttk.Radiobutton(self.frame, text='15', variable=self.my_var, value=15,command=self.selected)
self.rb1.grid(row=0)
self.rb2.grid(row=1)
self.rb3.grid(row=2)

def selected(self):
     if self.my_var.get()==5:
        "do something"
     elif self.my_var.get()==10:
        "do something"
     else:
        "do something"
Karilynn answered 24/9, 2013 at 10:28 Comment(1)
Maybe this has changed since 2013, but to get text='5' you should use self.my_var_set(5) - it's the radiobutton's value you use for activation. Anyway - your example is still helpful in 2021 ;-)Gnosis
S
15

As for your first question, a convenient and straightforward way is to call the invoke method:

rb2.invoke()

Note that this also runs any command associated with this button.

See the Ttk::radiobutton invoke documentation.

Speculative answered 16/10, 2014 at 9:17 Comment(1)
this was very useful!!Markhor
M
3

You can use rb1.state['selected'] to set the default as rb1 and self.my_var.get() to get the value (i.e the text variable) of the radiobutton.

Majormajordomo answered 8/7, 2014 at 16:33 Comment(0)
B
3

I'm just adding something to Gogo's answer but i can't comment because i don't have enough reputation.

Add self.my_var=tk.IntVar() before the radiobuttons or your program will not know the variable. see The Variable Classes for more informations

for exemple, in my case i need a StringVar(): this works for python 3.* change tkinter to Tkinter for python 2 (i'm not 100% sure tho)

import tkinter as tk
from tkinter import ttk

class XFile_Page(tk.Frame):
    ...

    self.joinType = tk.StringVar()
    self.rbLeft = ttk.Radiobutton(self.frameRadioButtons, text='left', variable=self.joinType, value='left',command=self.RadioBtnSelected)
    self.rbRight = ttk.Radiobutton(self.frameRadioButtons, text='right', variable=self.joinType, value='right',command=self.RadioBtnSelected)

    self.rbLeft.grid(row=0)
    self.rbRight.grid(row=1)

    #select the rbLeft radiobutton
    self.rbLeft.invoke()

    def RadioBtnSelected(self):
        print(self.joinType.get())
Brabant answered 13/7, 2017 at 9:0 Comment(0)
H
0

To answer the first question:

If you do not like to use variable and also would not like to use "rb1.invoke()", you can use:

rb1.select()

Read documentation here

This is for Python 3.10.12 [GCC 11.4.0]

Hagiocracy answered 2/8 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.