关闭并打开新窗口 PYQT5 [英] Close and Open new Window PYQT5

查看:73
本文介绍了关闭并打开新窗口 PYQT5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按一个窗口中的按钮并关闭该窗口,然后再打开一个新窗口

I would like to press a button in a window and close that window,after that open a new window

我该怎么做?

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

I already tried it but it sends this message the console:

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

QCoreApplication::exec: The event loop is already running

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

我的主要问题是当我按下按钮时我无法打开新窗口

My main problem is when i pressed the button I can't open the new window

推荐答案

PyQt 应用程序中只能有一个 QApplication,所以如果你已经创建了它,请执行不要再这样做了.

There can only be one QApplication within the PyQt application, so if you already created it, do not do it again.

另一个问题是变量只存在于上下文中,在你的情况下是mainWindow,所以在函数StartInterface的末尾会消除这个变量和窗口,解决办法是让mainWindow成为类的成员,所以上下文将是类而不是函数,因此它将保持正确.

Another problem is that the variables exist only within the context, in your case mainWindow, so at the end of the function StartInterface will eliminate this variable and the window, the solution is to make the mainWindow member of the class, so the context will be the class and no longer the function, so it will stay correctly.

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

这篇关于关闭并打开新窗口 PYQT5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