Sanjaya
Sanjaya

Reputation: 49

How do I read the radio button value without clicking in tkinter python

What is the best way to read a radio button when it is already selected in tkinter Python.

I have a function in main loop, Radio button is used to call other functions, this process is run in While1: loop, problem is I have to select the radio button in each time of processing. is there any method to select the earlier selected radio button value, unless radio button is not changed.

Upvotes: 1

Views: 384

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36722

You should use mainloop instead of a infinite while loop.
Here is an example that prints the value of the selected radiobutton, every second:

from tkinter import *


def read_value(e):
    print(v.get())
    master.after(1000, read_value, 'dummy')


master = Tk()

v = IntVar()
v.set(1)

r1 = Radiobutton(master, text="One", variable=v, value=1)
r1.pack(anchor=W)
r2 = Radiobutton(master, text="Two", variable=v, value=2)
r2.pack(anchor=W)

read_value('dummy')

master.mainloop()

Upvotes: 2

Related Questions