EwokHugz
EwokHugz

Reputation: 345

Open a window and close a window in PyQt5

I am trying to load a new class with a UI window from a welcome screen, and at the same time close the welcome screen. When I click the button to go to open the new window it opens fine, but I would just like to close the first window on the action event.

I have tried .hide(), .terminate(), .destroy(), and the window still stays.

This is the relevant code:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()                
        #self.email_blast_widget = EmailBlast()
        #self.setCentralWidget(self.email_blast_widget)
        self.welcome = OpeningWindow()
        self.setCentralWidget(self.welcome)        
        bar = self.menuBar()
        file_file = bar.addMenu('File')         
        file_edit = bar.addMenu('Edit')

    def load_email(self):
        self.mail = EmailBlast()        
        self.mail.show()
        self.welcome = OpeningWindow()
        self.destroy()

class OpeningWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.welcome = QtWidgets.QLabel("Welcome To Zach's \nEmail Blast Widget")
        self.email_button = QtWidgets.QPushButton("Email")
        self.csv_button = QtWidgets.QPushButton("CSVs")
        self.exit_button = QtWidgets.QPushButton("Exit")

        self.init_ui()

 def init_ui(self):
    # set layout to place widgets
    self.email_button.clicked.connect(self.load_email)
    self.csv_button.clicked.connect(self.load_csv)
    #self.exit_button.clicked.connect(self.exit)
    self.exit_button.clicked.connect(QtWidgets.qApp.quit)


class EmailBlast(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()     
        # create widgets

Upvotes: 2

Views: 24684

Answers (2)

hamed
hamed

Reputation: 81

You can use self.close() in every class you want.

Upvotes: 0

EwokHugz
EwokHugz

Reputation: 345

self.exit_button.clicked.connect(QtWidgets.qApp.quit)

This solution works for my purposes, however, if you are trying to close a window without shutting down the application this will not work. Use self.close()

Upvotes: 5

Related Questions