I want to produce simple GUI, a label and a push button. When I press the push button it should invoke a separate thread (the GUI unfreezes ). In the thread I have simple loop
for i in range(4):
self.label.setText(str(i))
time.sleep(1)
the loop must change the label text each second. So I expect to see the GUI how the label changes 0, 1, 2, 3 each second. This is my code:
self.pushButton.clicked.connect(self.clicked_1)
self.label.setText("Lubo1")
def clicked_1(self):
for i in range(4):
self.label.setText(str(i))
app.processEvents()
print("Sleep!")
time.sleep(1)
print("Weak up!")
x = threading.Thread(target=clicked_1, args=(1,))
x.start()
The code works exactly as I expect when I comment x = threading.Thread(target=clicked_1, args=(1,)) and uncomment app.processEvents().
However when app.processEvents() is commented and x = threading.Thread(target=clicked_1, args=(1,)) is uncommented the code gives an error:
AttributeError: 'int' object has no attribute 'label'
I don't want to use app.processEvents() since I know this is not the right way. The correct way is to use threading, but I don't know how to overcome the AttributeError: 'int' object has no attribute 'label' error. Please help.
Best Regards, thank you in advance.
Lubomir Valkov