How to reboot PyQt5 application - python

I'm trying to restart my application after an update from the client side. I was able to achieve till the auto update part. I tried to surf on How to restart PyQt application?. There are few similar questions earlier, but none of them have good explanation or example with a button click event. Could you guys help me understand on how to reboot a PyQt application. Basically I want to restart the application from if __name__ == '__main__': everytime there is an update.
Note: AppLogin is my private module I created to handle application login. So basically that would be the landing QDialog once application is opened.
from PyQt5.QtWidgets import *
import sys
import AppLogin
class App:
def __init__(self):
btn = QPushButton(main_window)
btn.setText('close')
btn.pressed.connect(self.restart)
main_window.show()
def restart(self):
# Here goes the code for restart
pass
if __name__ == '__main__':
appctxt = QApplication(sys.argv)
log_in = AppLogin.Login()
if log_in.exec_() == QDialog.Accepted:
main_window = QMainWindow()
ui = App()
exit_code = appctxt.exec_()
sys.exit(exit_code)

The logic is to end the eventloop and launch the application an instant before it closes:
import sys
from PyQt5 import QtCore, QtWidgets
def restart():
QtCore.QCoreApplication.quit()
status = QtCore.QProcess.startDetached(sys.executable, sys.argv)
print(status)
def main():
app = QtWidgets.QApplication(sys.argv)
print("[PID]:", QtCore.QCoreApplication.applicationPid())
window = QtWidgets.QMainWindow()
window.show()
button = QtWidgets.QPushButton("Restart")
button.clicked.connect(restart)
window.setCentralWidget(button)
sys.exit(app.exec_())
if __name__ == "__main__":
main()

A solution 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()

Related

Restarting PyQt app in PyCharm with QProcess

Button click starts a new detached process of the same app. However, when launched from PyCharm, it crashes on second restart. Print is no longer visible after restart as well.
I want my app to be able to restart many times when launched from either PyCharm or cmd.
import sys, os
from PyQt5.QtCore import QProcess
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
button = QPushButton("Restart")
button.clicked.connect(self.restart)
self.setCentralWidget(button)
def restart(self):
print("Restart")
self.close()
proc = QProcess()
proc.startDetached(sys.executable, sys.argv)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == "__main__":
main()

How to run a PyQt app with connexion module?

I want to run a connexion server in a Qt app, but i don't know how doin this.
I've tried stuff like below, but execution is stuck in "connexion loop" and button "close server" won't show unit i ctrl-c connexion server in console... :
import sys, os
import connexion
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QPushButton
connex_app = connexion.App("Hello World")
class OpennoteserverDlg(QPushButton):
def __init__(self, parent=None):
super().__init__(
"&Close Server", parent)
self.clicked.connect(self.close)
self.setWindowTitle("Opennote-server")
app = QApplication(sys.argv)
form = OpennoteserverDlg()
form.show()
app.connex_app = connex_app
app.connex_app.run()
app.exec_()
The run() method is blocking, so it will not allow the GUI event loop to be executed, causing several GUI tasks not to work correctly. The solution is to run the server in another thread
import signal
import sys
import threading
import connexion
from PyQt5.QtWidgets import QApplication, QPushButton
class OpennoteserverDlg(QPushButton):
def __init__(self, parent=None):
super().__init__("&Close Server", parent)
self.clicked.connect(self.close)
self.setWindowTitle("Opennote-server")
def run_server():
connex_app = connexion.App("Hello World")
connex_app.run()
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QApplication(sys.argv)
form = OpennoteserverDlg()
form.show()
threading.Thread(target=run_server, daemon=True).start()
sys.exit(app.exec_())

Pyside2 second window(QDialog) closes the main one

import sys
from PySide2.QtCore import QFile
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtUiTools import QUiLoader
class MyMainWindow(QMainWindow):
def __init__(self):
super().__init__()
loader = QUiLoader()
self.ui = loader.load("mainWindow.ui", self)
self.ui.pushButton_call_dialog.clicked.connect(self.call_dialog)
self.ui.close()
self.ui.show()
def call_dialog(self):
loader = QUiLoader()
self.dialog = loader.load("dialog.ui")
self.dialog.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyMainWindow()
window.show
sys.exit(app.exec_())
Hi everyone,
any idea why the second (dialog) window closes the entire application?
Of course, it is not a crash since i'm getting a message saying:
Process finished with exit code 0
Thanks for your help
You could handle your QDialog on a separate class, and then make them interact only, the structure might change a bit, but you can see if it's a viable answer:
import sys
from PySide2.QtWidgets import *
class MyWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
button = QPushButton("Dialog")
button.clicked.connect(self.open_dialog)
self.setCentralWidget(button)
def open_dialog(self):
dialog = MyDialog()
dialog.show()
dialog.exec_()
class MyDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
button = QPushButton("Close")
button.clicked.connect(self.close_dialog)
layout = QHBoxLayout()
layout.addWidget(button)
self.setLayout(layout)
def close_dialog(self):
self.close()
if __name__ == "__main__":
app = QApplication()
m = MyWindow()
m.show()
sys.exit(app.exec_())
Just notice that you should include the setUp step on each class.
Hope it helps.
To put the dialog into a separate class didn't work for either.
Every time the Dialog.close() event was called, it closes the whole application.
What worked for me, was to use hide() instead

