python - 关闭并打开新窗口 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
        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成为类的成员,所以context 将是类,不再是函数,因此它将保持正确。

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

关于python - 关闭并打开新窗口 PYQT5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47604561/

相关文章:

python - 自动重新加载在 Bottle 框架中不起作用

python - 诗歌安装 - ParseConstraintError 无法解析版本约束 : install ERROR

python - 检查按下的键是PyQt中的字母还是空格

python - 使用 atexit 终止线程时脚本卡在退出处

python - 我该如何着手只打印大写锁定的名字的第一个字母以及以下内容?

python - 应该避免通配符导入吗?

python-3.x - 使用 PyQt5 在浏览器中实现 Web 检查

python - 如何在Python中从多列中的行组中找到2个最大值,并在输出中显示其行索引和列索引

python - 向 Python 中的特定角色发送私有(private)消息

Python:如何创建一个要求准确字数的函数?