0

在我的项目中,我创建了两个主窗口,我想从 mainwindow1(正在运行)调用 mainwindow2。在 mainwindow1 我已经使用了 app.exec_() (PyQt) 并显示 maindow2 我在按钮的单击事件中使用 maindow2.show() 但不显示任何内容

4

1 回答 1

6

调用 mainwindow2.show() 应该适合你。您能否提供更完整的代码示例?其他地方可能有问题。

编辑: 更新代码以显示如何在打开和关闭其他窗口时隐藏和显示窗口的示例。

from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, \
            QLabel, QVBoxLayout, QWidget
from PyQt4.QtCore import pyqtSignal

class MainWindow1(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent) 
        button = QPushButton('Test')
        button.clicked.connect(self.newWindow)
        label = QLabel('MainWindow1')

        centralWidget = QWidget()
        vbox = QVBoxLayout(centralWidget)
        vbox.addWidget(label)
        vbox.addWidget(button)
        self.setCentralWidget(centralWidget)

    def newWindow(self):
        self.mainwindow2 = MainWindow2(self)
        self.mainwindow2.closed.connect(self.show)
        self.mainwindow2.show()
        self.hide()

class MainWindow2(QMainWindow):

    # QMainWindow doesn't have a closed signal, so we'll make one.
    closed = pyqtSignal()

    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.parent = parent
        label = QLabel('MainWindow2', self)

    def closeEvent(self, event):
        self.closed.emit()
        event.accept()

def startmain():
    app = QApplication(sys.argv)
    mainwindow1 = MainWindow1()
    mainwindow1.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    import sys
    startmain()
于 2011-04-12T09:37:51.800 回答