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/password_setup_window.py

110 lines
3.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.

#!/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 user_manager import UserManager
from ui.login_window import LoginWindow
class PasswordSetupWindow:
"""密码设置窗口类"""
def __init__(self, email):
self.email = email
self.user_manager = UserManager()
self.setup_ui()
def setup_ui(self):
"""设置UI界面"""
self.window = tk.Tk()
self.window.title("数学学习软件 - 设置密码")
self.window.geometry("400x250")
self.window.resizable(False, False)
# 居中显示
self.center_window()
# 创建主框架
main_frame = ttk.Frame(self.window, padding="20")
main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# 标题
title_label = ttk.Label(main_frame, text="设置密码", font=("Arial", 16, "bold"))
title_label.grid(row=0, column=0, columnspan=2, pady=(0, 20))
# 密码要求说明
info_text = "密码要求6-10位包含大小写字母和数字"
info_label = ttk.Label(main_frame, text=info_text, font=("Arial", 9), foreground="gray")
info_label.grid(row=1, column=0, columnspan=2, pady=(0, 10))
# 密码输入
ttk.Label(main_frame, text="密码:").grid(row=2, column=0, sticky=tk.W, pady=5)
self.password_var = tk.StringVar()
self.password_entry = ttk.Entry(main_frame, textvariable=self.password_var,
show="*", width=30)
self.password_entry.grid(row=2, column=1, pady=5, padx=(10, 0))
# 确认密码输入
ttk.Label(main_frame, text="确认密码:").grid(row=3, column=0, sticky=tk.W, pady=5)
self.confirm_password_var = tk.StringVar()
self.confirm_password_entry = ttk.Entry(main_frame, textvariable=self.confirm_password_var,
show="*", width=30)
self.confirm_password_entry.grid(row=3, column=1, pady=5, padx=(10, 0))
# 按钮框架
button_frame = ttk.Frame(main_frame)
button_frame.grid(row=4, column=0, columnspan=2, pady=20)
# 设置密码按钮
setup_btn = ttk.Button(button_frame, text="设置密码", command=self.setup_password)
setup_btn.grid(row=0, column=0, padx=5)
# 绑定回车键
self.window.bind('<Return>', lambda e: self.setup_password())
# 设置焦点
self.password_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 setup_password(self):
"""设置密码"""
password = self.password_var.get().strip()
confirm_password = self.confirm_password_var.get().strip()
if not password or not confirm_password:
messagebox.showerror("错误", "请输入密码和确认密码")
return
if password != confirm_password:
messagebox.showerror("错误", "两次输入的密码不一致")
return
# 注册用户
success, message = self.user_manager.register_user(self.email, password)
if success:
messagebox.showinfo("成功", message)
self.window.destroy()
# 返回登录窗口
login_window = LoginWindow()
else:
messagebox.showerror("错误", message)