Reputation: 11
Not getting radiobutton 'value' from other function(def) in tkinter, how to achieve this without using class?
In this case a=a1.get() is not taking value from command (of sub1 button) in ques1() function.
from tkinter import *
global root
root=Tk()
root.geometry("500x500")
a1=StringVar()
ans1=StringVar()
def ans1():
a=a1.get() #not getting it from ques1()
print(a)
def ques1():
root.destroy()
global window1
window1=Tk()
window1.geometry("500x500")
question1=Label(window1, text="How many Planets are there in Solar System").grid()
q1r1=Radiobutton(window1, text='op 1', variable=a1, value="correct").grid()
q1r2=Radiobutton(window1, text='op 2', variable=a1, value="incorrect").grid()
sub1=Button(window1, text="Submit", command=ans1).grid()
next1But=Button(window1, text="Next Question", command=ques2).grid()
def ques2():
window1.destroy()
window2=Tk()
window2.geometry("500x500")
question2=Label(window2, text="How many Planets are there in Solar System").grid()
next2But=Button(window2, text="Next Question")
button=Button(root,text="Start Test", command=ques1).grid()
Upvotes: 1
Views: 181
Reputation: 7303
Your code has some common mistakes. You are creating a new window on each question. It is not a good idea. You can use Toplevel
but I will suggest you to use the root
. You can destroy all of your previous widgets and place new ones. When the first question, both radiobuttons are unchecked and return 0 when none is selected. You are creating the buttons in Window1
so you will have to tie it with your var.
from tkinter import *
global root
root=Tk()
root.geometry("500x500")
a1=StringVar(root)
a1.set(0) #unchecking all radiobuttons
ans1=StringVar()
def ans1():
a=a1.get()
print(a)
def ques1():
for widget in root.winfo_children():
widget.destroy() #destroying all widgets
question1=Label(root, text="How many Planets are there in Solar System").grid()
q1r1=Radiobutton(root, text='op 1', variable=a1, value="correct").grid()
q1r2=Radiobutton(root, text='op 2', variable=a1, value="incorrect").grid()
sub1=Button(root, text="Submit", command=ans1).grid()
next1But=Button(root, text="Next Question", command=ques2).grid()
def ques2():
for widget in root.winfo_children():
widget.destroy()
question2=Label(root, text="How many Planets are there in Solar System").grid()
next2But=Button(root, text="Next Question")
button=Button(root,text="Start Test", command=ques1)
button.grid()
Upvotes: 0
Reputation: 13729
This is a side effect from using Tk
more than once in a program. Basically, "a1" is tied to the "root" window, and when you destroy "root", "a1" will no longer work.
You have a couple options:
Toplevel()
to make new windows instead of Tk
. Option 1 seems the best for you. Here it is:
from tkinter import *
root=Tk()
root.geometry("500x500")
a1=StringVar(value='hippidy')
ans1=StringVar()
def ans1():
a=a1.get() #not getting it from ques1()
print(repr(a))
def ques1():
global frame
frame.destroy() # destroy old frame
frame = Frame(root) # make a new frame
frame.pack()
question1=Label(frame, text="How many Planets are there in Solar System").grid()
q1r1=Radiobutton(frame, text='op 1', variable=a1, value="correct").grid()
q1r2=Radiobutton(frame, text='op 2', variable=a1, value="incorrect").grid()
sub1=Button(frame, text="Submit", command=ans1).grid()
next1But=Button(frame, text="Next Question", command=ques2).grid()
def ques2():
global frame
frame.destroy() # destroy old frame
frame = Frame(root) # make a new frame
frame.pack()
question2=Label(frame, text="How many Planets are there in Solar System").grid()
next2But=Button(frame, text="Next Question")
frame = Frame(root) # make a new frame
frame.pack()
button=Button(frame,text="Start Test", command=ques1).grid()
root.mainloop()
Also, don't be scared of classes. They are great.
Also, the way you have a widget initialization and layout on the same line is known to cause bugs. Use 2 lines always. So instead of this
button=Button(frame,text="Start Test", command=ques1).grid()
Use this:
button=Button(frame,text="Start Test", command=ques1)
button.grid()
Upvotes: 2
Reputation: 385820
You need to use a single instance of Tk
. Variables and widgets created in one cannot be accessed from another.
Upvotes: 1