Skip to content

Commit e98f587

Browse files
Merge branch 'dev' into main
Signed-off-by: Wilson.Huang <114924470+WilsonHuangDev@users.noreply.github.com>
2 parents d573761 + 9ae0be0 commit e98f587

13 files changed

Lines changed: 678 additions & 195 deletions

PasswdChanger/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from .passwd_changer import PasswordChanger
22
from .user_creator import UserCreator
33

4-
__all__ = ["PasswordChanger", "UserCreator"]
4+
__all__ = [
5+
"PasswordChanger",
6+
"UserCreator"
7+
]

PasswdChanger/passwd_changer.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import wx
22

3+
from modules.debug_window import DebugLogger
4+
from modules.window_manager import WindowManager
5+
36

47
class SecurePasswordTextCtrl(wx.TextCtrl):
58
def __init__(self, parent):
@@ -16,12 +19,13 @@ def on_key_down(self, event):
1619

1720

1821
class PasswordChanger(wx.Frame):
19-
def __init__(self, parent=None):
22+
def __init__(self):
2023
style = wx.CAPTION | wx.STAY_ON_TOP | wx.CLOSE_BOX
21-
super().__init__(parent, title="Windows 登录辅助工具", size=(400, 250), style=style) # 修复点:传递parent给父类
22-
self.parent = parent # 保存父窗口引用
24+
super().__init__(None, title="Windows 登录辅助工具", size=(320, 250), style=style)
25+
self.SetIcon(wx.Icon("Assets/icon.ico")) # 设置窗口图标
2326
self.init_ui()
2427
self.Center()
28+
DebugLogger.log("[DEBUG] PasswordChanger 初始化完成")
2529

2630
def init_ui(self):
2731
panel = wx.Panel(self)
@@ -40,23 +44,33 @@ def init_ui(self):
4044
grid.Add(self.confirm_pass, 0, wx.EXPAND)
4145

4246
btn_box = wx.BoxSizer(wx.HORIZONTAL)
43-
btn_change = wx.Button(panel, label="修改密码")
44-
btn_return = wx.Button(panel, label="返回")
47+
btn_change = wx.Button(panel, label="确认修改", size=(80, 30))
48+
btn_return = wx.Button(panel, label="返回", size=(80, 30))
4549
btn_change.Bind(wx.EVT_BUTTON, self.on_change)
4650
btn_return.Bind(wx.EVT_BUTTON, self.on_return)
4751
btn_box.Add(btn_change, 0, wx.RIGHT, 10)
4852
btn_box.Add(btn_return)
4953

54+
tittle = wx.StaticText(panel, label="修改用户密码", style=wx.ALIGN_CENTER)
55+
tittle_font = tittle.GetFont()
56+
tittle_font.SetPointSize(12) # 设置字体大小为12
57+
tittle_font.SetWeight(wx.FONTWEIGHT_BOLD) # 设置字体为粗体
58+
tittle.SetFont(tittle_font)
59+
5060
main_sizer = wx.BoxSizer(wx.VERTICAL)
61+
main_sizer.AddStretchSpacer(1)
62+
main_sizer.Add(tittle, 0, wx.ALIGN_CENTER)
63+
main_sizer.AddStretchSpacer(1)
5164
main_sizer.Add(grid, 1, wx.EXPAND | wx.ALL, 15)
65+
main_sizer.AddStretchSpacer(1)
5266
main_sizer.Add(btn_box, 0, wx.ALIGN_CENTER | wx.BOTTOM, 15)
5367

5468
panel.SetSizer(main_sizer)
5569

5670
def on_return(self, event):
57-
if self.parent: # 修改点4:直接使用保存的父窗口引用
58-
self.parent.Show()
59-
self.Destroy() # 修改点5:销毁当前窗口
71+
from modules.main_window import MainWindow
72+
DebugLogger.log("[DEBUG] 正在从修改用户密码窗口返回主窗口")
73+
WindowManager().switch_window(MainWindow)
6074

