基于Python開發(fā)一個選擇題訓練工具
一、引言
隨著人工智能技術的不斷進步,傳統(tǒng)的教學方式已經(jīng)逐漸向智能化、互動化轉變。在眾多英語測試題型中,選擇題作為一種高效的方式被廣泛應用于各類培訓與考試中。為了幫助學生高效學習與自測,本篇文章將采用Python編寫一款基于 Python 開發(fā)的選擇題訓練工具。該工具不僅支持加載 Excel 文件中的題庫,還具備題干和選項展示、答題記錄、音頻朗讀等多種功能,旨在為用戶提供便捷、高效的學習體驗。
二、開發(fā)背景
在日常教學過程中,老師往往需要準備大量的選擇題作為測試題目。我們可以把這些題目存儲在 Excel 文件中,如果手動處理和展示題目會非常繁瑣。為了提高教學效率,選用一種自動化展示工具成為一種迫切的需求。本項目通過 Python 和 Tkinter 庫實現(xiàn)了一個圖形化界面應用,可以自動加載并展示選擇題,記錄用戶答題情況,同時通過音頻朗讀題目,進一步增強互動性。軟件的示意圖如下:

軟件展示
三、軟件特點
題庫加載與展示:支持從 Excel 文件中加載選擇題,并且可以根據(jù)課程單元名稱篩選題目。
音頻朗讀功能:使用 Python 的 Pygame 庫播放題目音頻,即朗讀題干,幫助用戶通過聽力鞏固已學單詞。
答題記錄與統(tǒng)計:軟件會自動記錄用戶的答題情況,并在決定答題結束時,提供正確率和錯誤題目的回顧功能。
多線程支持:使用線程處理音頻播放等耗時操作,避免界面卡頓,提升用戶體驗。同時,當完成一道題時,會自動轉到下一道,并給出正確或錯誤的提示。正確就是顯示綠色的button選項,錯誤就顯示紅色的button選項。
圖形化界面:基于 Tkinter 實現(xiàn)的 GUI 界面,簡單直觀,易于操作。
四、使用方法
1. 安裝依賴
在運行程序之前,需要安裝一些 Python 庫,依賴包括 openpyxl, pygame 和 requests。可以使用以下命令安裝所需的庫:
pip install openpyxl pygame requests
2. 準備題庫
本程序需要一個 Excel 文件,并命名為【選擇題.xlsx】作為題庫,Excel 文件應包括如下列:
單元:課程名稱或章節(jié)
序號:題目的編號
題干:選擇題的內(nèi)容
選項A、B、C、D:四個選擇項
正確答案:正確的答案選項
Excel表的內(nèi)容如下:

