Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using 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?

like image 818
apelliciari Avatar asked May 31 '11 10:05

apelliciari


1 Answers

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_()
like image 121
pedrotech Avatar answered Sep 28 '22 11:09

pedrotech