0

I'm a beginner in python classes and i have a little problem with this code (it's a pyqt code). The function globalprogress is called from another file but when he call the function UpdateBar, my computer keeps telling me that "Ui_IA object has no attribute 'progression'" but that's not true.

Does somebody knows if I did something wrong here? Thank you.

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_IA(object):

    def setupUi(self, IA):
        IA.setObjectName("IA")
        IA.resize(931, 570)
        self.centralWidget = QtWidgets.QWidget(IA)
        self.centralWidget.setObjectName("centralWidget")
        self.progression = QtWidgets.QProgressBar(self.centralWidget)
        self.progression.setGeometry(QtCore.QRect(50, 450, 831, 61))
        self.progression.setProperty("value", 0)
        self.progression.setObjectName("progression")

    def UpdateBar(self):
        self.progression.setValue(percentage)

def globalprogress(number):
    global percentage
    UIIA = Ui_IA()
    percentage = int(number)
    UIIA.UpdateBar()
  • You have to call the setupUI method that allows you to fill in the widgets, I recommend you check the docs: http://pyqt.sourceforge.net/Docs/PyQt5/designer.html#using-the-generated-code – eyllanesc Feb 19 '19 at 18:38

3 Answers3

0

the function setupUI(), in which the progression attribute is set, is not called by default. You can use __init__(), which will automatically run as soon as the class is instantiated (i.e. an object of the class is made), or force setupUI() to run within the globalprogress() function.

sanyassh
  • 8,100
  • 13
  • 36
  • 70
Bilbo Baggins
  • 47
  • 1
  • 6
0

You have to call setupUi() on your UIIA object before executing UpdateBar().

Mike
  • 16
  • 1
  • 2
0

Try it:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_IA(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(931, 570)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")

        self.progression = QtWidgets.QProgressBar(self.centralWidget)
        self.progression.setGeometry(QtCore.QRect(50, 450, 831, 61))
        self.progression.setProperty("value", 0)
        self.progression.setObjectName("Progression")

        MainWindow.setCentralWidget(self.centralWidget)    

    def UpdateBar(self, MainWindow, percentage=0):
        self.progression.setValue(percentage)
        self.progression.show()

def globalprogress(number):
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_IA()
    ui.setupUi(MainWindow)
    MainWindow.show()
    ui.UpdateBar(MainWindow, number) 
    sys.exit(app.exec_())

if __name__ == "__main__":
    globalprogress(55)    

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33