0

For example, I want to change the text of a button by binding it to a function from another class. But I get an error on the line self.ui.pushButton.setText("OK"):

AttributeError: 'bool' object has no attribute 'ui'

Here is main.py code:

from PySide6.QtWidgets import QApplication, QWidget

import test_gui

class App(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.ui = test_gui.Ui_Form()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(Second.func)

class Second(App):
    def __init__(self):
        App.__init__(self)

    def func(self):
        self.ui.pushButton.setText("OK")

if __name__ == "__main__":
    app = QApplication([])
    mw = App()
    mw.show()
    app.exec()

Here is test_gui.py code:

# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'test_gui.ui'
##
## Created by: Qt User Interface Compiler version 6.2.4
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QPushButton, QSizePolicy, QWidget)

class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(392, 279)
        self.pushButton = QPushButton(Form)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(120, 110, 75, 24))

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
        self.pushButton.setText(QCoreApplication.translate("Form", u"PushButton", None))
    # retranslateUi

Please tell me what is the problem?

Alex
  • 21
  • 4
  • Your question is unclear. You're never creating an instance of the second class, so what are you trying to do? – musicamante Apr 05 '22 at 18:04
  • @musicamante I want to work with the attributes of one instance of a class through another class. It does not create additional instances. – Alex Apr 05 '22 at 19:04
  • PySide6 Docs: [Using UI files from Qt Designer](https://doc.qt.io/qtforpython/tutorials/basictutorial/uifiles.html). – ekhumoro Apr 05 '22 at 20:21
  • @Alex Sorry but that's a bit confusing. Try to clarify what your actual purpose is, as right now your code doesn't make a lot of sense: you're trying to call `func` like it was a static method (or a class method?), but then that function is written as an *instance* method, since it needs to access `ui`, which is an attribute that only exists on an instance. – musicamante Apr 05 '22 at 23:02

0 Answers0