Excel表內(nèi)容
3. 運行程序
運行 Python 程序,tkinter GUI界面會自動加載,用戶可以在頂部下拉框選擇不同的課程,然后開始答題,結束前查看戰(zhàn)績,回顧錯誤題目,還可以點朗讀通過音頻功能聽題干。
4. 功能概覽
選擇題顯示:在每道題目顯示時,用戶可以選擇答案。程序會自動判斷正確與否,并高亮顯示選擇的答案。
戰(zhàn)績統(tǒng)計:用戶答題完成后,可以查看自己的正確率統(tǒng)計。
錯誤回顧:用戶可以查看答錯的題目,并且看到正確答案與自己的選擇。
朗讀功能:用戶可以點擊按鈕讓程序朗讀題干。
五、源碼展示
以下是該程序的主要代碼:
import tkinter as tk
from tkinter import ttk, messagebox
from openpyxl import load_workbook
import random
import os,re
import pygame,requests
from io import BytesIO
from threading import Lock, Thread, Timer
def load_excel_data(filename):
workbook = load_workbook(filename)
sheet = workbook.active
questions = []
lessons = set() # 用于存儲所有課程名,避免重復
for row in sheet.iter_rows(min_row=2, values_only=True):
lesson = row[0]
lessons.add(lesson)
question = {
'單元': lesson,
'序號': row[1],
'題干': row[2],
'選項': [row[3], row[4], row[5], row[6]],
'正確答案': row[7]
}
questions.append(question)
return list(lessons), questions
lessons, questions = load_excel_data('選擇題.xlsx')
random.shuffle(questions)
# 使用正則表達式從課程名中提取數(shù)字,并按數(shù)字排序課程名
sorted_lessons = sorted(lessons, key=lambda x: (lambda m: int(m.group(0)) if m else 0)(re.search(r'\d+', x)))
class QuizApp:
def __init__(self, root):
self.root = root
# 設置窗口大小
self.root.geometry("600x500")
#self.root.resizable(False, False)
pygame.init()
self.all_questions = questions
self.questions = questions # 當前顯示的問題列表
self.current_question_index = 0
self.correct_answers = 0
self.wrong_answers = 0
self.error_questions = []
self.setup_ui()
self.load_question()
def thread_it(self,func):
self.thread1=Thread(target=func)
self.thread1.setDaemon(True)
self.thread1.start()
def setup_ui(self):
self.root.title("選擇題訓練工具")
self.lesson_combobox = ttk.Combobox(self.root, values=['全部'] + sorted_lessons, state="readonly")
self.lesson_combobox.pack(pady=(10, 0))
self.lesson_combobox.set('全部')
self.lesson_combobox.bind('<<ComboboxSelected>>', self.on_combobox_change)
self.question_label = tk.Label(self.root, text="", font=("Times New Roman", 20), fg="blue", wraplength=520, justify=tk.LEFT) #設置文本長度,并且文本左對齊
self.question_label.pack(pady=(10, 40))
self.options_frame = tk.Frame(self.root)
self.options_frame.pack(pady=20)
option_labels = ['A', 'B', 'C', 'D'] # 選項標簽
self.option_buttons = [] # 存儲選項按鈕
self.option_labels = [] # 存儲選項標簽控件
for i in range(4):
# 創(chuàng)建選項標簽控件并放置
label = tk.Label(self.options_frame, text=f"{option_labels[i]}.", font=("Times New Roman", 16))
label.grid(row=i, column=0, pady=5, sticky="e")
self.option_labels.append(label)
# 創(chuàng)建選項按鈕并放置
btn = tk.Button(self.options_frame, text="", font=("Times New Roman", 16), width=20,
command=lambda b=i: self.check_answer(b), # type: ignore
anchor="w")
btn.grid(row=i, column=1, pady=5)
self.option_buttons.append(btn)
self.action_frame = tk.Frame(self.root)
self.action_frame.pack(side=tk.BOTTOM, pady=20)
tk.Button(self.action_frame, text="退出程序", font=("宋體", 16, "bold"), width=9, command=self.ui_quit).pack(side=tk.LEFT, padx=10)
tk.Button(self.action_frame, text="我的戰(zhàn)績", font=("宋體", 16, "bold"), width=9, command=self.show_score).pack(side=tk.LEFT, padx=10)
tk.Button(self.action_frame, text="查看錯誤", font=("宋體", 16, "bold"), width=9, command=self.show_errors).pack(side=tk.LEFT, padx=10)
tk.Button(self.action_frame, text="朗讀題干", font=("宋體", 16, "bold"), width=9, command=self.show_sound).pack(side=tk.LEFT, padx=10)# 其余UI代碼與原來相同...
def on_combobox_change(self, event):
selected_lesson = self.lesson_combobox.get()
if selected_lesson == '全部':
self.questions = self.all_questions
else:
self.questions = [q for q in self.all_questions if q['單元'] == selected_lesson]
self.current_question_index = 0
self.correct_answers = 0
self.wrong_answers = 0
self.error_questions = []
self.load_question()
def load_question(self):
if self.current_question_index < len(self.questions):
question = self.questions[self.current_question_index]
self.question_label.config(text=str(self.current_question_index+1)+". "+question['題干'],anchor='w')
correct_answer = question['正確答案']
options = question['選項']
self.correct_option_index = options.index(correct_answer)
random.shuffle(options)
for i, option in enumerate(options):
self.option_buttons[i].config(text=option, bg='SystemButtonFace')
def check_answer(self, button_index):
question = self.questions[self.current_question_index]
selected_option = self.option_buttons[button_index].cget('text')
correct_option = question['正確答案']
# 確定正確答案按鈕的索引
correct_option_index = None
for i, btn in enumerate(self.option_buttons):
if btn.cget('text') == correct_option:
correct_option_index = i
break
if selected_option == correct_option:
# 用戶選擇了正確的答案
self.option_buttons[button_index].config(bg='light green')
self.correct_answers += 1
else:
# 用戶選擇了錯誤的答案
self.option_buttons[button_index].config(bg='red')
if correct_option_index is not None:
self.option_buttons[correct_option_index].config(bg='light green')
self.wrong_answers += 1
self.error_questions.append((question['題干'], selected_option, question['正確答案']))
# 準備間隔1秒顯示下一個問題
self.root.after(1000, self.next_question)
def next_question(self):
self.current_question_index += 1
if self.current_question_index < len(self.questions):
self.load_question()
else:
messagebox.showinfo("結束", "所有問題都已回答完畢!")
def ui_quit(self):
self.root.destroy()
def show_score(self):
messagebox.showinfo("戰(zhàn)績", f"正確: {self.correct_answers}\n錯誤: {self.wrong_answers}\n總計: {len(self.questions)}\n正確率:{self.correct_answers/len(self.questions)*100}%")
def show_sound(self):
self.thread_it(self.show_sound2)
def show_errors(self):
error_messages = "\n".join([f"題干: {q[0]},正確答案是:{q[2]}, 您的選擇: {q[1]}" for q in self.error_questions])
messagebox.showinfo("錯誤回顧", error_messages if error_messages else "完美!沒有任何錯誤。")
def show_sound2(self):
self.current_name = self.question_label.cget("text")
audio_path = f"https://dict.youdao.com/dictvoice?audio={self.current_name}&type=1"
resp = requests.get(audio_path)
audio_data = BytesIO(resp.content)
pygame.mixer.music.load(audio_data)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
# 退出pygame
if __name__ == "__main__":
root = tk.Tk()
app = QuizApp(root)
root.mainloop()六、注意事項
Excel 文件格式:確保題庫文件【選擇題.xlsx】的格式正確,每個問題必須包含單元、序號、題干、選項和正確答案等字段。
音頻播放:程序中使用了 Pygame 庫進行音頻播放,因此需要安裝相應的音頻庫,并且程序會訪問在線字典接口生成題目音頻。
線程管理:為了避免界面卡頓,音頻播放等操作被放置在單獨的線程中處理,保證主界面的流暢性。
數(shù)據(jù)存儲:答題結果和錯誤回顧會暫時保存在程序內(nèi)存中,但如果需要長期保存數(shù)據(jù),可以考慮添加導出功能。
七、總結
這款選擇題訓練工具通過 Python 實現(xiàn)了一個簡單易用的答題系統(tǒng),利用 Tkinter 提供了良好的用戶界面,結合 Pygame 和音頻播放技術,增強了選擇題問答的互動性。它可以幫助學生提高學習效率,幫助教師管理題庫,并提供了直觀的成績統(tǒng)計與錯誤回顧功能,是一款非常實用的教學輔助工具。
到此這篇關于基于Python開發(fā)一個選擇題訓練工具的文章就介紹到這了,更多相關Python選擇題訓練工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Pycharm?debug程序,跳轉至指定循環(huán)條件/循環(huán)次數(shù)問題
這篇文章主要介紹了Pycharm?debug程序,跳轉至指定循環(huán)條件/循環(huán)次數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
python并發(fā)執(zhí)行request請求的示例
這篇文章主要介紹了python并發(fā)執(zhí)行request請求,我將為我們展示使用concurrent.futures.ThreadPoolExecutor和requests庫并發(fā)執(zhí)行HTTP請求的示例,需要的朋友可以參考下2024-06-06
Python結合PyQt5編寫一個批量目錄重命名工具并打包成EXE
這篇文章主要為大家詳細介紹了Python如何結合PyQt5編寫一個批量目錄重命名工具,并打包成帶圖標的 EXE,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2026-01-01
用Python實現(xiàn)一個簡單的用戶系統(tǒng)
大家好,本篇文章主要講的是用Python實現(xiàn)一個簡單的用戶系統(tǒng),感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Python實現(xiàn)Excel和CSV之間的相互轉換
通過使用Python編程語言,編寫腳本來自動化Excel和CSV之間的轉換過程,可以批量處理大量文件,定期更新數(shù)據(jù),并集成轉換過程到自動化工作流程中,本文將介紹如何使用Python 實現(xiàn)Excel和CSV之間的相互轉換,需要的朋友可以參考下2024-03-03

