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.
math_test/email_config_gui.py

180 lines
6.6 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.

"""
邮箱配置界面
用于设置SMTP发送邮箱
"""
import tkinter as tk
from tkinter import ttk, messagebox
from email_config import DEFAULT_SENDER_CONFIG, EMAIL_HELP_TEXT
import json
import os
class EmailConfigWindow:
"""邮箱配置窗口"""
def __init__(self, parent, data_manager):
self.parent = parent
self.data_manager = data_manager
self.config_file = "email_config.json"
# 创建配置窗口
self.window = tk.Toplevel(parent)
self.window.title("邮箱配置")
self.window.geometry("500x400")
self.window.resizable(False, False)
self.window.grab_set() # 模态窗口
self.create_widgets()
self.load_config()
def create_widgets(self):
"""创建界面组件"""
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=10)
# 说明
info_label = ttk.Label(main_frame,
text="配置用于发送验证码的邮箱建议使用QQ邮箱",
font=("Arial", 10))
info_label.grid(row=1, column=0, columnspan=2, pady=5)
# 发送邮箱
ttk.Label(main_frame, text="发送邮箱:").grid(row=2, column=0, sticky=tk.W, pady=5)
self.email_entry = ttk.Entry(main_frame, width=30)
self.email_entry.grid(row=2, column=1, padx=10, pady=5)
# 授权码
ttk.Label(main_frame, text="授权码:").grid(row=3, column=0, sticky=tk.W, pady=5)
self.password_entry = ttk.Entry(main_frame, width=30, show="*")
self.password_entry.grid(row=3, column=1, padx=10, pady=5)
# 发送者名称
ttk.Label(main_frame, text="发送者名称:").grid(row=4, column=0, sticky=tk.W, pady=5)
self.name_entry = ttk.Entry(main_frame, width=30)
self.name_entry.grid(row=4, column=1, padx=10, pady=5)
# 帮助按钮
help_btn = ttk.Button(main_frame, text="获取授权码帮助",
command=self.show_help)
help_btn.grid(row=5, column=0, columnspan=2, pady=10)
# 测试按钮
test_btn = ttk.Button(main_frame, text="测试邮箱配置",
command=self.test_email)
test_btn.grid(row=6, column=0, columnspan=2, pady=5)
# 按钮框架
button_frame = ttk.Frame(main_frame)
button_frame.grid(row=7, column=0, columnspan=2, pady=20)
# 保存按钮
save_btn = ttk.Button(button_frame, text="保存配置",
command=self.save_config)
save_btn.grid(row=0, column=0, padx=10)
# 取消按钮
cancel_btn = ttk.Button(button_frame, text="取消",
command=self.window.destroy)
cancel_btn.grid(row=0, column=1, padx=10)
def load_config(self):
"""加载配置"""
try:
if os.path.exists(self.config_file):
with open(self.config_file, 'r', encoding='utf-8') as f:
config = json.load(f)
self.email_entry.insert(0, config.get("email", ""))
self.password_entry.insert(0, config.get("password", ""))
self.name_entry.insert(0, config.get("name", "数学学习软件"))
else:
# 使用默认配置
self.name_entry.insert(0, DEFAULT_SENDER_CONFIG["name"])
except Exception as e:
print(f"加载配置失败: {e}")
def save_config(self):
"""保存配置"""
email = self.email_entry.get().strip()
password = self.password_entry.get().strip()
name = self.name_entry.get().strip()
if not email or not password:
messagebox.showerror("错误", "请填写邮箱和授权码")
return
if "@" not in email:
messagebox.showerror("错误", "请输入有效的邮箱地址")
return
if not name:
name = "数学学习软件"
config = {
"email": email,
"password": password,
"name": name
}
try:
# 保存到文件
with open(self.config_file, 'w', encoding='utf-8') as f:
json.dump(config, f, ensure_ascii=False, indent=2)
# 更新数据管理器配置
self.data_manager.sender_config = config
messagebox.showinfo("成功", "邮箱配置保存成功!")
self.window.destroy()
except Exception as e:
messagebox.showerror("错误", f"保存配置失败: {e}")
def test_email(self):
"""测试邮箱配置"""
email = self.email_entry.get().strip()
password = self.password_entry.get().strip()
name = self.name_entry.get().strip()
if not email or not password:
messagebox.showerror("错误", "请填写邮箱和授权码")
return
# 临时更新配置
old_config = self.data_manager.sender_config.copy()
self.data_manager.sender_config = {
"email": email,
"password": password,
"name": name or "数学学习软件"
}
# 发送测试邮件
test_code = "123456"
success = self.data_manager.send_verification_email(email, test_code)
# 恢复原配置
self.data_manager.sender_config = old_config
if success:
messagebox.showinfo("测试成功", f"测试邮件已发送到 {email}")
else:
messagebox.showerror("测试失败", "邮件发送失败,请检查配置")
def show_help(self):
"""显示帮助信息"""
help_window = tk.Toplevel(self.window)
help_window.title("授权码获取帮助")
help_window.geometry("600x500")
help_window.resizable(False, False)
text_widget = tk.Text(help_window, wrap=tk.WORD, padx=10, pady=10)
text_widget.pack(fill=tk.BOTH, expand=True)
text_widget.insert(tk.END, EMAIL_HELP_TEXT)
text_widget.config(state=tk.DISABLED)
# 关闭按钮
close_btn = ttk.Button(help_window, text="关闭",
command=help_window.destroy)
close_btn.pack(pady=10)