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.
Mathlearn/src/ui/level_selection_window.py

114 lines
3.8 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.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
学段选择窗口
处理题目数量输入和开始答题
"""
import tkinter as tk
from tkinter import messagebox, ttk
import sys
import os
# 添加父目录到路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from ui.exam_window import ExamWindow
class LevelSelectionWindow:
"""学段选择窗口类"""
def __init__(self, email, level):
self.email = email
self.level = level
self.setup_ui()
def setup_ui(self):
"""设置UI界面"""
self.window = tk.Tk()
self.window.title(f"数学学习软件 - {self.level}")
self.window.geometry("400x300")
self.window.resizable(False, False)
# 居中显示
self.center_window()
# 创建主框架
main_frame = ttk.Frame(self.window, padding="30")
main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# 标题
title_label = ttk.Label(main_frame, text=f"{self.level}数学练习",
font=("Arial", 16, "bold"))
title_label.grid(row=0, column=0, columnspan=2, pady=(0, 30))
# 题目数量输入
ttk.Label(main_frame, text="请输入题目数量:",
font=("Arial", 12)).grid(row=1, column=0, sticky=tk.W, pady=10)
self.count_var = tk.StringVar()
self.count_entry = ttk.Entry(main_frame, textvariable=self.count_var,
width=20, font=("Arial", 12))
self.count_entry.grid(row=1, column=1, pady=10, padx=(10, 0))
# 提示信息
info_text = "建议题目数量5-20题"
info_label = ttk.Label(main_frame, text=info_text,
font=("Arial", 9), foreground="gray")
info_label.grid(row=2, column=0, columnspan=2, pady=(0, 20))
# 按钮框架
button_frame = ttk.Frame(main_frame)
button_frame.grid(row=3, column=0, columnspan=2, pady=20)
# 开始答题按钮
start_btn = ttk.Button(button_frame, text="开始答题",
command=self.start_exam, style="Large.TButton")
start_btn.grid(row=0, column=0, padx=10)
# 返回按钮
back_btn = ttk.Button(button_frame, text="返回",
command=self.go_back)
back_btn.grid(row=0, column=1, padx=10)
# 配置大按钮样式
style = ttk.Style()
style.configure("Large.TButton", font=("Arial", 12, "bold"))
# 绑定回车键
self.window.bind('<Return>', lambda e: self.start_exam())
# 设置焦点
self.count_entry.focus()
def center_window(self):
"""窗口居中显示"""
self.window.update_idletasks()
width = self.window.winfo_width()
height = self.window.winfo_height()
x = (self.window.winfo_screenwidth() // 2) - (width // 2)
y = (self.window.winfo_screenheight() // 2) - (height // 2)
self.window.geometry(f'{width}x{height}+{x}+{y}')
def start_exam(self):
"""开始答题"""
try:
count = int(self.count_var.get().strip())
if count < 1 or count > 50:
messagebox.showerror("错误", "题目数量应在1-50之间")
return
self.window.destroy()
# 打开答题窗口
exam_window = ExamWindow(self.email, self.level, count)
except ValueError:
messagebox.showerror("错误", "请输入有效的数字")
def go_back(self):
"""返回主窗口"""
self.window.destroy()
from ui.main_window import MainWindow
main_window = MainWindow(self.email)