繁体   English   中英

如果 class PyQt6 在外面打电话 Function

[英]Call Function outside if the class PyQt6


我想在 Window class 中执行 nextstep() function,但在 ZA2F2ED4FDC40C2CBBD4ZC2 之外。
function 在 window.nextstep() 上调用,但未设置 label 文本。
错误信息:
 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'.

这是代码:

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

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. 然后,您需要先初始化Window class 实例,然后才能运行window.nextstep() 还要求还必须初始化QApplication实例。

例如。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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