Windows息屏控制

查看 9|回复 0
作者:TinPandora   
新手尝试用AI做了个控制屏幕息屏的Python程序,满足我出门在外能快速息屏,坐在电脑前无需息屏的需求。
只是时间有限加之还有其他事情,没法把bug改完:
①应用设置的开机自启动无效,哪怕管理员模式启动;
②立即息屏快捷键在输入完成后,需要点一下其他按钮才能正常使用自定义好的快捷键。
针对自启无效,我用了个之前会的办法曲线解决:WIN+R,输入shell:startup,把软件的快捷方式放进去,完成。
另外,我是用nuitka打包的,因为我环境中的pyinstaller不知道出了啥问题,把报错丢AI+重装pyinstaller也没用,就不想再花时间弄,之后有空再重新整理下环境试试。
提供一条打包示例:[PowerShell] 纯文本查看 复制代码c:\Users\Administrator\Desktop\py3810\.venv\Scripts\python.exe build_nuitka.py
代码和成品放在下方。
[Python] 纯文本查看 复制代码import sys
import ctypes
import subprocess
import os
import sys
import winreg
import argparse
from pathlib import Path
from PySide6.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
    QGridLayout, QPushButton, QLabel, QLineEdit, QFrame, QMessageBox,
    QGroupBox, QSpacerItem, QSizePolicy, QCheckBox, QKeySequenceEdit,
    QSystemTrayIcon, QMenu
)
from PySide6.QtCore import Qt, QTimer, QSettings, QStandardPaths
from PySide6.QtGui import QFont, QPalette, QColor, QKeySequence, QShortcut, QIcon, QPixmap, QAction
# 获取应用程序路径(支持打包后的可执行文件)
def get_app_path():
    """获取应用程序路径,支持打包后的可执行文件"""
    if getattr(sys, 'frozen', False):
        # 如果是打包后的可执行文件
        return Path(sys.executable).parent
    else:
        # 如果是Python脚本
        return Path(__file__).parent
# 获取配置文件路径
def get_config_path():
    """获取配置文件路径"""
    config_dir = QStandardPaths.writableLocation(QStandardPaths.AppConfigLocation)
    return Path(config_dir) / "ScreenController"
