mson
mson

Reputation: 29

Bug with radiobutton - tkinter

I use Tkinter for make a GUI. I have a window with 2 radiobutton ('Yes' and 'No'), but when I select one, it don't run the script :

root = Tk()

Button(root, text='TEST', command=root.quit).pack()

root.mainloop()

master = Tk()
v = IntVar()

Radiobutton(master, text='Yes', variable=v, value=0).pack()
Radiobutton(master, text='No', variable=v, value=1).pack()
Button(master, text='Exit', command=master.quit).pack()

master.mainloop()

print(v.get())

if v.get() == 0:
    testy = Tk()
    Label(testy, text='Bad').pack()
    testy.mainloop()

else:
    testn = Tk()
    Label(testn, text='Bad').pack()
    testn.mainloop()

If I don't have the first window, it works but with it, it don't.

Somebody know how to fix this problem ?

Upvotes: 0

Views: 631

Answers (2)

Vlox
Vlox

Reputation: 729

Possible duplicate of tkinter radiobutton not updating variable, but seeing as this question was asked first the answer may make more sense here.


Make sure you are assigning a master to the IntVar like self.rbv=tk.IntVar(master) #or 'root' or whatever you are using):

import Tkinter as tk
import ttk

class My_GUI:

    def __init__(self,master):
        self.master=master
        master.title("TestRadio")

        self.rbv=tk.IntVar(master)#<--- HERE! notice I specify 'master'
        self.rb1=tk.Radiobutton(master,text="Radio1",variable=self.rbv,value=0,indicatoron=False,command=self.onRadioChange)
        self.rb1.pack(side='left')
        self.rb2=tk.Radiobutton(master,text="Radio2",variable=self.rbv,value=1,indicatoron=False,command=self.onRadioChange)
        self.rb2.pack(side='left')
        self.rb3=tk.Radiobutton(master,text="Radio3",variable=self.rbv,value=2,indicatoron=False,command=self.onRadioChange)
        self.rb3.pack(side='left')

    def onRadioChange(self,event=None):
        print self.rbv.get()

root=tk.Tk()
gui=My_GUI(root)
root.mainloop()

try running that, click the different buttons (they are radiobuttons but with indicatoron=False) and you will see it prints correctly changed values!

Upvotes: 1

Israel Unterman
Israel Unterman

Reputation: 13510

  1. You have initiated several Tk() systems, but there should be only one.
  2. If you want to get a new window then use Toplevel()
  3. No code is executed after mainloop() except for events. The code continues to "flow" after mainloop only after closing the windows.

So here is your code with fixes:

from tkinter import *

root = Tk()

Button(root, text='TEST', command=root.quit).pack()

master = Toplevel()
v = IntVar()

def check_radio():
    print(v.get())

    if v.get() == 0:
        Label(Toplevel(), text='Bad').pack()
    else:
        Label(Toplevel(), text='Good').pack()


Radiobutton(master, text='Yes', variable=v, value=0, command=check_radio).pack()
Radiobutton(master, text='No', variable=v, value=1, command=check_radio).pack()
Button(master, text='Exit', command=master.quit).pack()

root.mainloop()

Check carefully, I changed the parents of widgets and other changes.

Upvotes: 1

Related Questions