You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
6.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import random
import tkinter as tk
from tkinter import messagebox, ttk
import pymysql
# 定义问题和选项
with pymysql.connect(host='localhost', user='root', password='123456', database='mynewdb') as connection:
cursor = connection.cursor()
sql1 = "select 题干 from mynewdb.test"
cursor.execute(sql1)
questions = cursor.fetchall()
sql2 = "select 选项A, 选项B, 选项C, 选项D from mynewdb.test"
cursor.execute(sql2)
result = cursor.fetchall()
# 将查询结果转化为需要的格式
options = []
for row in result:
options.append(list(row)) # 每个问题的选项
# 查询正确答案
sql3 = "select 答案 from mynewdb.test"
cursor.execute(sql3)
correct_answers = cursor.fetchall()
# 存储正确答案
right_answers = [answer[0] for answer in correct_answers]
columns = ["题号", "你的答案", "正确答案", "得分"]
class QuestionnaireGUI:
def __init__(self, master):
self.master = master
self.question_index = 0
self.score = 0 # 初始化总分
self.whole_score = 0
self.your_answers = []
self.right_answers = right_answers # 从数据库加载的正确答案
# 用于存储未出过的题目索引
self.remaining_questions = list(range(len(questions)))
self.question_label = tk.Label(master, text="")
self.question_label.pack()
self.option_buttons = []
for i in range(4):
button = tk.Button(master, text="", command=lambda i=i: self.select_option(i))
button.pack()
self.option_buttons.append(button)
self.next_button = tk.Button(master, text="下一题", command=self.generate_next_question)
self.next_button.pack()
self.submit_button = tk.Button(master, text="提交问卷", command=self.submit_questionnaire)
self.submit_button.pack()
self.test_tree = ttk.Treeview(self.master)
self.test_tree.pack(expand=True, fill='both')
self.test_tree['columns'] = columns
self.test_tree.column("#0", width=0, stretch=tk.NO) # 隐藏第一个空列
for column in columns:
self.test_tree.column(column, anchor=tk.W, width=100, stretch=tk.YES)
self.test_tree.heading(column, text=column, anchor=tk.W)
self.recommendation_universities_tree = ttk.Treeview(self.master, columns=[1, 2], show='headings')
self.recommendation_universities_tree.column('1', width=300)
self.recommendation_universities_tree.column('2', width=300)
self.recommendation_universities_tree.pack()
# Initialize the tree with the first row of questions
self.populate_tree()
def generate_next_question(self):
if len(self.remaining_questions) > 0 and self.question_index < 10:
# 随机从剩余的题目中选择一题
random_index = random.choice(self.remaining_questions)
self.remaining_questions.remove(random_index)
# 显示题目和选项
self.question_label.config(text=questions[random_index][0])
for i, option in enumerate(options[random_index]):
self.option_buttons[i].config(text=f"{chr(ord('A') + i)}. {option}")
self.question_index += 1 # 增加题目索引
else:
self.next_button.config(state="disabled")
self.submit_button.config(state="normal")
def recommendation_universities(self):
# 修改查询语句:根据总分获取分数小于总分的学校
with pymysql.connect(host='localhost', user='root', password='123456', database='mynewdb') as connection:
cursor = connection.cursor()
self.score = self.score * 7.5
sql4 = "select 学校名称, 总分 from mynewdb.score where 总分 < %s" # 这里使用占位符
cursor.execute(sql4, (self.score,))
score_result = cursor.fetchall()
print()
for i in score_result:
self.recommendation_universities_tree.insert('', "end", values=i)
def select_option(self, option_index):
if len(self.your_answers) < 10: # 确保最多选择10个答案
self.your_answers.append(option_index)
self.next_button.config(state="normal")
def submit_questionnaire(self):
# 计算得分
self.score = 0 # 每次提交前重置分数
for i, answer in enumerate(self.your_answers):
# 获取正确答案
correct_answer = self.right_answers[i]
# 如果用户答案正确,得分
if chr(ord('A') + answer) == correct_answer:
score = 10 # 假设每题10分
self.score += score
else:
score = 0
# 添加数据到Treeview
question_data = (
i + 1, # 题号
chr(ord('A') + answer), # 用户选择的答案A、B、C、D
correct_answer, # 正确答案
score, # 得分
)
self.whole_score = self.whole_score + 10
self.test_tree.insert("", "end", values=question_data)
# 更新总分显示
self.update_score_display()
# 显示推荐建议
self.show_recommendation()
self.recommendation_universities()
def update_score_display(self):
# 删除已有的总分标签,如果有
for widget in self.master.winfo_children():
if isinstance(widget, tk.Label) and widget.cget("text").startswith("总分:"):
widget.destroy()
# 显示新的总分
score1_label = tk.Label(self.master, text="总分:")
score1_label.pack(side=tk.LEFT)
score2_label = tk.Label(self.master, text=str(self.score))
score2_label.pack(side=tk.LEFT)
def show_recommendation(self):
"""根据分数显示个性化推荐"""
if self.score >= 90:
recommendation = "你做得非常好!继续保持,强化做题速度和答题技巧。"
elif self.score >= 70:
recommendation = "你基本掌握了相关知识,但可以针对薄弱点进行进一步巩固。"
else:
recommendation = "你可能需要加强基础知识,特别是错题部分。建议多做相关题目进行复习。"
# 显示推荐建议
recommendation_label = tk.Label(self.master, text="推荐建议:")
recommendation_label.pack()
recommendation_text = tk.Label(self.master, text=recommendation)
recommendation_text.pack()
def populate_tree(self):
# 预填充一行数据作为示例
for i in range(len(self.your_answers)):
sample_data = (i, self.your_answers, self.right_answers, 10)
self.test_tree.insert("", "end", values=sample_data)
if self.your_answers[i] == self.right_answers[i]:
self.score += 10
self.update_score_display() # 确保开始时显示总分