Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

python - Close and Open new Window PYQT5

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

How can I do it?

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...