6175
def on_change(self, event):
6276
try:
@@ -72,26 +86,25 @@ def on_change(self, event):
7286

7387
# 调试信息已在CommandExecutor中处理
7488
if success:
75-
wx.MessageBox("密码修改成功", "成功", wx.OK | wx.ICON_INFORMATION)
89+
wx.MessageBox("密码修改成功!", "成功", wx.OK | wx.ICON_INFORMATION)
7690
else:
77-
wx.MessageBox(f"操作失败{msg}", "错误", wx.OK | wx.ICON_ERROR)
78-
except Exception as e:
79-
wx.MessageBox(f"系统错误{str(e)}", "错误", wx.OK | wx.ICON_ERROR)
91+
wx.MessageBox(f"[ERROR] 操作失败: {msg}", "错误", wx.OK | wx.ICON_ERROR)
92+
except (Exception, RuntimeError, NotImplementedError) as e:
93+
wx.MessageBox(f"[ERROR] 系统错误: {str(e)}", "错误", wx.OK | wx.ICON_ERROR)
8094
finally:
8195
self.new_pass.Value = ""
8296
self.confirm_pass.Value = ""
8397

8498
def validate_input(self, username, p1, p2):
8599
if not username:
86-
wx.MessageBox("用户名不能为空", "错误", wx.OK | wx.ICON_ERROR)
100+
wx.MessageBox("用户名不能为空!", "错误", wx.OK | wx.ICON_ERROR)
87101
return False
88102
if p1 != p2:
89-
wx.MessageBox("两次输入的密码不一致", "错误", wx.OK | wx.ICON_ERROR)
103+
wx.MessageBox("两次输入的密码不一致!", "错误", wx.OK | wx.ICON_ERROR)
90104
return False
91105
return True
92106

93107

94108
def run():
95109
app = wx.App()
96-
PasswordChanger().Show()
97110
app.MainLoop()

PasswdChanger/user_creator.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import wx
22

3+
from modules.debug_window import DebugLogger
4+
from modules.window_manager import WindowManager
5+
36

47
class UserCreator(wx.Frame):
5-
def __init__(self, parent=None): # 修复点:正确接收parent参数
8+
def __init__(self):
69
style = wx.CAPTION | wx.STAY_ON_TOP | wx.CLOSE_BOX
7-
super().__init__(parent, title="Windows 登录辅助工具", size=(400, 250), style=style) # 修复点:传递parent给父类
8-
self.parent = parent # 保存父窗口引用
10+
super().__init__(None, title="Windows 登录辅助工具", size=(320, 280), style=style)
11+
self.SetIcon(wx.Icon("Assets/icon.ico")) # 设置窗口图标
912
self.init_ui()
1013
self.Center()
14+
DebugLogger.log("[DEBUG] UserCreator 初始化完成")
1115

1216
def init_ui(self):
1317
panel = wx.Panel(self)
@@ -40,13 +44,23 @@ def init_ui(self):
4044

4145
# 按钮布局
4246
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
43-
btn_create = wx.Button(panel, label="创建用户")
44-
btn_return = wx.Button(panel, label="返回")
47+
btn_create = wx.Button(panel, label="创建用户", size=(80, 30))
48+
btn_return = wx.Button(panel, label="返回", size=(80, 30))
4549
btn_sizer.Add(btn_create, 0, wx.RIGHT, 10)
4650
btn_sizer.Add(btn_return)
4751

