feat: 完成PyQt5 GUI实现,支持系统托盘和图标显示

1. 实现了基于PyQt5的GUI界面 2. 添加系统托盘功能,支持最小化到托盘 3. 修复了图标显示问题,包括窗口图标和任务栏图标 4. 优化了打包配置,支持PyInstaller打包 5. 版本更新到v3.3.1
This commit is contained in:
huangzhenpc
2025-02-12 14:18:08 +08:00
parent 56b619c4dc
commit e3058b9e39
8 changed files with 86 additions and 17 deletions

View File

@@ -22,7 +22,7 @@ a = Analysis(
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
excludes=['_tkinter', 'tkinter', 'Tkinter'], # 排除 tkinter 相关模块
noarchive=False,
optimize=0,
)

View File

@@ -5,7 +5,7 @@ import os
from PIL import Image
from PyQt5.QtWidgets import (QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QLineEdit, QPushButton, QFrame, QTextEdit,
QMessageBox, QApplication)
QMessageBox, QApplication, QSystemTrayIcon, QMenu)
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QIcon, QPixmap
@@ -34,17 +34,34 @@ class MainWindow(QMainWindow):
self.setWindowTitle(f"听泉Cursor助手 v{version} (本机Cursor版本: {cursor_version})")
self.setMinimumSize(600, 500)
# 设置图标
try:
current_dir = os.path.dirname(os.path.abspath(__file__))
icon_path = os.path.join(os.path.dirname(current_dir), "icon", "th.jpg")
if os.path.exists(icon_path):
self.setWindowIcon(QIcon(icon_path))
logging.info(f"成功加载图标: {icon_path}")
else:
logging.error(f"图标文件不存在: {icon_path}")
except Exception as e:
logging.error(f"设置图标失败: {str(e)}")
# 设置窗口图标
icon_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "icon", "two.ico")
if os.path.exists(icon_path):
window_icon = QIcon(icon_path)
if not window_icon.isNull():
self.setWindowIcon(window_icon)
logging.info(f"成功设置窗口图标: {icon_path}")
# 创建系统托盘图标
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(self.windowIcon())
self.tray_icon.setToolTip("听泉Cursor助手")
# 创建托盘菜单
tray_menu = QMenu()
show_action = tray_menu.addAction("显示主窗口")
show_action.triggered.connect(self.show)
quit_action = tray_menu.addAction("退出")
quit_action.triggered.connect(QApplication.instance().quit)
# 设置托盘菜单
self.tray_icon.setContextMenu(tray_menu)
# 连接托盘图标的信号
self.tray_icon.activated.connect(self.on_tray_icon_activated)
# 显示托盘图标
self.tray_icon.show()
# 创建主窗口部件
central_widget = QWidget()
@@ -107,6 +124,26 @@ class MainWindow(QMainWindow):
# 启动时检查一次状态
QTimer.singleShot(0, self.check_status)
def on_tray_icon_activated(self, reason):
"""处理托盘图标的点击事件"""
if reason == QSystemTrayIcon.DoubleClick:
self.show()
self.activateWindow()
def closeEvent(self, event):
"""重写关闭事件,最小化到托盘而不是退出"""
if hasattr(self, 'tray_icon') and self.tray_icon.isVisible():
event.ignore()
self.hide()
self.tray_icon.showMessage(
"听泉Cursor助手",
"程序已最小化到系统托盘",
QSystemTrayIcon.Information,
2000
)
else:
event.accept()
def copy_device_id(self):
"""复制设备ID到剪贴板"""
if not self.check_status():

BIN
icon/logo1.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

BIN
icon/logo2.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
icon/logo3.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

29
main.py
View File

@@ -6,7 +6,9 @@ import atexit
import shutil
import tempfile
from pathlib import Path
from PyQt5.QtWidgets import QApplication, QMessageBox
from PyQt5.QtWidgets import QApplication, QMessageBox, QSystemTrayIcon, QMenu
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
from gui.main_window import MainWindow
def cleanup_temp():
@@ -65,7 +67,32 @@ def main():
logging.info("正在初始化主窗口...")
app = QApplication(sys.argv)
# 设置应用程序ID (在设置图标之前)
if sys.platform == "win32":
import ctypes
myappid = u'nezha.cursor.helper.v3'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
logging.info(f"设置应用程序ID: {myappid}")
# 设置应用程序图标
try:
icon_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "icon", "two.ico")
if os.path.exists(icon_path):
app_icon = QIcon(icon_path)
if not app_icon.isNull():
app.setWindowIcon(app_icon)
logging.info(f"成功设置应用程序图标: {icon_path}")
else:
logging.error("图标文件加载失败")
else:
logging.error(f"图标文件不存在: {icon_path}")
except Exception as e:
logging.error(f"设置应用程序图标失败: {str(e)}")
window = MainWindow()
window.setWindowIcon(app.windowIcon()) # 确保窗口使用相同的图标
logging.info("正在启动主窗口...")
window.show()

View File

@@ -1,4 +1,9 @@
# Use Tsinghua mirror for faster download in China:
# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
requests==2.31.0
pyinstaller==6.3.0
pillow==10.2.0 # 用于处理图标
setuptools==65.5.1 # 解决pkg_resources.extern问题
pillow==10.2.0 # For icon processing
setuptools==65.5.1 # Fix pkg_resources.extern issue
PyQt5==5.15.10 # GUI framework
pywin32==306 # Windows API support

View File

@@ -1 +1 @@
3.2.8
3.3.1