简化代码,解决程序图标问题

This commit is contained in:
yqsphp
2026-01-13 15:23:02 +08:00
parent 7f75aaac10
commit e66c610b65
6 changed files with 7292 additions and 119 deletions

BIN
icons/icon.icns Normal file

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 104 KiB

After

Width:  |  Height:  |  Size: 104 KiB

BIN
icons/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

7231
resources.py Normal file

File diff suppressed because it is too large Load Diff

8
resources.qrc Normal file
View File

@@ -0,0 +1,8 @@
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource>
<file>icons/icon.ico</file>
<file>icons/icon.png</file>
<file>icons/icon.icns</file>
</qresource>
</RCC>

View File

@@ -2,18 +2,56 @@ import sys
import os
import threading
import socket
from datetime import datetime
import resources
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QGroupBox, QFrame, QPushButton, \
QListWidget, QSlider, QTextEdit, QFileDialog, QMessageBox, QListWidgetItem
QListWidget, QSlider, QFileDialog, QMessageBox, QListWidgetItem, QApplication
from core.device_discovery import DeviceDiscovery
from core.dlna_controller import DLNAController
from core.http_file_server import HTTPFileServer
from core.logger import AppLogger
class IconManager:
"""图标管理器处理Qt资源系统的图标加载"""
@staticmethod
def get_icon(icon_name="icon"):
"""
从Qt资源系统加载图标
支持格式优先级:
1. 根据平台自动选择格式
2. 使用资源别名
3. 使用完整路径
"""
# 根据平台选择图标格式
platform = sys.platform
if platform == "win32":
extensions = [".ico", ".png"]
elif platform == "darwin": # macOS
extensions = [".icns", ".png"]
else: # Linux和其他
extensions = [".png", ".svg"]
# 尝试不同的路径格式
icon = QIcon()
for ext in extensions:
resource_path = f":/icons/{icon_name}{ext}"
icon = QIcon(resource_path)
if not icon.isNull():
print(f"✓ 从资源加载: {resource_path}")
return icon
# 如果都没找到,创建空图标
if icon.isNull():
print("⚠ 无法从资源加载图标,使用默认图标")
# 可以使用Qt内置图标作为备选
icon = QIcon.fromTheme("application-x-executable")
return icon
class MainWindow(QMainWindow):
"""智能媒体投屏器主窗口"""
@@ -36,25 +74,18 @@ class MainWindow(QMainWindow):
"""初始化UI界面"""
version = "v1.0.2"
app_name = "多媒体投屏 by yqsphp"
icon = "icon.ico"
self.setWindowTitle(app_name)
self.setGeometry(100, 100, 700, 500)
# 获取图标路径(支持打包和开发环境)
if getattr(sys, 'frozen', False):
# pyInstaller打包后的exe环境
# base_path = sys._MEIPASS
# Nuitka打包后
base_path = os.path.dirname(sys.executable)
else:
# 开发环境
base_path = os.path.abspath(".")
# 1. 加载主图标
icon = IconManager.get_icon()
icon_path = os.path.join(base_path, icon)
# 2. 设置窗口图标(标题栏)
self.setWindowIcon(icon)
# 设置应用程序图标(影响任务栏)
self.setWindowIcon(QIcon(icon_path))
# 3. 设置应用程序图标(任务栏)
QApplication.instance().setWindowIcon(icon)
# Windows专用设置AppUserModelID非常重要
if sys.platform == "win32":
@@ -69,33 +100,23 @@ class MainWindow(QMainWindow):
# 创建中央部件
central_widget = QWidget()
self.setCentralWidget(central_widget)
# 主布局
main_layout = QVBoxLayout(central_widget)
main_layout.setSpacing(10)
main_layout.setContentsMargins(10, 10, 10, 10)
# 水平布局用于左右面板
horizontal_layout = QHBoxLayout()
horizontal_layout.setSpacing(10)
# 左侧控制面板
left_panel = self.create_left_panel()
left_panel = self.create_panel()
horizontal_layout.addWidget(left_panel, 1)
# 右侧信息面板
# right_panel = self.create_right_panel()
# horizontal_layout.addWidget(right_panel, 1)
# 将水平布局添加到垂直布局中
main_layout.addLayout(horizontal_layout)
# 状态栏
self.statusBar().showMessage("准备就绪")
# 添加弹性空间
main_layout.addStretch()
# 底部版权信息
copyright_label = QLabel(f"© {app_name} {version}")
copyright_label.setAlignment(Qt.AlignCenter)
@@ -107,7 +128,6 @@ class MainWindow(QMainWindow):
margin-top: 10px;
""")
main_layout.addWidget(copyright_label)
# 设置扁平化样式
self.setStyleSheet("""
/* 主窗口 */
@@ -227,7 +247,7 @@ class MainWindow(QMainWindow):
}
""")
def create_left_panel(self):
def create_panel(self):
"""创建左侧控制面板(文件和设备控制合并)"""
panel = QWidget()
layout = QVBoxLayout(panel)
@@ -308,7 +328,7 @@ class MainWindow(QMainWindow):
# 设备列表
self.device_list = QListWidget()
self.device_list.itemClicked.connect(self.select_device_from_list)
self.device_list.itemClicked.connect(self.select_device)
self.device_list.setMinimumHeight(100)
device_layout.addWidget(self.device_list)
@@ -378,84 +398,6 @@ class MainWindow(QMainWindow):
layout.addStretch()
return panel
def create_right_panel(self):
"""创建右侧信息面板"""
panel = QWidget()
layout = QVBoxLayout(panel)
layout.setSpacing(10)
# ==================== 系统信息区域 ====================
system_group = QGroupBox("系统信息")
system_layout = QVBoxLayout()
system_layout.setSpacing(8)
# 获取本机IP
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
local_ip = s.getsockname()[0]
s.close()
except:
local_ip = "获取失败"
# 服务器状态
server_frame = QFrame()
server_layout = QHBoxLayout(server_frame)
server_layout.addWidget(QLabel("服务地址:"))
self.ip_label = QLabel(f"{local_ip}:{self.http_port}")
server_layout.addWidget(self.ip_label, 1)
system_layout.addWidget(server_frame)
# 文件服务状态
service_frame = QFrame()
service_layout = QHBoxLayout(service_frame)
service_layout.addWidget(QLabel("文件服务:"))
self.file_service_label = QLabel("就绪")
self.file_service_label.setStyleSheet("color: #28a745;")
service_layout.addWidget(self.file_service_label, 1)
system_layout.addWidget(service_frame)
system_group.setLayout(system_layout)
layout.addWidget(system_group)
# ==================== 操作日志区域 ====================
log_group = QGroupBox("操作日志")
log_layout = QVBoxLayout()
log_layout.setSpacing(2)
self.log_text = QTextEdit()
self.log_text.setReadOnly(True)
self.log_text.setMinimumHeight(250)
# 添加初始日志
self.log_message("系统", "智能媒体投屏器已启动")
self.log_message("系统", f"本地IP地址: {local_ip}")
log_layout.addWidget(self.log_text, 1)
# 日志控制按钮
log_btn_layout = QHBoxLayout()
log_btn_layout.setSpacing(8)
clear_log_btn = QPushButton("清空日志")
clear_log_btn.clicked.connect(self.log_text.clear)
log_btn_layout.addWidget(clear_log_btn)
log_btn_layout.addStretch()
log_layout.addLayout(log_btn_layout)
log_group.setLayout(log_layout)
layout.addWidget(log_group, 1) # 日志区域占据更多空间
layout.addStretch()
return panel
def init_connections(self):
"""初始化信号连接"""
# 设备发现
@@ -502,15 +444,13 @@ class MainWindow(QMainWindow):
self.device_discovery = DeviceDiscovery()
# 获取刷新后设备信息
new_device = self.device_discovery.discover_media_renderers()
self.update_device_list(new_device, self.selected_device)
# 搜索完成后启用按钮
self.enable_refresh_button()
self.update_device(new_device, self.selected_device)
# 确保在UI线程中更新
thread = threading.Thread(target=do_quick_discovery, daemon=True)
thread.start()
def update_device_list(self, new_devices, current_selected_device=None):
def update_device(self, new_devices, current_selected_device=None):
"""
更新设备列表显示保留选中状态使用UDN作为唯一标识
Args:
@@ -602,8 +542,6 @@ class MainWindow(QMainWindow):
# 检查是否是之前选中的设备
if current_selected_udn and device.get('udn') == current_selected_udn:
current_selected_item = found_item
elif current_selected_device and device.get('ip') == current_selected_device.get('ip'):
current_selected_item = found_item
else:
# 设备不存在,添加新项目
item = QListWidgetItem(item_text)
@@ -613,8 +551,6 @@ class MainWindow(QMainWindow):
# 检查是否是之前选中的设备
if current_selected_udn and device.get('udn') == current_selected_udn:
current_selected_item = item
elif current_selected_device and device.get('ip') == current_selected_device.get('ip'):
current_selected_item = item
# 恢复选中状态
if current_selected_item:
@@ -638,15 +574,13 @@ class MainWindow(QMainWindow):
# 如果有选中的设备,启用控制按钮
if self.selected_device:
self.enable_control_buttons(True)
device_name = self.selected_device.get('friendly_name', '未知设备')
self.log_message("设备选择", f"当前选中: {device_name}")
else:
self.log_message("设备发现", "未发现任何投屏设备", "warning")
self.selected_device = None
self.enable_control_buttons(False)
def select_device_from_list(self, item):
def select_device(self, item):
"""从列表中选择设备"""
device = item.data(Qt.UserRole)
if device: