关闭并打开新窗口PYQT5
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关闭并打开新窗口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_())
我的主要问题是当我按下按钮时我无法打开新窗口
答案
在QApplication
应用程序中只能有一个PyQt
,所以如果你已经创建了它,那就不要再做了。
另一个问题是变量只存在于上下文中,在你的情况下是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_Clicked(self):
dialog.hide()
dialog1.show()
以上是关于关闭并打开新窗口PYQT5的主要内容,如果未能解决你的问题,请参考以下文章