用py制作一个答题系统具备上下题,记录错题,已完成题目,保存radiobutton答案选项等功能的代码
时间: 2024-03-16 19:43:45 浏览: 70
Android控件RadioButton实现多选一功能
好的,以下是一个简单的代码示例,实现了上述功能:
```python
import csv
import tkinter as tk
from tkinter import ttk
class QuizApp:
def __init__(self, quiz_file):
self.quiz_data = []
with open(quiz_file, 'r') as f:
reader = csv.reader(f)
for row in reader:
self.quiz_data.append(row)
self.current_index = 0
self.answers = {}
self.wrong_answers = []
self.create_gui()
def create_gui(self):
self.root = tk.Tk()
self.root.title('Quiz App')
self.question_label = ttk.Label(self.root, text='')
self.question_label.pack(padx=20, pady=10)
self.radio_vars = []
self.radio_buttons = []
for i in range(4):
var = tk.StringVar()
self.radio_vars.append(var)
rb = ttk.Radiobutton(self.root, text='', variable=var, value=str(i))
rb.pack(padx=20, pady=5)
self.radio_buttons.append(rb)
self.prev_button = ttk.Button(self.root, text='Prev', command=self.show_prev_question)
self.prev_button.pack(side='left', padx=20, pady=10)
self.next_button = ttk.Button(self.root, text='Next', command=self.show_next_question)
self.next_button.pack(side='left', padx=20, pady=10)
self.submit_button = ttk.Button(self.root, text='Submit', command=self.submit_answers)
self.submit_button.pack(side='left', padx=20, pady=10)
self.status_canvas = tk.Canvas(self.root, width=400, height=20)
self.status_canvas.pack(padx=20, pady=10)
self.show_question()
def show_question(self):
question = self.quiz_data[self.current_index][0]
self.question_label.configure(text=question)
for i in range(4):
answer = self.quiz_data[self.current_index][i+1]
self.radio_buttons[i].configure(text=answer)
self.radio_vars[i].set('')
def show_prev_question(self):
if self.current_index > 0:
self.current_index -= 1
self.show_question()
def show_next_question(self):
if self.current_index < len(self.quiz_data) - 1:
self.current_index += 1
self.show_question()
def submit_answers(self):
for i in range(4):
if self.radio_vars[i].get() == '0':
self.answers[self.current_index] = i
if self.quiz_data[self.current_index][-1] != 'A':
self.wrong_answers.append(self.current_index)
break
self.show_question()
self.update_status()
def update_status(self):
self.status_canvas.delete('all')
self.status_canvas.create_text(10, 10, text=f'Question: {self.current_index+1}/{len(self.quiz_data)}')
self.status_canvas.create_text(200, 10, text=f'Correct: {len(self.answers)}')
self.status_canvas.create_text(390, 10, text=f'Wrong: {len(self.wrong_answers)}')
def run(self):
self.root.mainloop()
def save_answers(self, output_file):
with open(output_file, 'w', newline='') as f:
writer = csv.writer(f)
for index, answer in self.answers.items():
writer.writerow([index, self.quiz_data[index][-1], self.radio_buttons[answer].cget('text')])
if __name__ == '__main__':
quiz = QuizApp('quiz.csv')
quiz.run()
quiz.save_answers('answers.csv')
```
其中,`quiz.csv`是题目数据文件,格式如下:
```
下列哪个不是Python的基本数据类型?,A.int,B.float,C.str,D.list,A
Python中用什么关键字定义一个函数?,A.def,B.function,C.fun,D.method,A
```
在`__init__`方法中,读取题目数据,并初始化一些变量。在`create_gui`方法中,创建界面并显示第一题。在`show_question`方法中,根据当前题目的索引,更新界面上的题目和答案选项。在`show_prev_question`和`show_next_question`方法中,用于切换上下题。在`submit_answers`方法中,保存用户的答案,并更新界面上的题目和答案选项。在`update_status`方法中,更新答题统计信息。在`run`方法中,启动界面,并在界面关闭后保存用户的答案到文件中。
阅读全文