Search code examples
pythonpyqtpyqt5pyqt6

Call Function outside if the class PyQt6



I want to execute the nextstep() function in the Window class, but outside the class.
The function is called on window.nextstep() but the label text is not set.
Error message:
 File "<stdin>", line 1, in <module>.
  File "D:\python\mcmods\mcmods\src\guitools.py", line 56, in setupApp
    window.nextstep()
  File "D:\python\mcmods\mcmods\src\guitools.py", line 39, in nextstep
    self.label.setText("Konfigurationen werden erstellt")
AttributeError: 'Window' object has no attribute 'label'.

Here ist the code:

def setupApp():
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setFixedSize(320,250)
            self.setWindowTitle("Wird geladen...")

            self.setWindowFlags(Qt.WindowType.FramelessWindowHint)
            #self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
            prog_bar = QProgressBar(self)
            prog_bar.setGeometry(50, 100, 250, 30)
            prog_bar.setValue(0)
            label = QLabel("X wird gestartet...", self)
            label.setGeometry(90, 100, 250, 30)

        def nextstep(self):
            if os.path.exists(str(Path.home) + "\mcmods") == False:
                appdata = AppDataPaths("mcmods")
                appdata.setup()
                adpath = str(appdata.app_data_path)
                self.label.setText("Konfigurationen werden erstellt")
                os.system('mkdir "' + adpath + '"')
                sleep(1)
                newconfig = open(adpath + "\config.json", "w+")
                sleep(1)
                newconfig.write(str('''{
                    "version": "''' + VERSION + '''",
                    "DataFolder": "''' + adpath + "\mcmods\\" + '''",
                    "allowModImport": false
                    }'''))
                newconfig.close()

Solution

  • You didn't include where you execute the nextstep method, but either way I would suggest removing the function that encapsulates the Window class and making the window a top level class. Then you would need to initialize a Window class instance before you can run window.nextstep(). It is also required that a QApplication instance must be initialized as well.

    For example.

    from PyQt6.QtWidgets import *
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setFixedSize(320,250)
            self.setWindowTitle("Wird geladen...")
    
            self.setWindowFlags(Qt.WindowType.FramelessWindowHint)
            #self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
            prog_bar = QProgressBar(self)
            prog_bar.setGeometry(50, 100, 250, 30)
            prog_bar.setValue(0)
            self.label = QLabel("X wird gestartet...", self)  # self.label
            self.label.setGeometry(90, 100, 250, 30)          # self.label
    
        def nextstep(self):
            if os.path.exists(str(Path.home) + "\mcmods") == False:
                appdata = AppDataPaths("mcmods")
                appdata.setup()
                adpath = str(appdata.app_data_path)
                self.label.setText("Konfigurationen werden erstellt")
                os.system('mkdir "' + adpath + '"')
                sleep(1)
                newconfig = open(adpath + "\config.json", "w+")
                sleep(1)
                newconfig.write(str('''{
                    "version": "''' + VERSION + '''",
                    "DataFolder": "''' + adpath + "\mcmods\\" + '''",
                    "allowModImport": false
                    }'''))
                newconfig.close()
    
    app = QApplication([])
    window = Window()
    window.nextstep()
    app.exec()