1

我想有一个没有主窗口的应用程序,但有许多小部件一一显示(对话框对我不起作用)。

我有以下代码:

# -*- 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_())

当我按下按钮应用程序关闭。如何将应用程序的执行传递给第二个小部件?

4

1 回答 1

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_())
于 2013-09-22T22:25:37.130 回答