T. Di Pietro T. Di Pietro - 1 year ago 104
Python Question

Can't access the variable inside the generated code from pyqt5

this is my first post and I would like to thanks the amazing community of Stack Overflow for helping me during all this years!

However, after extensive research, i couldn't find a solution to my problem. I have the file generated by QtCreator which contain a progressbar.
In my code, I have 2 class, and 1 is a Thread. This Thread must change the value of the progressbar, but I completely fail to do so.

I can't access the variable from my Thread, but i can from the init of Mainwindow. I think the problem is the nature of the "self" variable in setprogressBar, but I'm literally stuck finding out what it is..

When i tried to execute this code, here is the result :

File "C:\test.py", line 14, in setprogressBar
self.progressBar.setProperty("value", pourcentage)
AttributeError: type object 'MainWindow' has no attribute 'progressBar'

File A, generated with QtCreator :

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.progressBar = QtWidgets.QProgressBar(self.widget)

File B, my code :

from PyQt5 import QtWidgets
from UImainwindow import Ui_MainWindow
from threading import Thread
import sys

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
# access variables inside of the UI's file
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self) # gets defined in the UI file
self.progressBar.setProperty("value", 24) #This is working

def setprogressBar(self, pourcentage):
self.progressBar.setProperty("value", pourcentage) #This is not

class B(Thread):
def __init__(self):

def run(self):
MainWindow.setprogressBar(MainWindow ,48)

APP = QtWidgets.QApplication(sys.argv)

Bi = B()

MAINWIN = MainWindow()


Thx a lot for the help guys !

Answer Source

MainWindow is a class, not an object. What you should do instead is something like:

class B(Thread):
    def __init__(self, target):
        self.__target = target

    def run(self):

MAINWIN = MainWindow()


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download