52+
tittle = wx.StaticText(panel, label="创建用户", style=wx.ALIGN_CENTER)
53+
tittle_font = tittle.GetFont()
54+
tittle_font.SetPointSize(12) # 设置字体大小为12
55+
tittle_font.SetWeight(wx.FONTWEIGHT_BOLD) # 设置字体为粗体
56+
tittle.SetFont(tittle_font)
57+
4858
main_sizer = wx.BoxSizer(wx.VERTICAL)
59+
main_sizer.AddStretchSpacer(1)
60+
main_sizer.Add(tittle, 0, wx.ALIGN_CENTER)
61+
main_sizer.AddStretchSpacer(1)
4962
main_sizer.Add(sizer, 1, flag=wx.ALL | wx.EXPAND, border=15)
63+
main_sizer.AddStretchSpacer(1)
5064
main_sizer.Add(btn_sizer, flag=wx.ALIGN_CENTER | wx.BOTTOM, border=15)
5165

5266
panel.SetSizer(main_sizer)
@@ -84,20 +98,20 @@ def on_create(self, event):
8498
if not success:
8599
raise Exception(msg)
86100

87-
wx.MessageBox("用户创建成功", "成功", wx.OK | wx.ICON_INFORMATION)
88-
except Exception as e:
89-
wx.MessageBox(f"创建失败{str(e)}", "错误", wx.OK | wx.ICON_ERROR)
101+
wx.MessageBox("用户创建成功!", "成功", wx.OK | wx.ICON_INFORMATION)
102+
except (Exception, RuntimeError, NotImplementedError) as e:
103+
wx.MessageBox(f"[ERROR] 创建失败: {str(e)}", "错误", wx.OK | wx.ICON_ERROR)
90104

91105
def validate_input(self, user, p1, p2):
92106
if not user:
93-
wx.MessageBox("用户名不能为空", "错误", wx.OK | wx.ICON_ERROR)
107+
wx.MessageBox("用户名不能为空!", "错误", wx.OK | wx.ICON_ERROR)
94108
return False
95109
if p1 != p2:
96-
wx.MessageBox("两次密码输入不一致", "错误", wx.OK | wx.ICON_ERROR)
110+
wx.MessageBox("两次密码输入不一致!", "错误", wx.OK | wx.ICON_ERROR)
97111
return False
98112
return True
99113

100114
def on_return(self, event):
101-
if self.parent: # 修改点7:直接使用保存的父窗口引用
102-
self.parent.Show()
103-
self.Destroy() # 修改点8:销毁当前窗口
115+
from modules.main_window import MainWindow
116+
DebugLogger.log("[DEBUG] 正在从创建用户窗口返回主窗口")
117+
WindowManager().switch_window(MainWindow)

main.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import ctypes
22
import sys
33
import traceback
4+
import time
45

56
import wx
67

7-
from modules import ConfigManager
88
from modules.debug_window import DebugLogger
9+
from modules.window_manager import WindowManager
910

1011

1112
class ProcessManager:
@@ -26,46 +27,48 @@ def require_admin():
2627
)
2728

2829
if ret <= 32:
29-
print(f"提权失败,错误代码: {ret}")
30+
print(f"[ERROR] 提权失败: {ret}")
3031
return False
3132

3233
sys.exit(0)
3334
except Exception as e:
34-
print(f"权限请求失败: {str(e)}")
35-
input("按回车退出...")
35+
print(f"[ERROR] 权限请求失败: {str(e)}")
36+
print("程序将在 10 秒后自动退出...")
37+
time.sleep(10)
3638
sys.exit(1)
3739

3840
@staticmethod
3941
def main_loop():
4042
try:
41-
# 确保只创建一个 wx.App 实例
42-
app = wx.App() if not wx.GetApp() else wx.GetApp()
43+
# 确保单例App实例
44+
if not wx.GetApp():
45+
app = wx.App()
46+
else:
47+
app = wx.GetApp()
4348

44-
# 加载配置
45-
config = ConfigManager.get_config()
46-
debug_mode = config.get('debug_mode', 0) == 1
49+
app.SetExitOnFrameDelete(False) # 防止最后一个窗口关闭时退出
4750

