gpt4 book ai didi

python - 关闭并打开新窗口 PYQT5

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:30 25 4
gpt4 key购买 nike

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

我该怎么做?

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com