Open second window with QTDesigner 5 in Python 3

I have problem with QTDesigner 5, which should be trivial, but I just can't figure out the problem.
What I want to do is to open a second Window when clicking on a button:
I have designed the Main Window and the secondary one with QTDesigner (PyQT5!) and converted them with pyuic to .py files. The Main Window opens without problems with the following Code:
from PyQt5 import QtGui, QtWidgets, QtCore, uic
import UI14 as UIImport
import GIPrompt as GIImport
class MainWindow(UIImport.Ui_MainWindow):
def __init__(self, window):
UIImport.Ui_MainWindow.__init__(self)
self.setupUi(window)
self.radioButtonGI.clicked.connect(self.openGIPrompt)
def openGIPrompt(self):
windowGI = QtWidgets.QDialog()
Gi = GIPrompt(windowGI)
windowGI.show()
class GIPrompt(GIImport.Ui_GIPrompt):
def __init__(self, windowGI):
GIImport.Ui_GIPrompt.__init__(self)
self.setupUi(windowGI)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)
window.show()
sys.exit(app.exec_())
If I add the following to the main function, the "GiPrompt" Window opens as well along with the Main Window:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)
window.show()
"""Open secondWindow"""
windowGI = QtWidgets.QDialog()
Gi = GIPrompt(windowGI)
windowGI.show()
sys.exit(app.exec_())
If I try to open the second window via the openGIPrompt function, nothing happens. I do not get an error message, and no window appears. A print command however tells me that the init_function of the second Window is called...
Has someone an idea, what the problem could be?
Thanks in advance!
I have figured out the problem:
Apparently, the initialized Window is disposed of by garbage collection, as the variables are not declared as self:
This fixed the problem:
from PyQt5 import QtGui, QtWidgets, QtCore, uic
import UI14 as UIImport
import GIPrompt as GIImport
class MainWindow(UIImport.Ui_MainWindow):
windowGI=None
Gi=None
def __init__(self, window):
UIImport.Ui_MainWindow.__init__(self)
self.setupUi(window)
self.radioButtonGI.clicked.connect(self.openGIPrompt)
def openGIPrompt(self):
self.windowGI = QtWidgets.QDialog()
self.Gi = GIPrompt(self.windowGI)
self.windowGI.show()
class GIPrompt(GIImport.Ui_GIPrompt):
def __init__(self, windowGI):
GIImport.Ui_GIPrompt.__init__(self)
self.setupUi(windowGI)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
prog = MainWindow(window)
window.show()
sys.exit(app.exec_())

PyQt window closes immediately after opening

I am getting an issue when trying to open a PyQt window.
The code below is an example of my original code. When I imported the module in import Test and ran test.Start(), I got the following error:
QCoreApplication::exec: The event loop is already running
After some research, I found out it was because I had already already made a QApplication.
test.py....
import sys
def Start():
app = QApplication(sys.argv)
m = myWindow()
m.show()
app.exec_()
class myWindow():....
if __name__ == "__main__":
Start()
So then I read that I could rewrite my code like this and it would fix the error:
test.py....
def Start():
m = myWindow()
m.show()
class myWindow():....
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
Start()
app.exec_()
Now I no longer get the QCoreApplication::exec: The event loop is already running error, but my window closes almost immediately after opening.
You need to keep a reference to the opened window, otherwise it goes out of scope and is garbage collected, which will destroy the underlying C++ object also. Try:
def Start():
m = myWindow()
m.show()
return m
class myWindow():....
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Start()
app.exec_()
You can also do:
def Start():
global m
m = myWindow()
m.show()
class myWindow():....
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Start()
app.exec_()
Use the following code. Your problem is in your imports and using "show" as a name for function as far as I assume. You haven't provided what you have written in your class, so it's difficult to guess. But following code works like a charm. ;-)
Best wishes, good luck!
import sys
from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
def Start():
m = myWindow()
m.showWid()
sys.exit(app.exec())
class myWindow:
def __init__(self):
self.window = QWidget()
self.window.setWindowTitle("Program Title")
self.window.setFixedWidth(600)
self.window.setStyleSheet("background: #18BEBE;")
def showWid(self):
self.window.show()
if __name__ == "__main__":
Start()

Categories