没有从tkinter中的其他函数(def(获取单选按钮'值',如何在不使用class的情况下实现这一点?
在这种情况下,a=a1.get(( 不会从 ques1(( 函数中的命令(sub1 按钮(中获取值。
从 tkinter 进口 *
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()
在程序中多次使用Tk
的副作用。基本上,"a1"与"根"窗口相关联,当您销毁"根"时,"a1"将不再起作用。
您有以下几种选择:
- 始终保持同一窗口打开,并换掉框架。
- 使用
Toplevel()
创建新窗口而不是Tk
。
选项 1 似乎最适合您。在这里:
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()
另外,不要害怕上课。他们很棒。
此外,众所周知,在同一行上初始化小部件和布局的方式会导致错误。始终使用 2 行。所以取而代之的是这个
button=Button(frame,text="Start Test", command=ques1).grid()
使用这个:
button=Button(frame,text="Start Test", command=ques1)
button.grid()
使用 Tk
的单个实例。在一个中创建的变量和小部件无法从另一个访问。
你的代码有一些常见的错误。您正在为每个问题创建一个新窗口。这不是一个好主意。您可以使用Toplevel
但我建议您使用root
。您可以销毁所有以前的小部件并放置新的小部件。当第一个问题时,两个单选按钮都未选中,如果未选中任何单选按钮,则返回 0。您正在Window1
中创建按钮,因此您必须将其与您的变量绑定。
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()