Python 关闭并打开新窗口PYQT5

Python 关闭并打开新窗口PYQT5,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,我想在一个窗口按下一个按钮,关闭该窗口,然后打开一个新窗口 我怎么做 我已经尝试过了,但它会将此消息发送到控制台: QCoreApplication::exec:事件循环已在运行 class Window(QWidget): def __init__(self,parent = None): super().__init__(parent) self.title = 'pySim Z-eighty' self.left = 0

我想在一个窗口按下一个按钮,关闭该窗口,然后打开一个新窗口

我怎么做

我已经尝试过了,但它会将此消息发送到控制台:

QCoreApplication::exec:事件循环已在运行

class Window(QWidget):
    def __init__(self,parent = None):
        super().__init__(parent)
        self.title = 'pySim Z-eighty'
        self.left = 0
        self.top = 0
        self.width = 1200
        self.height = 3000
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.button = QPushButton("Z80")
        self.button1 = QPushButton()
        self.button2 = QPushButton()
        self.container =    QWidget()
        self.layout = QGridLayout()
        self.layout.addWidget(self.button1, 1, 0)
        self.layout.addWidget(self.button, 1, 1)
        self.layout.addWidget(self.button2, 1, 2)
        self.container.setLayout(self.layout)
        self.layoutPrincipal = QBoxLayout(0)
        self.layoutPrincipal.addWidget(self.container)
        self.setLayout(self.layoutPrincipal)
        self.button.pressed.connect(self.IniciarInterfaz)

    def IniciarInterfaz(self):
        self.hide()
        app = QApplication(sys.argv)
        ex = mainWindow()
        ex.setStyleSheet("background-color: #fff")
        ex.show()
        sys.exit(app.exec_())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Window()
    ex.show()
    sys.exit(app.exec_())

我的主要问题是,当我按下按钮时,我无法打开新窗口,
PyQt
应用程序中只能有一个
QApplication
,因此,如果您已经创建了它,请不要再这样做

另一个问题是,变量只存在于上下文中,在您的情况下是mainWindow,因此在函数的末尾StartInterface将删除此变量和窗口,解决方案是使mainWindow成为类的成员,因此上下文将是类而不是函数,因此它将保持正确

def IniciarInterfaz(self):
    self.hide()
    self.ex = mainWindow()
    self.ex.setStyleSheet("background-color: #fff")
    self.ex.show()

PYQT无打开和关闭方法

hide()和show()方法您可以根据需要使用按钮

def PlatformType_单击(自身):
dialog.hide()
对话1.show()

显示您的代码。要改进您的问题,您应该阅读显示以下链接的内容:,问题的原因可能是多种原因,我们不想浪费时间猜测。我的问题是,当我按下按钮Z80并调用self.ini时,它无法打开我的新窗口什么是主窗口?新窗口,我想用Z80按钮打开