没有从tkinter中的其他函数(def)获取单选按钮“value”,如何在不使用类的情况下实现这一点?

2024-04-20 06:02:06 发布

您现在位置:Python中文网/ 问答频道 /正文

没有从tkinter中的其他函数(def)获取单选按钮“value”,如何在不使用类的情况下实现这一点?你知道吗

在本例中,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()

Tags: textvaluetkinterdefa1buttonrootcommand
3条回答

您需要使用Tk的单个实例。在其中创建的变量和小部件不能从另一个访问。你知道吗

你的代码有一些常见的错误。您正在为每个问题创建一个新窗口。这不是个好主意。您可以使用Toplevel,但我建议您使用root。您可以销毁所有以前的小部件并放置新的小部件。第一个问题时,两个单选按钮都未选中,如果未选中任何单选按钮,则返回0。您正在Window1中创建按钮,因此必须将其与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()

这是在程序中多次使用Tk的副作用。基本上,“a1”绑定到“root”窗口,当您销毁“root”时,“a1”将不再工作。你知道吗

你有几个选择:

  1. 保持同一个窗口始终打开,并调出。你知道吗
  2. 使用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()

另外,不要害怕上课。他们很棒。你知道吗

另外,在同一行上进行小部件初始化和布局的方式也会导致bug。始终使用两行。所以不是这个

button=Button(frame,text="Start Test", command=ques1).grid()

使用此选项:

button=Button(frame,text="Start Test", command=ques1)
button.grid()

相关问题 更多 >