有没有一种方法可以定义单选按钮的命令,以在多选测验中显示正确或错误答案



我是一个初学者,试图用Tkinter在python中创建一个多选测验。如果我创建了一个混乱的代码,很抱歉。我用单选按钮回答不同的问题。当选择选项1时,我想显示消息"This is the correct answer",而当选择任何其他选项时,我希望显示消息"这是错误的答案"。目前,无论选择哪个选项,消息总是"这是错误的答案"。我知道这个值与它无关,但我没有找到正确的方法。有办法定义这种命令吗?非常感谢您的帮助、建议和回答。

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()

由于您使用的是布尔值,我认为使用BooleanVar更有意义。

如果你知道哪个是正确的,你可以简单地将按钮传递给check功能并更改它们的颜色:

def check(btn1, btn2):
btn1.config(bg='green')
btn2.config(bg='red')

然后修改单选按钮(就在您定义的位置下方):

for btn in (R1, R2):
btn.config(command=lambda btn1=R1,btn2=R2:check(btn1,btn2))

注意,我使用了两个按钮,因为R2R3具有相同的值,所以它们可以有效地组合为一个。

下面是一个例子;它使用按钮列表来存储所有创建的单选按钮,并根据文本更改每个单选按钮的颜色,同时检查玩家是否得到了正确的答案。

import tkinter as tk
def check_answer():
if question_answer.get() == 2: #get the value of the integer variable
print('you got it right')  #if it has been set to 2 by the player, they got it right
for btn in btnlist: #check each of our radiobuttons
if int(btn['text']) == 2: #if the text of that button is equal to the correct answer
btn.config(bg='green') #make it green
else:
btn.config(bg='red') #otherwise make it red
win = tk.Tk()
question = 'What is 1+1?' #put your question here
question_answer = tk.IntVar() #we use an Integer Variable to store the value of the answer
question_answer.set(0) #we set the value of the correct answer to 0
lbl = tk.Label(win, text=question)
lbl.grid(columnspan=4)
column = 0
btnlist = []
for answer in range(4): #create radiobuttons in a for loop
btn = tk.Radiobutton(win, text=str(answer), variable=question_answer,
value=answer) #associate each button with the answer variable
#but give each button its own unique value
btnlist.append(btn)
btn.grid(row=1, column=column)
column += 1
confirm_btn = tk.Button(win, text='Confirm', command=check_answer)
confirm_btn.grid(columnspan=4)
win.mainloop()

在本例中,我使用了IntVar,因为答案是整数,您也可以根据需要使用BooleanVarStringVar

编辑:根据您在评论中的要求:

import tkinter as tk
win = tk.Tk()
text_to_add_to_btns = ['A', 'B', 'C', 'D', 'E'] #change to whatever text you like
#with however many elements which represent each individual button
btn_list = []
Column = 0
for txt in text_to_add_to_btns:
btn = tk.Button(win, text=txt)
btn.grid(row=0, column=Column, sticky='nesw')
btn_list.append(btn)
Column += 1
win.mainloop()

我们创建了一个默认列表,其中包含要作为单独列表元素添加到每个按钮的文本。然后,我们在该列表上循环,为每个元素创建每个按钮,并将按钮的文本设置为该元素,然后将其附加到单独的按钮列表中。

相关内容

最新更新