简体   繁体   中英

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

Here's a complete working example. The main window is just a QMainWindow . It will open a QDialog on button click. Their ui definitions:

# file ui_main.py
from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow:
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(442, 205)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(180, 80, 75, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))

#file ui_dialog.py
from PyQt5 import QtCore, QtWidgets

class Ui_Dialog:
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(508, 300)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(150, 250, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

In another file, define the MainWindow and PreferenceDialog classes:

# file main.py
from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog

from ui_dialog import Ui_Dialog
from ui_main import Ui_MainWindow

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.pushButton.clicked.connect(self.newusr_clk)

The newuser_clk slot will hide MainWindow and open up a new QDialog box via dialog.exec() . When the QDialog is closed, MainWindow is showed once again.

    def newusr_clk(self):
        self.hide()
        dialog = PreferencesDialog(parent=self)
        if dialog.exec():
            pass # do stuff on success
        self.show()

Here is the PreferenceDialog class:

class PreferencesDialog(QDialog):
    def __init__(self, parent=None):
        super(PreferencesDialog, self).__init__(parent)

        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

And here's how to run the script:

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

hope that helps!

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)

This is the easy way to hide the current window and open the new window

class placeOrder(object):
    def __init__(self, Form):
       Form.setObjectName("Form")
       Form.setGeometry(0, 40, 1250, 650)
       self.orderWindow = Form

    def newWindow(self):
       from newWindow import newWindow
       self.windowNew = QtWidgets.QMainWindow()
       self.newWindowObj=newWindow(self.windowNew)
       self.orderWindow.hide()
       self.windowNew.show()

if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
orderWindow = QtWidgets.QMainWindow()
ui = placeOrder(orderWindow)
orderWindow.show()
sys.exit(app.exec_())   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM