gpt4 book ai didi

pyqt - 如何重新启动我的 pyqt 应用程序?

转载 作者:行者123 更新时间:2023-12-03 20:49:51 29 4
gpt4 key购买 nike

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__ 中定义

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 方法,而不是信号/插槽。

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 - 如何重新启动我的 pyqt 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34372164/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com