【发布时间】:2019-02-13 00:38:26
【问题描述】:
我是一名初学者,尝试使用 Tkinter 在 python 中创建多项选择测验。对不起,如果我创建了一个混乱的代码。我正在使用单选按钮来获得不同的答案。我想在选择选项 1 时显示消息“这是正确答案”,在选择任何其他选项时显示“这是错误答案”。目前,无论选择何种选项,消息始终是“这是错误答案”。我明白价值与它无关,但我没有找到正确的方法。有没有办法定义这种命令?非常感谢您提供的任何帮助、建议和答案。
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
window = Tk()
window.title("Quiz")
window.geometry("500x150")
score = 0
def inst():
t = Label(window, text="Choose the correct statement to answer the
question")
t.pack()
def start():
start_game()
greet = Label(window, text="Welcome to the Quiz")
greet.pack()
startButton = Button(window, command=start, text="Start")
startButton.pack()
instr = Button(window, text="Instructions", command=inst)
instr.pack()
end = Button(window, text="Exit", command=window.destroy)
end.pack()
def start_game():
top = Toplevel()
top.title("Question 1")
var = StringVar()
def check():
if var.get() is True:
messagebox.showinfo('Congrats', message='This is the correct
answer.Score is {}'.format(score))
else:
messagebox.showinfo('Lose', message='This answer is wrong.')
R1 = Radiobutton(top,
text="Option 1",
indicatoron=0,
width=20,
padx=20,
pady=10,
variable=var,
value=True,
command=check)
R1.pack( anchor = W )
R2 = Radiobutton(top,
text="Option 2",
indicatoron=0,
width=20,
padx=20,
pady=10,
variable=var,
value=False,
command=check)
R2.pack( anchor = W )
R3 = Radiobutton(top,
text="Option 3",
indicatoron=0,
width=20,
padx=20,
pady=10,
variable=var,
value=False,
command=check)
R3.pack( anchor = W)
label = Label(top)
label.pack()
window.mainloop()
【问题讨论】:
标签: python tkinter radio-button