0

I have 2 different windows, my main window created in my first file and a QDialog window where the code is located in a second file... What I want to do is to be able from file2 to close the main window created in file1, close also the QDialog from file2 and create a new window in file3. The application must NOT quit.

Creating a new window with the code in file3 would be easy, I am not worried about that... The problem is that in file2, I'm unable to tell the main window to close.

Here is the code from the 2 files to be more clear...

main.py:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QRegion, QPalette, QPixmap, QBrush
import creation_partie
# import sys


class Menu(QMainWindow):
    def __init__(self):
        super(Menu, self).__init__()

     # *SOME OTHER CODE NOT NEEDED FOR THIS QUESTION*

        self.init_ui()
        self.setWindowTitle("Bienvenue dans Skip-Bo!")

     # *SOME OTHER CODE NOT NEEDED FOR THIS QUESTION*


    def center(self):
        # *SOME OTHER CODE NOT NEEDED FOR THIS QUESTION*

    def quitter(self):
        self.close()

    def creer_nouvelle_partie(self):
        self.fenetre_creer_une_partie = creation_partie.NouvellePartie()
        self.fenetre_creer_une_partie.setModal(True)
        self.fenetre_creer_une_partie.show()

    def next_step(self):
        pass

    def init_ui(self):
        # --------------- Paramètres de la fenêtre --------------------
        self.resize(1280, 720)
        self.center()
        self.background_picture2.load('background_menu2.png')
        self.palette.setBrush(self.backgroundRole(), QBrush(self.background_picture2))
        self.setPalette(self.palette)
        # self.background_picture.setStyleSheet("background-image: url(background_menu.png);")
        # self.setCentralWidget(self.background_picture)
        # self.background_picture.setScene(self.scene)
        # # --------------- Fin des paramètres -------------------------

        # *SOME OTHER CODE NOT NEEDED FOR THIS QUESTION*


if __name__ == '__main__':
    app = QApplication([])
    menu = Menu()
    menu.show()
    app.exec_()

creation_partie.py:

from PyQt5.QtWidgets import *
# from PyQt5.QtCore import *
# from PyQt5.QtGui import QRegion, QPalette, QPixmap, QBrush
import main

class NouvellePartie(QDialog):
    def __init__(self):
        super(NouvellePartie, self).__init__()
        # *SOME OTHER CODE NOT NEEDED FOR THIS QUESTION*
        self.init_ui()

    def init_ui(self):
        # --------------- Paramètres de la fenêtre --------------------
        self.resize(640, 325)
        self.center()
        # *SOME OTHER CODE NOT NEEDED FOR THIS QUESTION*

    def texte_change(self):
        # *SOME OTHER CODE NOT NEEDED FOR THIS QUESTION*

    def center(self):
        # *SOME OTHER CODE NOT NEEDED FOR THIS QUESTION*

    def annuler(self):
        self.close()

    def confirmer(self):
        print("Yup")
        # send informations to the server backend of the game
        self.close()
        main.menu.close()




if __name__ == '__main__':
    app = QApplication([])
    nouvelle_partie = NouvellePartie()
    nouvelle_partie.show()
    app.exec_()

So... what I want to do, is that in the fonction confirmer in creation_partie.py be able to close menu from main.py. menu is a QMainWindow and fenetre_creer_une_partie is my QDialog.

Right now, the code compile and run fine until I click on the button that should close menu. PyCharm is not raising compiling error formain.menu.close()`, but when the code is about to be executed (at the moment I click my button), I get the following error:

module 'main' has no attribute 'menu'

How can I fix that?

P.S.: Sorry for the french variable and class names, I a french Canadian :) .

Thanks!

EDIT: Since the question has been closed, I will put my solution here: Basically, I created a function in ˙main.py˙ out of the class which close the main window. After that, the only thing I needed was to call that function in ˙creer_partie.py˙ by using ˙main.my-closing-function˙.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Francis Dolbec
  • 175
  • 1
  • 11
  • `if __name__ == '__main__':` has a characteristic, if you have "n" files and each one has that code then only the first one will be executed, the others will not. Consequently in your case "menu" does not exist so create that object: 1) add `self.menu = Menu()` on `__init__` and change to `self.menu.close()` – eyllanesc Jun 05 '20 at 02:21
  • Euh... I did understand that ˙creer_partie˙ can't see menu, by reading your link... I did add ˙self.menu = Menu()˙ in the ˙__init__˙ of my class ˙NouvellePartie˙, but when I run the code ˙self.menu.close()˙ it doesn't close the main window... – Francis Dolbec Jun 05 '20 at 02:33
  • I did manage to find an easy solution... Just look at my edit in the main question. – Francis Dolbec Jun 05 '20 at 03:49

0 Answers0