Search code examples
pythonpython-3.xpyqt5qt-designer

How do I hide current window while opening a new one using PyQt5


I've linked another file to a pushButton in my main window. Every time I click it, the new window does open but the old (primary window) is still there. How do I close it or hide it without affecting the new window.

PS: I'm a newcomer here. Apologies for any idiocies or mistakes while posting this query on my part. Thank you!

Here's the relevant code:

    import sys  
    from PyQt5 import QtCore, QtGui, QtWidgets  
    from create2 import Ui_MainWindow  
    from PyQt5.QtWidgets import QMainWindow, QWidget  
    from PyQt5.QtCore import QMargins  

    class Ui_Form(object):  
        def setupUi(self, Form):  
            Form.setObjectName("Form")  
            Form.resize(813, 655)  
            self.widget = QtWidgets.QWidget(Form)  
            self.widget.setGeometry(QtCore.QRect(190, 50, 501, 591))  
            self.widget.setObjectName("widget")  
            .  
            .  
            .  
        def newusr_clk(self):  
            self.mainwin=QMainWindow()  
            self.ui=Ui_MainWindow()  
            self.ui.setupUi(self.mainwin)  
            self.mainwin.show() 

Solution

  • So I finally figured out how to hide the current window while opening a new one. This was the code in my case (since I was working with a Form):

    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Form = QtWidgets.QWidget()
        ui = Ui_Form()
        ui.setupUi(Form)
        Form.show()
        sys.exit(app.exec_())
    

    So I used this command to hide the current window:

    Form.setVisible(False)