class ScreenController(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Windows屏幕控制")
        self.setFixedSize(800, 750)  # 增加窗口尺寸以容纳更好的布局
        
        # 设置存储
        config_path = get_config_path()
        config_path.mkdir(parents=True, exist_ok=True)
        settings_file = config_path / "settings.ini"
        self.settings = QSettings(str(settings_file), QSettings.IniFormat)
        
        # 全局快捷键
        self.global_shortcut = None
        
        # 系统托盘
        self.tray_icon = None
        self.setup_tray_icon()
        
        self.setup_ui()
        self.setup_style()
        self.load_settings()
        
        # 定时器用于更新当前设置
        self.update_timer = QTimer()
        self.update_timer.timeout.connect(self.update_current_setting)
        self.update_timer.start(5000)  # 每5秒更新一次
        
        # 初始更新
        self.update_current_setting()
        
    def setup_ui(self):
        """设置用户界面"""
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 主布局
        main_layout = QVBoxLayout(central_widget)
        main_layout.setSpacing(25)
        main_layout.setContentsMargins(25, 25, 25, 25)
        
        # 标题
        title_label = QLabel("Windows屏幕控制器")
        title_font = QFont("Microsoft YaHei", 18, QFont.Bold)
        title_label.setFont(title_font)
        title_label.setAlignment(Qt.AlignCenter)
        title_label.setStyleSheet("""
            QLabel {
                color: #2c3e50;
                padding: 20px;
                background-color: #ecf0f1;
                border-radius: 10px;
                border: 2px solid #bdc3c7;
            }
        """)
        main_layout.addWidget(title_label)
        
        # 快速息屏按钮
        self.screen_off_btn = QPushButton("立即息屏")
        self.screen_off_btn.clicked.connect(self.turn_off_screen)
        self.screen_off_btn.setFixedHeight(60)
        button_font = QFont("Microsoft YaHei", 14, QFont.Bold)
        self.screen_off_btn.setFont(button_font)
        self.screen_off_btn.setStyleSheet("""
            QPushButton {
                background-color: #27ae60;
                color: white;
                border: none;
                border-radius: 10px;
                padding: 15px;
            }
            QPushButton:hover {
                background-color: #2ecc71;
            }
            QPushButton:pressed {
                background-color: #229954;
            }
        """)
        main_layout.addWidget(self.screen_off_btn)
        
        # 分隔线
        separator = QFrame()
        separator.setFrameShape(QFrame.HLine)
        separator.setFrameShadow(QFrame.Sunken)
        separator.setStyleSheet("QFrame { color: #bdc3c7; }")
        main_layout.addWidget(separator)
        
        # 创建水平布局容器,将息屏时间设置和应用设置分左右两侧
        horizontal_container = QWidget()
        horizontal_layout = QHBoxLayout(horizontal_container)
        horizontal_layout.setSpacing(20)
        horizontal_layout.setContentsMargins(0, 0, 0, 0)
        
        # 左侧:息屏时间设置组
        settings_group = QGroupBox("自动息屏时间设置")
        settings_group.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
        settings_group.setStyleSheet("""
            QGroupBox {
                font-weight: bold;
                border: 2px solid #3498db;
                border-radius: 10px;
                margin-top: 10px;
                padding-top: 10px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 20px;
                padding: 0 10px 0 10px;
                color: #2980b9;
            }
        """)
        
        settings_layout = QVBoxLayout(settings_group)
        settings_layout.setSpacing(15)
        
        # 预设时间按钮网格 - 重新设计为更好的布局
        preset_label = QLabel("快速设置(分钟):")
        preset_label.setFont(QFont("Microsoft YaHei", 11, QFont.Bold))
        preset_label.setStyleSheet("QLabel { color: #2c3e50; margin-bottom: 5px; }")
        settings_layout.addWidget(preset_label)
        
        # 第一行:1, 5, 10分钟
        preset_row1 = QWidget()
        preset_layout1 = QHBoxLayout(preset_row1)
        preset_layout1.setSpacing(12)
        preset_layout1.setContentsMargins(0, 0, 0, 0)
        
        for time_val in [1, 5, 10]:
            btn = QPushButton(f"{time_val}分钟")
            btn.clicked.connect(lambda checked=None, t=time_val: self.set_screen_timeout(t))
            btn.setFixedHeight(45)
            btn.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
            btn.setStyleSheet("""
                QPushButton {
                    background-color: #3498db;
                    color: white;
                    border: none;
                    border-radius: 10px;
                    padding: 10px;
                    min-width: 80px;
                }
                QPushButton:hover {
                    background-color: #5dade2;
                }
                QPushButton:pressed {
                    background-color: #2980b9;
                }
            """)
            preset_layout1.addWidget(btn)
        
        settings_layout.addWidget(preset_row1)
        
        # 第二行:15, 30, 60分钟
        preset_row2 = QWidget()
        preset_layout2 = QHBoxLayout(preset_row2)
        preset_layout2.setSpacing(12)
        preset_layout2.setContentsMargins(0, 0, 0, 0)
        
        for time_val in [15, 30, 60]:
            btn = QPushButton(f"{time_val}分钟")
            btn.clicked.connect(lambda checked=None, t=time_val: self.set_screen_timeout(t))
            btn.setFixedHeight(45)
            btn.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
            btn.setStyleSheet("""
                QPushButton {
                    background-color: #16a085;
                    color: white;
                    border: none;
                    border-radius: 10px;
                    padding: 10px;
                    min-width: 80px;
                }
                QPushButton:hover {
                    background-color: #48c9b0;
                }
                QPushButton:pressed {
                    background-color: #138d75;
                }
            """)
            preset_layout2.addWidget(btn)
        
        settings_layout.addWidget(preset_row2)
        
        # 添加间距
        settings_layout.addItem(QSpacerItem(20, 15, QSizePolicy.Minimum, QSizePolicy.Fixed))
        
        # 自定义时间设置 - 重新设计
        custom_label = QLabel("自定义时间:")
        custom_label.setFont(QFont("Microsoft YaHei", 11, QFont.Bold))
        custom_label.setStyleSheet("QLabel { color: #2c3e50; margin-bottom: 5px; }")
        settings_layout.addWidget(custom_label)
        
        custom_widget = QWidget()
        custom_layout = QHBoxLayout(custom_widget)
        custom_layout.setSpacing(12)
        custom_layout.setContentsMargins(0, 0, 0, 0)
        
        self.custom_time_input = QLineEdit()
        self.custom_time_input.setPlaceholderText("输入分钟数")
        self.custom_time_input.setFixedHeight(45)
        self.custom_time_input.setFont(QFont("Microsoft YaHei", 11))
        self.custom_time_input.returnPressed.connect(self.set_custom_timeout)
        self.custom_time_input.setStyleSheet("""
            QLineEdit {
                border: 2px solid #bdc3c7;
                border-radius: 10px;
                padding: 12px;
                background-color: white;
                font-size: 11pt;
            }
            QLineEdit:focus {
                border-color: #f39c12;
            }
        """)
        custom_layout.addWidget(self.custom_time_input, 2)
        
        minutes_label = QLabel("分钟")
        minutes_label.setFont(QFont("Microsoft YaHei", 11))
        minutes_label.setStyleSheet("QLabel { color: #7f8c8d; }")
        custom_layout.addWidget(minutes_label)
        
        custom_btn = QPushButton("设置")
        custom_btn.clicked.connect(self.set_custom_timeout)
        custom_btn.setFixedHeight(45)
        custom_btn.setFixedWidth(90)
        custom_btn.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        custom_btn.setStyleSheet("""
            QPushButton {
                background-color: #f39c12;
                color: white;
                border: none;
                border-radius: 10px;
                padding: 10px;
            }
            QPushButton:hover {
                background-color: #f4d03f;
            }
            QPushButton:pressed {
                background-color: #d68910;
            }
        """)
        custom_layout.addWidget(custom_btn)
        
        settings_layout.addWidget(custom_widget)
        
        # 添加间距
        settings_layout.addItem(QSpacerItem(20, 15, QSizePolicy.Minimum, QSizePolicy.Fixed))
        
        # 永不息屏按钮 - 重新设计
        never_btn = QPushButton("🚫 永不息屏")
        never_btn.clicked.connect(lambda checked=None: self.set_screen_timeout(0))
        never_btn.setFixedHeight(50)
        never_btn.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
        never_btn.setStyleSheet("""
            QPushButton {
                background-color: #e74c3c;
                color: white;
                border: none;
                border-radius: 12px;
                padding: 12px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #ec7063;
            }
            QPushButton:pressed {
                background-color: #c0392b;
            }
        """)
        settings_layout.addWidget(never_btn)
        
        # 将息屏时间设置组添加到左侧
        horizontal_layout.addWidget(settings_group)
        
        # 右侧:应用设置组 - 重新设计
        app_settings_group = QGroupBox("⚙️ 应用设置")
        app_settings_group.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
        app_settings_group.setStyleSheet("""
            QGroupBox {
                font-weight: bold;
                border: 2px solid #9b59b6;
                border-radius: 12px;
                margin-top: 15px;
                padding-top: 15px;
                background-color: #fafafa;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 20px;
                padding: 0 15px 0 15px;
                color: #8e44ad;
                background-color: white;
                border-radius: 5px;
            }
        """)
        
        app_settings_layout = QVBoxLayout(app_settings_group)
        app_settings_layout.setSpacing(20)
        
        # 开机自启动复选框 - 重新设计
        autostart_widget = QWidget()
        autostart_layout = QHBoxLayout(autostart_widget)
        autostart_layout.setContentsMargins(10, 5, 10, 5)
        
        autostart_icon = QLabel("🚀")
        autostart_icon.setFont(QFont("Microsoft YaHei", 14))
        autostart_layout.addWidget(autostart_icon)
        
        self.autostart_checkbox = QCheckBox("开机自启动")
        self.autostart_checkbox.setFont(QFont("Microsoft YaHei", 11))
        self.autostart_checkbox.stateChanged.connect(self.toggle_autostart)
        self.autostart_checkbox.setStyleSheet("""
            QCheckBox {
                color: #2c3e50;
                padding: 8px;
                font-weight: 500;
            }
            QCheckBox::indicator {
                width: 20px;
                height: 20px;
            }
            QCheckBox::indicator:unchecked {
                border: 2px solid #bdc3c7;
                border-radius: 4px;
                background-color: white;
            }
            QCheckBox::indicator:checked {
                border: 2px solid #9b59b6;
                border-radius: 4px;
                background-color: #9b59b6;
                image: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIiIGhlaWdodD0iMTIiIHZpZXdCb3g9IjAgMCAxMiAxMiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTEwIDNMNC41IDguNUwyIDYiIHN0cm9rZT0id2hpdGUiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIi8+Cjwvc3ZnPgo=);
            }
            QCheckBox::indicator:hover {
                border-color: #9b59b6;
            }
        """)
        autostart_layout.addWidget(self.autostart_checkbox)
        autostart_layout.addStretch()
        
        app_settings_layout.addWidget(autostart_widget)
        
        # 启动时最小化复选框 - 重新设计
        minimize_widget = QWidget()
        minimize_layout = QHBoxLayout(minimize_widget)
        minimize_layout.setContentsMargins(10, 5, 10, 5)
        
        minimize_icon = QLabel("📱")
        minimize_icon.setFont(QFont("Microsoft YaHei", 14))
        minimize_layout.addWidget(minimize_icon)
        
        self.minimize_on_start_checkbox = QCheckBox("启动时最小化到系统托盘")
        self.minimize_on_start_checkbox.setFont(QFont("Microsoft YaHei", 11))
        self.minimize_on_start_checkbox.stateChanged.connect(self.save_settings)
        self.minimize_on_start_checkbox.setStyleSheet("""
            QCheckBox {
                color: #2c3e50;
                padding: 8px;
                font-weight: 500;
            }
            QCheckBox::indicator {
                width: 20px;
                height: 20px;
            }
            QCheckBox::indicator:unchecked {
                border: 2px solid #bdc3c7;
                border-radius: 4px;
                background-color: white;
            }
            QCheckBox::indicator:checked {
                border: 2px solid #9b59b6;
                border-radius: 4px;
                background-color: #9b59b6;
                image: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIiIGhlaWdodD0iMTIiIHZpZXdCb3g9IjAgMCAxMiAxMiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTEwIDNMNC41IDguNUwyIDYiIHN0cm9rZT0id2hpdGUiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIi8+Cjwvc3ZnPgo=);
            }
            QCheckBox::indicator:hover {
                border-color: #9b59b6;
            }
        """)
        minimize_layout.addWidget(self.minimize_on_start_checkbox)
        minimize_layout.addStretch()
        
        app_settings_layout.addWidget(minimize_widget)
        
        # 快捷键设置 - 重新设计
        shortcut_container = QWidget()
        shortcut_container_layout = QVBoxLayout(shortcut_container)
        shortcut_container_layout.setContentsMargins(10, 5, 10, 5)
        shortcut_container_layout.setSpacing(8)
        
        shortcut_header = QWidget()
        shortcut_header_layout = QHBoxLayout(shortcut_header)
        shortcut_header_layout.setContentsMargins(0, 0, 0, 0)
        
        shortcut_icon = QLabel("⌨️")
        shortcut_icon.setFont(QFont("Microsoft YaHei", 14))
        shortcut_header_layout.addWidget(shortcut_icon)
        
        shortcut_label = QLabel("立即息屏快捷键:")
        shortcut_label.setFont(QFont("Microsoft YaHei", 11, QFont.Bold))
        shortcut_label.setStyleSheet("QLabel { color: #2c3e50; font-weight: 500; }")
        shortcut_header_layout.addWidget(shortcut_label)
        shortcut_header_layout.addStretch()
        
        shortcut_container_layout.addWidget(shortcut_header)
        
        shortcut_widget = QWidget()
        shortcut_layout = QHBoxLayout(shortcut_widget)
        shortcut_layout.setSpacing(12)
        shortcut_layout.setContentsMargins(0, 0, 0, 0)
        
        self.shortcut_edit = QKeySequenceEdit()
        self.shortcut_edit.setFixedHeight(40)
        self.shortcut_edit.setFont(QFont("Microsoft YaHei", 10))
        self.shortcut_edit.keySequenceChanged.connect(self.on_shortcut_changed)
        self.shortcut_edit.setStyleSheet("""
            QKeySequenceEdit {
                border: 2px solid #bdc3c7;
                border-radius: 10px;
                padding: 10px;
                background-color: white;
                font-size: 10pt;
            }
            QKeySequenceEdit:focus {
                border-color: #9b59b6;
            }
        """)
        shortcut_layout.addWidget(self.shortcut_edit, 2)
        
        clear_shortcut_btn = QPushButton("清除")
        clear_shortcut_btn.clicked.connect(self.clear_shortcut)
        clear_shortcut_btn.setFixedHeight(40)
        clear_shortcut_btn.setFixedWidth(70)
        clear_shortcut_btn.setFont(QFont("Microsoft YaHei", 9, QFont.Bold))
        clear_shortcut_btn.setStyleSheet("""
            QPushButton {
                background-color: #95a5a6;
                color: white;
                border: none;
                border-radius: 10px;
                padding: 8px;
            }
            QPushButton:hover {
                background-color: #b2bec3;
            }
            QPushButton:pressed {
                background-color: #7f8c8d;
            }
        """)
        shortcut_layout.addWidget(clear_shortcut_btn)
        
        shortcut_container_layout.addWidget(shortcut_widget)
        app_settings_layout.addWidget(shortcut_container)
        
        # 将应用设置组添加到右侧
        horizontal_layout.addWidget(app_settings_group)
        
        # 将水平布局容器添加到主布局
        main_layout.addWidget(horizontal_container)
        
        # 当前设置显示
        self.current_setting_label = QLabel("当前设置:获取中...")
        self.current_setting_label.setFont(QFont("Microsoft YaHei", 10))
        self.current_setting_label.setAlignment(Qt.AlignCenter)
        self.current_setting_label.setStyleSheet("""
            QLabel {
                color: #7f8c8d;
                padding: 15px;
                background-color: #f8f9fa;
                border-radius: 8px;
                border: 1px solid #dee2e6;
            }
        """)
        main_layout.addWidget(self.current_setting_label)
        
        # 添加弹性空间
        main_layout.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
        
    def setup_style(self):
        """设置整体样式"""
        self.setStyleSheet("""
            QMainWindow {
                background-color: #ffffff;
            }
            QWidget {
                background-color: #ffffff;
            }
        """)
        
    def setup_tray_icon(self):
        """设置系统托盘图标"""
        if not QSystemTrayIcon.isSystemTrayAvailable():
            QMessageBox.critical(self, "系统托盘", "系统托盘不可用")
            return
        
        # 创建托盘图标
        self.tray_icon = QSystemTrayIcon(self)
        
        # 尝试加载favicon.ico图标
        app_path = get_app_path()
        icon_path = app_path / "favicon.ico"
        
        if icon_path.exists():
            icon = QIcon(str(icon_path))
        else:
            # 如果找不到favicon.ico,创建一个简单的图标
            pixmap = QPixmap(16, 16)
            pixmap.fill(Qt.blue)
            icon = QIcon(pixmap)
        
        self.tray_icon.setIcon(icon)
        
        # 创建托盘菜单
        tray_menu = QMenu()
        
        # 显示主窗口
        show_action = QAction("显示主窗口", self)
        show_action.triggered.connect(self.show_main_window)
        tray_menu.addAction(show_action)
        
        # 立即息屏
        screen_off_action = QAction("立即息屏", self)
        screen_off_action.triggered.connect(self.turn_off_screen)
        tray_menu.addAction(screen_off_action)
        
        tray_menu.addSeparator()
        
        # 退出程序
        quit_action = QAction("退出", self)
        quit_action.triggered.connect(QApplication.instance().quit)
        tray_menu.addAction(quit_action)
        
        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.activated.connect(self.tray_icon_activated)
        
        # 设置托盘提示
        self.tray_icon.setToolTip("Windows屏幕控制器")
        
        # 显示托盘图标
        self.tray_icon.show()
   
    def tray_icon_activated(self, reason):
        """托盘图标被激活时的处理"""
        if reason == QSystemTrayIcon.DoubleClick:
            self.show_main_window()
   
    def show_main_window(self):
        """显示主窗口"""
        self.show()
        self.raise_()
        self.activateWindow()
   
    def turn_off_screen(self):
        """立即关闭屏幕"""
        try:
            # 发送消息关闭显示器
            ctypes.windll.user32.SendMessageW(0xFFFF, 0x0112, 0xF170, 2)
        except Exception as e:
            if self.isVisible():
                QMessageBox.warning(self, "错误", f"关闭屏幕失败: {str(e)}")
            else:
                # 如果窗口不可见,通过托盘显示消息
                if self.tray_icon:
                    self.tray_icon.showMessage("错误", f"关闭屏幕失败: {str(e)}", QSystemTrayIcon.Warning, 3000)
   
    def set_screen_timeout(self, minutes):
        """设置屏幕超时时间"""
        try:
            if minutes == 0:
                message = "已设置为永不息屏"
                # 永不关闭屏幕
                result1 = subprocess.run(['powercfg', '/change', 'monitor-timeout-ac', '0'],
                                       capture_output=True, text=True, check=True,
                                       creationflags=subprocess.CREATE_NO_WINDOW)
                result2 = subprocess.run(['powercfg', '/change', 'monitor-timeout-dc', '0'],
                                       capture_output=True, text=True, check=True,
                                       creationflags=subprocess.CREATE_NO_WINDOW)
            else:
                message = f"已设置息屏时间为 {minutes} 分钟"
                # 设置指定的超时时间
                result1 = subprocess.run(['powercfg', '/change', 'monitor-timeout-ac', str(minutes)],
                                       capture_output=True, text=True, check=True,
                                       creationflags=subprocess.CREATE_NO_WINDOW)
                result2 = subprocess.run(['powercfg', '/change', 'monitor-timeout-dc', str(minutes)],
                                       capture_output=True, text=True, check=True,
                                       creationflags=subprocess.CREATE_NO_WINDOW)
            
            if self.isVisible():
                QMessageBox.information(self, "成功", message)
            else:
                # 如果窗口不可见,通过托盘显示消息
                if self.tray_icon:
                    self.tray_icon.showMessage("设置成功", message, QSystemTrayIcon.Information, 3000)
            
            self.update_current_setting()
            
        except subprocess.CalledProcessError as e:
            error_msg = f"设置失败: {e.stderr if e.stderr else str(e)}"
            if self.isVisible():
                QMessageBox.warning(self, "错误", error_msg)
            else:
                if self.tray_icon:
                    self.tray_icon.showMessage("错误", error_msg, QSystemTrayIcon.Warning, 3000)
        except FileNotFoundError:
            error_msg = "找不到powercfg命令,请确保在Windows系统上运行"
            if self.isVisible():
                QMessageBox.warning(self, "错误", error_msg)
            else:
                if self.tray_icon:
                    self.tray_icon.showMessage("错误", error_msg, QSystemTrayIcon.Warning, 3000)
        except Exception as e:
            error_msg = f"未知错误: {str(e)}"
            if self.isVisible():
                QMessageBox.warning(self, "错误", error_msg)
            else:
                if self.tray_icon:
                    self.tray_icon.showMessage("错误", error_msg, QSystemTrayIcon.Warning, 3000)
   
    def set_custom_timeout(self):
        """设置自定义超时时间"""
        try:
            time_str = self.custom_time_input.text().strip()
            if not time_str:
                QMessageBox.warning(self, "警告", "请输入时间")
                return
            
            minutes = int(time_str)
            if minutes
Nuitka打包脚本 build_nuitka.py
[Python] 纯文本查看 复制代码#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Nuitka打包脚本 - Windows屏幕控制器
使用方法:
python build_nuitka.py
"""
import os
import sys
import subprocess
from pathlib import Path
def build_with_nuitka():
    """使用Nuitka打包程序"""
   
    # 获取当前脚本所在目录
    script_dir = Path(__file__).parent
   
    # 主程序文件
    main_script = script_dir / "sc.py"
   
    # 图标文件
    icon_file = script_dir / "favicon.ico"
   
    # 检查文件是否存在
    if not main_script.exists():
        print(f"错误: 找不到主程序文件 {main_script}")
        return False
   
    if not icon_file.exists():
        print(f"警告: 找不到图标文件 {icon_file}")
   
    # 构建Nuitka命令
    nuitka_cmd = [
        sys.executable, "-m", "nuitka",
        "--standalone",                    # 独立模式
        "--onefile",                      # 单文件模式
        "--windows-console-mode=disable",      # 禁用控制台窗口
        "--enable-plugin=pyside6",        # 启用PySide6插件
        "--include-qt-plugins=platforms,styles,iconengines,imageformats",  # 包含Qt插件
        
        "--output-filename=WindowsScreenController.exe",  # 输出文件名
    ]
   
    # 如果图标文件存在,添加图标和数据文件参数
    if icon_file.exists():
        nuitka_cmd.extend([
            f"--windows-icon-from-ico={icon_file}",  # 设置程序图标
            f"--include-data-files={icon_file}=favicon.ico",  # 包含图标文件到程序中
        ])
   
    # 添加主程序文件
    nuitka_cmd.append(str(main_script))
   
    print("开始使用Nuitka打包...")
    print(f"命令: {' '.join(nuitka_cmd)}")
    print()
   
    try:
        # 执行打包命令
        result = subprocess.run(nuitka_cmd, cwd=script_dir, check=True)
        
        print("\n打包完成!")
        
        # 查找生成的可执行文件
        exe_file = script_dir / "WindowsScreenController.exe"
        if exe_file.exists():
            print(f"可执行文件位置: {exe_file}")
            print(f"文件大小: {exe_file.stat().st_size / 1024 / 1024:.2f} MB")
        else:
            print("警告: 未找到生成的可执行文件")
        
        return True
        
    except subprocess.CalledProcessError as e:
        print(f"\n打包失败! 错误代码: {e.returncode}")
        return False
    except FileNotFoundError:
        print("\n错误: 找不到Nuitka。请确保已安装Nuitka:")
        print("pip install nuitka")
        return False
    except Exception as e:
        print(f"\n打包过程中发生错误: {e}")
        return False
def main():
    """主函数"""
    print("Windows屏幕控制器 - Nuitka打包工具")
    print("=" * 50)
   
    # 检查Python版本
    if sys.version_info
成品:https://wwio.lanzoub.com/iYCW12ys83ub

微软, 快捷键

您需要登录后才可以回帖 登录 | 立即注册

返回顶部