对象没有属性,但定义了属性

时间:2019-09-25 15:58:38

标签: python-3.x pyqt5

我已经在自定义类中定义了一个属性,但是当我尝试访问它时,我仍然收到AttributeError。

class SMainWindow(QMainWindow):
    def __init__(self):
        # Constructor
        super(SMainWindow, self).__init__()
        self.myapp = PyQtApp()
        self.layout = QVBoxLayout()
        self.label_text = ''
        self.settings = scrudb.retrieve_settings('current')
        self.competition = self.retrieve_competition()
        self.set_competition(self.competition.id)
        self.label = QLabel(self.label_text)
        self.button_scrutineer = QPushButton('Scrutineer Competition')
        self.button_comps = QPushButton('Change Competition')
        self.button_comp = QPushButton('Edit Competition Details')
        self.button_dancers = QPushButton('Add/Edit Competitors')
        self.button_judges = QPushButton('Add/Edit Judges')
        self.button_dancerGroups = QPushButton(
            'Define Competitor Groups & Dances')
        self.button_import = QPushButton('Import CSV')
        self.button_delete = QPushButton('Delete Competition')
        self.button_exit = QPushButton('Exit')
        self.button_comps.clicked.connect(self.select_competition)
        self.button_delete.clicked.connect(self.delete_competition)
        self.button_exit.clicked.connect(self.exit_app)
        if (self.competition == None):
            self.disable_buttons()
        self.layout.addWidget(self.label)
        self.layout.addWidget(self.button_scrutineer)
        self.layout.addWidget(self.button_comps)
        self.layout.addWidget(self.button_comp)
        self.layout.addWidget(self.button_dancers)
        self.layout.addWidget(self.button_judges)
        self.layout.addWidget(self.button_dancerGroups)
        self.layout.addWidget(self.button_import)
        self.layout.addWidget(self.button_delete)
        self.layout.addWidget(self.button_exit)
        self.myapp.setLayout(self.layout)

    def set_competition(self, comp_id):
        self.competition = scrudb.retrieve_competition(comp_id)
        if (self.competition != None):
            self.label_text = ('<center>Competition:<br><strong>%s</strong><br>%8s<br>%s</center>' % (self.competition.name, self.get_formatted_date(self.competition.eventDate), self.competition.location))
            self.label.setText(self.label_text)
            self.settings.lastComp = self.competition.id
            scrudb.set_settings(self.settings)
            return self.competition
        else:
            self.label_text = ('<center>No Competition Selected</center>')
            return None

set_competition中的文件“ /Users/majikpig/mu_code/src/main/python/scruinterface1.py”,第182行

self.label.setText(self.label_text)

AttributeError:“ SMainWindow”对象没有属性“ label”

1 个答案:

答案 0 :(得分:0)

您需要更改字段的顺序...在set competition函数中,您尝试访问尚未定义的字段。

self.set_competition(self.competition.id)
self.label = QLabel(self.label_text)