开发者

PyQt: best way to do the trick "start at boot" for my program in Windows

I'm usi开发者_高级运维ng PyQt to develop an application that in Windows, if set in preferences, should be able to start at boot.

I'm releasing this software with PyInstaller as a single executable file; i don't have a proper "installer".

Which is the best way to achieve this? ( = starting at boot)

A possible solution is to add a link in the startup folder, but i have to do it from the software: it's possible? Other ways?

There is an universal path to the Startup folder? Can i have some rights' problem?


try this code (it works for me with 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_()


You may add registry key under [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run], with any name and value "path_to_your_exec". this will require local administrator right, but will work for all users. The same key but starting with [HKEY_CURRENT_USER] will not require administrator privileges, but will work only for current user. That registry path is the same for win2k..win7

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