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
AttributeError: type object 'MainWindow' has no attribute 'progressBar'
from PyQt5 import QtCore, QtGui, QtWidgets
def setupUi(self, MainWindow):
self.progressBar = QtWidgets.QProgressBar(self.widget)
from PyQt5 import QtWidgets
from UImainwindow import Ui_MainWindow
from threading import Thread
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
# access variables inside of the UI's file
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
APP = QtWidgets.QApplication(sys.argv)
Bi = B()
MAINWIN = MainWindow()
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): self.__target.setprogressBar(48) MAINWIN = MainWindow() bi = B(MAINWIN) bi.start() MAINWIN.show()