The PyQt window opens only once

32 Views Asked by At

I ask your question through a translator, so please understand that it may be a bit awkward.

When you run it for the first time(app.py), the Main Menu window and Main Menu Class window are created properly, but when you go through the loop once and return to the beginning, the program ends. I don't know why this is happening. If the conditional statement elif ~ == 2 is passed, the message QWidget: Must construct a QApplication before a QWidget appears on the console, and in other cases, it ends without any console message. What I want to implement is to make it repeat infinitely. First, the menu window (MainMenu) appears, and depending on what you select here, the window in if ~ == 1, later the window in if ~ == 2 appears, and then the window in the if statement appears. This infinite repetition is implemented so that when the window closes, the menu window opens again. Should I do this? Thank you for your reply.

from src.src import *
from src.windows import *


def main():
    while True:
        # Main Menu Page
        MainMenuApp = QApplication(sys.argv)
        MainMenuClass = MainMenuWindow()
        MainMenuClass.show()
        MainMenuApp.exec_()

        if MainMenuClass.select_menu_num == 1:
            # File Enc Page
            FileEncApp = QApplication(sys.argv)
            FileEncClass = FileEncWindow()
            FileEncClass.show()
            FileEncApp.exec_()
        
        elif MainMenuClass.select_menu_num == 2:
            # File Dec Page
            pass

main()

class FileEncWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi("program/src/FileEncWindow.ui", self)

        self.setWindowTitle(src.program_title)
        self.ui.title_label.setText("File Encryption")

        ...

    def closeEvent(self, event):
        if self.enc_working:
            reply = QMessageBox.question(self, src.program_title, 'Do you want to cancel your current action and return to the menu?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if reply == QMessageBox.Yes:
                self.close()
                print("enc window exited")
            else:
                event.ignore()
                print("enc window exit canceled")
        else:
            self.close()




class MainMenuWindow(QMainWindow, MainMenu_ui):
    def __init__(self) -> None:
        super().__init__()
        self.setupUi(self)
        
        self.setWindowTitle(src.program_title)
        self.title_label.setText(src.program_title)
        
        ...
    
    def closeEvent(self, event):
        if self.select_menu_num != None:
            self.close()
            return None
        
        reply = QMessageBox.question(self, src.program_title, 'Are you sure you want to quit the program?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            self.close()
            print("Program Exted")
            exit(0)
        else:
            event.ignore()
            self.select_menu_num = None
            print("Program Exit Canceled")

I tried creating several variables in one window that contain QApplication(sys.argv) and classes. It worked well, but of course it seemed too inefficient and out of place.

0

There are 0 best solutions below