48-
# 设置调试模式(关键修复点)
49-
DebugLogger.set_debug_mode(debug_mode) # 使用正确的类方法
5051
logger = DebugLogger() # 实例化(此时才会创建进程)
5152

52-
# 创建主窗口或登录窗口
53-
if config.get('auth_mode', 0) == 0:
54-
from modules.main_window import MainWindow
55-
frame = MainWindow()
56-
else:
57-
from modules.login_window import LoginWindow
58-
frame = LoginWindow()
53+
# 初始化窗口管理器
54+
from modules.login_window import LoginWindow
55+
56+
# 首次窗口显示
57+
if not WindowManager().current_window:
58+
WindowManager().switch_window(LoginWindow)
5959

60-
frame.Show()
6160
app.MainLoop()
6261

6362
except Exception as e:
6463
traceback.print_exc()
65-
input("发生错误,按回车退出...")
64+
print("程序将在 10 秒后自动退出...")
65+
time.sleep(10)
66+
sys.exit(1)
6667

6768

6869
if __name__ == "__main__":
70+
DebugLogger.log("[DEBUG] 程序开始运行")
6971
if not ProcessManager.require_admin():
7072
sys.exit(1)
7173
ProcessManager.main_loop()
74+
DebugLogger.log("[DEBUG] 程序结束")

modules/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from .main_window import MainWindow
66
from .login_window import LoginWindow
77
from .debug_window import DebugLogger
8+
from .power_options import PowerOptionsWindow
9+
from .window_manager import WindowManager
810

911
# 声明导出列表
1012
__all__ = [
@@ -13,5 +15,7 @@
1315
"CommandExecutor",
1416
"MainWindow",
1517
"LoginWindow",
16-
"DebugLogger"
18+
"DebugLogger",
19+
"PowerOptionsWindow",
20+
"WindowManager"
1721
]

modules/cmd_executor.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@
44

55

66
class CommandExecutor:
7-
DEBUG_MODE = 0
8-
9-
@classmethod
10-
def set_debug_mode(cls, debug_mode):
11-
cls.DEBUG_MODE = debug_mode
12-
137
@classmethod
148
def run_as_admin(cls, command: list, encoding: str = "gbk"):
159
try:
16-
if cls.DEBUG_MODE:
17-
DebugLogger.log(f"执行命令: {' '.join(command)}")
10+
DebugLogger.log(f"[DEBUG] 执行命令: {' '.join(command)}")
1811

1912
result = subprocess.run(
2013
command,
@@ -26,19 +19,16 @@ def run_as_admin(cls, command: list, encoding: str = "gbk"):
2619
encoding=encoding
2720
)
2821

29-
if cls.DEBUG_MODE:
30-
if result.stdout.strip():
31-
DebugLogger.log(f"命令输出: {result.stdout.strip()}")
32-
if result.stderr.strip():
33-
DebugLogger.log(f"命令错误: {result.stderr.strip()}")
22+
if result.stdout.strip():
23+
DebugLogger.log(f"[DEBUG] 命令输出: {result.stdout.strip()}")
24+
if result.stderr.strip():
25+
DebugLogger.log(f"[ERROR] 命令错误: {result.stderr.strip()}")
3426

3527
return True, result.stdout.strip()
3628
except subprocess.CalledProcessError as e:
3729
error_msg = e.stderr.strip() or "Unknown error"
38-
if cls.DEBUG_MODE:
39-
DebugLogger.log(f"命令失败: {error_msg}")
30+
DebugLogger.log(f"[ERROR] 命令失败: {error_msg}")
4031
return False, error_msg
41-
except Exception as e:
42-
if cls.DEBUG_MODE:
43-
DebugLogger.log(f"系统错误: {str(e)}")
32+
except (Exception, RuntimeError, NotImplementedError) as e:
33+
DebugLogger.log(f"[ERROR] 系统错误: {str(e)}")
4434
return False, str(e)

0 commit comments

Comments
 (0)