7
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class MainWindow(QMainWindow):
       EXIT_CODE_REBOOT = -123

#constructor
       def __init__(self):
            super().__init__() #call super class constructor
            #above is the constructor ^^^^^^^^^^^
            self.HomePage() #this goes to the homepage function

def HomePage(self):
    #this layout holds the review question 1
    self.quit_button_11 = QPushButton("restart", self)
    self.quit_button_11.clicked.connect(self.restart)

def restart(self):  # function connected to when restart button clicked
    qApp.exit( MainWindow.EXIT_CODE_REBOOT )


if __name__=="__main__":
currentExitCode = MainWindow.EXIT_CODE_REBOOT
while currentExitCode == MainWindow.EXIT_CODE_REBOOT:
    a = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    currentExitCode = a.exec_()
    a = None  # delete the QApplication object

How to restart this code ?

Mel
  • 5,837
  • 10
  • 37
  • 42
Piers
  • 103
  • 1
  • 1
  • 5
  • complementary ideas can be found here: http://stackoverflow.com/questions/5129788/how-to-restart-my-own-qt-application – ochurlaud Dec 19 '15 at 15:43
  • First, you should avoid the `from X import *`. I checked what you did, and there is a bug somewhere that I didn't understand. Use gdb to further debug. – ochurlaud Dec 26 '15 at 19:35
  • Does this answer your question? [How to reboot PyQt5 application](https://stackoverflow.com/questions/62609780/how-to-reboot-pyqt5-application) – Caleb Bassham May 12 '21 at 23:58

3 Answers3

12

Let's say you are in MainWindow. Define in the __init__

MainWindow.EXIT_CODE_REBOOT = -12345678  # or whatever number not already taken

Your slot restart() should contain:

qApp.exit( MainWindow.EXIT_CODE_REBOOT )

and your main:

currentExitCode = MainWindow.EXIT_CODE_REBOOT

while currentExitCode == MainWindow.EXIT_CODE_REBOOT:
    a = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    currentExitCode = a.exec_()

return currentExitCode

[1] https://wiki.qt.io/How_to_make_an_Application_restartable


EDIT: Minimal working example

Instead of a signal/slot, I just re-implemented the keyPressedEvent method.

import sys
from PyQt4 import QtGui
from PyQt4.QtCore import Qt

class MainWindow(QtGui.QMainWindow):
    EXIT_CODE_REBOOT = -123
    def __init__(self,parent=None):
        QtGui.QMainWindow.__init__(self, parent)

    def keyPressEvent(self,e):
        if (e.key() == Qt.Key_R):
            QtGui.qApp.exit( MainWindow.EXIT_CODE_REBOOT )


if __name__=="__main__":
    currentExitCode = MainWindow.EXIT_CODE_REBOOT
    while currentExitCode == MainWindow.EXIT_CODE_REBOOT:
        a = QtGui.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        currentExitCode = a.exec_()
        a = None  # delete the QApplication object
Drise
  • 4,310
  • 5
  • 41
  • 66
ochurlaud
  • 410
  • 5
  • 14
  • 1
    Thanks for responding! I put this code in and when I click the button, in the python shell it says it has restarted but my application doesn't come back up on my screen! Sorry if I'm being a nuisance, new to coding, but thanks! – Piers Dec 20 '15 at 00:54
  • you don't give any hint about your code, what the commands output: it's not easy to help you – ochurlaud Dec 20 '15 at 13:28
  • 4
    It is important to note this method does not restart Python or reload any libraries/modules. If you want to start completely fresh, you need to use a method that launches the application in a new process. – three_pineapples Dec 20 '15 at 20:12
  • @ochurlaud thanks again, this code restarted the application and showed me my first layout but then it crashed. Any tips to fix this? Thanks [I updated my code for you so that you could see what it looks like] – Piers Dec 21 '15 at 01:23
  • @three_pineapples that's whta I need. Do you care to post an answer? I will post one if my attempt works in a few days. But gotta work on other project atm. – MathCrackExchange Apr 22 '19 at 20:11
  • I just tested the sample code replacing pyqt4 by pyside2 and I get "RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance." Is there any way to do it with pyside2? I wonder if it could be related to this qtbug: https://bugreports.qt.io/browse/PYSIDE-1190 – laurapons Jul 04 '23 at 14:50
  • To anyone interested, to make this work on pyside2 you need to add: qApp.shutdown() before a=None – laurapons Jul 04 '23 at 15:01
9

Below will restart the whole application fully, regardless of frozen or unfrozen packages.

os.execl(sys.executable, sys.executable, *sys.argv)
Sina
  • 161
  • 1
  • 5
  • More information is here: https://docs.python.org/3/library/os.html#os.execl , note "These functions all execute a new program, replacing the current process; they do not return." – eri0o Apr 03 '23 at 13:53
2

I answered the same question in a duplicate topics (How to reboot PyQt5 application).

The idea is to close (or forget) the QMainWindow and recreate it.

If you just "show()" a single widget, the same idea works fine.

import sys
import uuid

from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton


class MainWindow(QMainWindow):
    singleton: 'MainWindow' = None

    def __init__(self):
        super().__init__()
        btn = QPushButton(f'RESTART\n{uuid.uuid4()}')
        btn.clicked.connect(MainWindow.restart)
        self.setCentralWidget(btn)
        self.show()

    @staticmethod
    def restart():
        MainWindow.singleton = MainWindow()


def main():
    app = QApplication([])
    MainWindow.restart()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
F.Le Chat
  • 41
  • 1