PyQt:如何启动一个分离的新实例,以避免一次崩溃杀死所有实例?
我正在编写一个程序(PyQt5)帮助我的同事处理数据。我有一个名为“newSession”的按钮,据说它应该启动程序的新实例(主窗口)并独立运行。
我想避免的问题是:万一一个主窗口崩溃,所有现有的主窗口都会崩溃。 (现在就是这样)
这是简化版本,只有两个按钮:
import sys
from PyQt5 import QtWidgets, QtCore
instanceList = []
class newWindow(QtWidgets.QWidget):
def __init__(self) -> None:
super().__init__()
self.idx = 0
self.setTitle()
self.setGeometry(400, 100, 500, 80)
btn1 = QtWidgets.QPushButton(self, text='New session')
btn2 = QtWidgets.QPushButton(self, text='Crash it!')
LO = QtWidgets.QVBoxLayout(self)
LO.addWidget(btn1)
LO.addWidget(btn2)
self.setLayout(LO)
btn1.clicked.connect(self.addWindow)
btn2.clicked.connect(self.crashIt)
def setTitle(self):
self.setWindowTitle('Window {0}'.format(self.idx))
def addWindow(self):
w = newWindow()
w.move(self.pos() + QtCore.QPoint(60, 60))
w.idx = self.idx + 1
w.setTitle()
instanceList.append(w)
w.show()
def crashIt(self):
a.b = 40
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = newWindow()
instanceList.append(window)
window.show()
sys.exit(app.exec_())
I am writing a program (PyQt5) helping my coworker to process data. I have a button call "newSession", which supposedly should start a new instance of the program (main window) and operates independently.
The problem I want to avoid: in case one main window crashes, it will crash all the existing main windows. (which it does right now)
Here is simplified version, with only two buttons:
import sys
from PyQt5 import QtWidgets, QtCore
instanceList = []
class newWindow(QtWidgets.QWidget):
def __init__(self) -> None:
super().__init__()
self.idx = 0
self.setTitle()
self.setGeometry(400, 100, 500, 80)
btn1 = QtWidgets.QPushButton(self, text='New session')
btn2 = QtWidgets.QPushButton(self, text='Crash it!')
LO = QtWidgets.QVBoxLayout(self)
LO.addWidget(btn1)
LO.addWidget(btn2)
self.setLayout(LO)
btn1.clicked.connect(self.addWindow)
btn2.clicked.connect(self.crashIt)
def setTitle(self):
self.setWindowTitle('Window {0}'.format(self.idx))
def addWindow(self):
w = newWindow()
w.move(self.pos() + QtCore.QPoint(60, 60))
w.idx = self.idx + 1
w.setTitle()
instanceList.append(w)
w.show()
def crashIt(self):
a.b = 40
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = newWindow()
instanceList.append(window)
window.show()
sys.exit(app.exec_())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论