Python Tkinter 测验项目答案校验功能实现问题
实现方案
你只需要按以下步骤调整代码即可实现答案校验与结果输出功能:
核心修改点
- 在类初始化方法中新增两个实例变量,分别记录当前题号、当前题的正确答案
- 调整随机抽题逻辑,直接从题库中抽取包含题目和答案的完整条目,不需要单独维护题目列表、答案列表
- 完善提交按钮的事件处理逻辑,将用户选择的答案转换为布尔值后与标准答案比对,将结果写入滚动文本组件
- 绑定下一题按钮的点击事件,实现题目切换、单选状态重置的逻辑
完整修改后代码
import tkinter as tk from tkinter import ttk, Label, scrolledtext, IntVar, Radiobutton import random class Python_Quiz: def __init__(self): self._win = tk.Tk() self._win.resizable(False, False) self._win.title("Python Quiz - Try Out This Quiz") self._win.geometry("400x450") # 新增实例变量:记录当前题号、当前题正确答案 self.current_q_num = 1 self.current_correct_ans = None # Button actionFrame = ttk.Frame(self._win) actionFrame.pack() # Start Button self._start_btn = ttk.Button(actionFrame, text="Start") self._start_btn.pack() self._start_btn.bind('<Button-1>', self.start_btn_event) self._myLabel_text = tk.StringVar() self._myLabel_text.set("Question will appear here") self._myLabel = Label(self._win, textvariable=self._myLabel_text) self._myLabel.pack() radioFrame = ttk.Frame(self._win) self._radValue = tk.IntVar() radioFrame.pack() self.i = IntVar() self._option_1_text = tk.StringVar() self._option_1_text.set("Option 1") self._option_1_rdbtn = Radiobutton(radioFrame, textvariable=self._option_1_text, variable=self.i, value=1) self._option_2_text = tk.StringVar() self._option_2_text.set("Option 2") self._option_2_rdbtn = Radiobutton(radioFrame, textvariable=self._option_2_text, variable=self.i, value=2) self._option_1_rdbtn.pack() self._option_2_rdbtn.pack() actionFrame_2 = ttk.Frame(self._win) actionFrame_2.pack() # Submit Button self._submit_btn = ttk.Button(actionFrame_2, text="Submit") self._submit_btn.config(state = tk.DISABLED) self._submit_btn.grid(column=0, row=0) # Next Button self._next_btn = ttk.Button(actionFrame_2, text="Next") self._next_btn.config(state = tk.DISABLED) self._next_btn.grid(column=1, row=0) # 绑定下一题按钮事件 self._next_btn.bind('<Button-1>', self.next_q_event) # Scrolled Text outputFrame = ttk.Frame(self._win) outputFrame.pack() scrol_w = 50 scrol_h = 5 self._scrol_stxt = scrolledtext.ScrolledText(outputFrame, width=scrol_w, height=scrol_h, font=("Helvetica", 15) , wrap=tk.WORD) self._scrol_stxt.config(state = tk.DISABLED) self._scrol_stxt.pack() self._win.mainloop() def start_btn_event(self, event): self._scrol_stxt.config(state = tk.NORMAL) self._scrol_stxt.insert(tk.INSERT,"选择答案后点击提交\n") self._scrol_stxt.config(state = tk.DISABLED) self._submit_btn.config(state = tk.NORMAL) self._submit_btn.bind('<Button-1>', self.submit_event_handler) # 直接从题库随机抽完整条目,存储题目和正确答案 current_q = random.choice(questionBank) self.current_correct_ans = current_q[1] self._myLabel_text.set(f"Q{self.current_q_num}. {current_q[0]}") self._option_1_text.set("True") self._option_2_text.set("False") self._start_btn.config(state = tk.DISABLED) self._option_1_rdbtn.bind('<Button-1>', self.selection_event) self._option_2_rdbtn.bind('<Button-1>', self.selection_event) def submit_event_handler(self, event): if (self.i.get() ==0): self._scrol_stxt.config(state = tk.NORMAL) self._scrol_stxt.insert(tk.INSERT,f"请选择第{self.current_q_num}题的答案\n") self._scrol_stxt.config(state = tk.DISABLED) self._next_btn.config(state = tk.DISABLED) else: # 转换用户选择为布尔值:1对应True,2对应False user_ans = True if self.i.get() == 1 else False # 比对答案 self._scrol_stxt.config(state = tk.NORMAL) if user_ans == self.current_correct_ans: self._scrol_stxt.insert(tk.INSERT,f"第{self.current_q_num}题回答正确\n") else: self._scrol_stxt.insert(tk.INSERT,f"第{self.current_q_num}题回答错误\n") self._scrol_stxt.config(state = tk.DISABLED) self._next_btn.config(state = tk.NORMAL) # 提交后禁用提交按钮,避免重复提交 self._submit_btn.config(state = tk.DISABLED) def next_q_event(self, event): # 题号+1 self.current_q_num += 1 # 重置单选选择 self.i.set(0) # 抽下一题 current_q = random.choice(questionBank) self.current_correct_ans = current_q[1] self._myLabel_text.set(f"Q{self.current_q_num}. {current_q[0]}") # 恢复提交按钮,禁用下一题按钮 self._submit_btn.config(state = tk.NORMAL) self._next_btn.config(state = tk.DISABLED) def selection_event(self, event): selection = "你选择了选项 " + str(self.i.get()) print(selection) questionBank = [ ['变量名不能以数字开头', True], ["x='1'+1是合法语句", False], ['=和==可以互换使用', False], ['逻辑运算符and优先级高于or', True], ['字符串类型是不可变类型', True], ['x,y = y, x可以交换x和y的值', True], ['2=x是合法语句', False], ['变量名最长可以为50个字符', True] ] def main(): Python_Quiz() main()
效果说明
修改后点击提交按钮会自动比对答案,在滚动文本区输出对应正误提示,点击下一题按钮会自动加载新的随机题目,支持连续答题。
内容的提问来源于stack exchange,提问作者Born2Student
相关产品推荐
相关产品推荐

