PyQt:在Windows中为我的程序执行“启动时启动”的最佳方法

时间:2011-05-31 10:17:10

标签: python windows pyqt startup pyinstaller

我正在使用PyQt开发一个应用程序,在Windows中,如果在首选项中设置,应该能够在启动时启动。

我将PyInstaller作为单个可执行文件发布此软件;我没有合适的“安装人员”。

实现这一目标的最佳方式是什么? (=从开始时开始)

一种可能的解决方案是在启动文件夹中添加一个链接,但我必须从软件中执行此操作:可能吗?其他方式?

Startup文件夹有一个通用路径?我可以有一些权利问题吗?

2 个答案:

答案 0 :(得分:6)

尝试此代码(它适用于py2exe):

import sys
from PyQt4.QtCore import QSettings
from PyQt4.QtGui import (QApplication, QWidget, QCheckBox, QPushButton,
                         QVBoxLayout)

RUN_PATH = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"

class MainWidget(QWidget):

    def __init__(self,parent=None):
        super(MainWidget, self).__init__(parent)
        self.settings = QSettings(RUN_PATH, QSettings.NativeFormat)
        self.setupUi()       
        # Check if value exists in registry
        self.checkbox.setChecked(self.settings.contains("MainWidget"))

    def setupUi(self):
        self.checkbox = QCheckBox("Boot at Startup", self)
        button = QPushButton("Close", self)
        button.clicked.connect(self.close)
        layout = QVBoxLayout(self)
        layout.addWidget(self.checkbox)
        layout.addWidget(button)

    def closeEvent(self, event):
        if self.checkbox.isChecked():
            self.settings.setValue("MainWidget",sys.argv[0]);
        else:
            self.settings.remove("MainWidget");
        event.accept()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWidget()
    w.show()
    app.exec_()

答案 1 :(得分:3)

您可以在[HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run]下添加任何名称和值“path_to_your_exec”的注册表项。这将需要本地管理员权限,但将适用于所有用户。 相同的密钥但以[HKEY_CURRENT_USER]开头不需要管理员权限,但仅适用于当前用户。 该注册表路径与win2k..win7

相同