我想拥有没有主窗口的app,但是有很多小部件一个接一个显示(对话框对我来说不起作用)。
我有以下代码:
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from start_window import Ui_Form
from second_window import Ui_Form as Ui_Form_Second
class StartForm(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.button)
def button(self):
secondWindow = SecondForm()
secondWindow.show()
self.hide()
class SecondForm(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form_Second()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartForm()
myapp.show()
sys.exit(app.exec_())
当我按下按钮app关闭时。如何将应用程序的执行传递给第二个小部件?
答案 0 :(得分:0)
问题是您没有保留对在button
处理程序中创建的窗口小部件的引用,因此窗口小部件会立即被销毁。
尝试这样的事情:
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from start_window import Ui_Form
from second_window import Ui_Form as Ui_Form_Second
class StartForm(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.button)
self.child_windows = []
def button(self):
secondWindow = SecondForm()
secondWindow.show()
self.child_windows.append(secondWindow)
self.hide()
class SecondForm(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form_Second()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartForm()
myapp.show()
sys.exit(app.exec_())