Files
playwrite/gui/main_window.py
Misaka 3b7c00377f style: format all Python files with Black
Apply Black formatter to the entire codebase for consistent code style.

Co-Authored-By: Claude (glm-5) <noreply@anthropic.com>
2026-02-26 22:44:03 +08:00

199 lines
7.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
主窗口
ERP 自动化工具的主窗口,包含多个功能标签页。
"""
import tkinter as tk
from tkinter import ttk
from gui.config_manager import ConfigManager
from gui.log_config import setup_gui_logging
from gui.data_extraction_tab import DataExtractionTab
from gui.material_validation_tab import MaterialValidationTab
from gui.settings_tab import SettingsTab
class MainWindow:
"""主窗口类"""
def __init__(self, root: tk.Tk, session_manager):
"""
初始化主窗口
Args:
root: Tk 根窗口
session_manager: SessionManager 实例,用于用户认证和权限管理
"""
self.root = root
self.config = ConfigManager()
self.session_manager = session_manager
self.shared_production_ids = [] # 共享的 Production ID 列表
# 初始化统一日志系统
setup_gui_logging()
# 设置窗口属性(包含用户信息)
user_type_display = "管理员" if session_manager.is_admin() else "用户"
self.root.title(
f"ERP 自动化工具 v1.0 - {session_manager.get_username()} ({user_type_display})"
)
self.root.geometry("1000x700")
# 设置最小窗口大小
self.root.minsize(800, 600)
# 创建菜单栏
self.create_menu()
# 创建主界面
self.create_widgets()
# 创建状态栏
self.create_status_bar()
# 居中窗口
self._center_window()
def get_shared_production_ids(self) -> list:
"""获取共享的 Production ID 列表"""
return self.shared_production_ids.copy()
def update_shared_production_ids(self, production_ids: list):
"""更新共享的 Production ID 列表"""
self.shared_production_ids = production_ids
# 通知物料校验标签页 Production ID 已更新
if hasattr(self, "validation_tab"):
if hasattr(self.validation_tab, "on_production_ids_updated"):
self.validation_tab.on_production_ids_updated(production_ids)
def create_menu(self):
"""创建菜单栏"""
menubar = tk.Menu(self.root)
self.root.config(menu=menubar)
# 文件菜单
file_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="文件", menu=file_menu)
file_menu.add_command(label="退出", command=self.root.quit)
# 帮助菜单
help_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="帮助", menu=help_menu)
help_menu.add_command(label="关于", command=self.show_about)
def create_widgets(self):
"""创建主界面组件"""
# 创建 Notebook标签页容器
self.notebook = ttk.Notebook(self.root)
self.notebook.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# 数据提取标签页(传递 self 以支持共享 Production ID
self.extraction_tab = DataExtractionTab(self.notebook, self.config, self)
self.notebook.add(self.extraction_tab, text="数据提取")
# 物料校验标签页(传入 session_manager 和 main_window
self.validation_tab = MaterialValidationTab(
self.notebook, self.config, self.session_manager, self
)
self.notebook.add(self.validation_tab, text="物料校验")
# 设置标签页(传入 session_manager
self.settings_tab = SettingsTab(
self.notebook, self.config, self.session_manager
)
self.notebook.add(self.settings_tab, text="设置")
# 初始化:如果数据提取页面已有 Production ID通知物料校验页面
self._initialize_shared_production_ids()
def _initialize_shared_production_ids(self):
"""初始化共享的 Production ID从数据提取页面获取"""
try:
if hasattr(self.extraction_tab, "production_id_input"):
production_ids = self.extraction_tab.production_id_input.get()
if production_ids:
self.update_shared_production_ids(production_ids)
except Exception:
pass # 如果获取失败,忽略错误
def create_status_bar(self):
"""创建状态栏"""
self.status_bar = ttk.Frame(self.root, relief=tk.SUNKEN)
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
# 状态文本
self.status_text = tk.StringVar()
self.status_text.set("就绪")
status_label = ttk.Label(
self.status_bar, textvariable=self.status_text, anchor=tk.W
)
status_label.pack(side=tk.LEFT, padx=5)
# 用户信息显示
user_type_display = "管理员" if self.session_manager.is_admin() else "用户"
original_admin = self.session_manager.get_original_admin()
if original_admin:
# Admin以其他用户身份操作
user_info_text = f"当前用户: {self.session_manager.get_username()} ({user_type_display}) - 以 {original_admin['username']} 身份登录"
else:
# 正常登录
user_info_text = (
f"当前用户: {self.session_manager.get_username()} ({user_type_display})"
)
self.user_info_var = tk.StringVar()
self.user_info_var.set(user_info_text)
user_label = ttk.Label(
self.status_bar, textvariable=self.user_info_var, anchor=tk.E
)
user_label.pack(side=tk.RIGHT, padx=5)
# 配置状态指示
self.config_status = tk.StringVar()
self.config_status.set("配置已加载")
config_label = ttk.Label(
self.status_bar, textvariable=self.config_status, anchor=tk.E
)
config_label.pack(side=tk.RIGHT, padx=5)
def _center_window(self):
"""将窗口居中显示"""
self.root.update_idletasks()
width = self.root.winfo_width()
height = self.root.winfo_height()
x = (self.root.winfo_screenwidth() // 2) - (width // 2)
y = (self.root.winfo_screenheight() // 2) - (height // 2)
self.root.geometry(f"{width}x{height}+{x}+{y}")
def show_about(self):
"""显示关于对话框"""
from tkinter import messagebox
messagebox.showinfo(
"关于 ERP 自动化工具",
"ERP 自动化工具 v1.0\n\n"
"功能:\n"
"• 数据提取 - 从 ERP 系统提取备料计划数据\n"
"• 物料校验 - 校验物料状态并匹配待删除物料\n"
"• 设置管理 - 管理系统配置\n"
"• 数据库持久化 - 将提取的数据自动保存到数据库\n\n"
"基于 Playwright 和 Python 开发",
)
def reload_config(self):
"""配置更新后重新加载配置到各个标签页"""
# 重新加载配置
self.config.reload()
# 通知各个标签页重新加载配置
if hasattr(self.extraction_tab, "reload_config"):
self.extraction_tab.reload_config()
if hasattr(self.validation_tab, "reload_config"):
self.validation_tab.reload_config()
# 更新状态栏
self.config_status.set("配置已重新加载")