如何重新启动我的 pyqt 应用程序? [英] How do I restart my pyqt application?

查看:147
本文介绍了如何重新启动我的 pyqt 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何重新启动此代码?

推荐答案

假设您在 MainWindow 中.在__init__

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

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

您的插槽 restart() 应包含:

qApp.exit( MainWindow.EXIT_CODE_REBOOT )

和你的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

我只是重新实现了 keyPressedEvent 方法,而不是信号/插槽.

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

这篇关于如何重新启动我的 pyqt 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