如何重启 PyQt5 应用程序
我正在尝试在客户端更新后重新启动我的应用程序.我能够实现直到自动更新部分.我尝试浏览如何重新启动 PyQt 应用程序?.之前很少有类似的问题,但没有一个有很好的解释或带有按钮单击事件的示例.你们能帮我了解如何重新启动 PyQt 应用程序吗?基本上我想在每次有更新时从 if __name__ == '__main__':
重新启动应用程序.
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.
注意:AppLogin
是我创建的用于处理应用程序登录的私有模块.所以基本上这就是应用程序打开后的登陆 QDialog
.
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()