Tkinter Radio button on the second window

D

Dream

The code create 2 windows. 2 radiobuttons are put on the second
window. A control variable "v" is binded to the 2 widgets.
But when I run the code, I found the control variable not binded
succsessfully: The second radiobutton was not selected by default;
Click ed each button always print 1. I don't know what is wrong. So I
need help.Thanks.

from Tkinter import *

def test(event_instance):
print v.get()

window1=Tk()
window2=Tk()
v=IntVar()
v.set(1)
radiobutton1=Radiobutton(window2,variable=v,value=0)
radiobutton2=Radiobutton(window2,variable=v,value=1)
radiobutton1.pack()
radiobutton2.pack()
radiobutton2.bind('<Button-1>',test)
radiobutton1.bind('<Button-1>',test)
window1.mainloop()
 
G

Gabriel Genellina

The code create 2 windows. 2 radiobuttons are put on the second
window. A control variable "v" is binded to the 2 widgets.
But when I run the code, I found the control variable not binded
succsessfully: The second radiobutton was not selected by default;
Click ed each button always print 1. I don't know what is wrong. So I
need help.Thanks.

from Tkinter import *
window1=Tk()
window2=Tk()

Don't create more than one root Tk instance. Weird things happen, like
this.
If you need another, separate window, use a Toplevel widget.
 
G

Guilherme Polo

The code create 2 windows. 2 radiobuttons are put on the second
window. A control variable "v" is binded to the 2 widgets.
But when I run the code, I found the control variable not binded
succsessfully: The second radiobutton was not selected by default;
Click ed each button always print 1. I don't know what is wrong. So I
need help.Thanks.

from Tkinter import *

def test(event_instance):
print v.get()

window1=Tk()
window2=Tk()

Now you have created two Tcl interpreters, and Tkinter stores the
"default master" as the first one created, so window1 is the default
master.
v=IntVar()

Here is your variable with no master specified, meaning it will use
the default master, "window1".
v.set(1)
radiobutton1=Radiobutton(window2,variable=v,value=0)
radiobutton2=Radiobutton(window2,variable=v,value=1)

Now you set the master for your buttons as window2, and use a variable
that has as master window1. This is the big problem, besides the use
of two Tcl interpreters. Tkinter doesn't really try to make this
situation work, so you better not continue with it.
 
D

Dream

Thank you very much.

Now I use "window2=Toplevel()" to create the second window, and it
works well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,075
Messages
2,570,562
Members
47,197
Latest member
NDTShavonn

Latest Threads

Top