Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt Not updating label

Tags:

python

pyqt

I've created a simple application to transfer files using Paramiko and SFTP. I also wanted to have a statusbar (a QLabel) to inform the user when I'm downloading/uploading stuff. So, my function to upload looks something like this:

def upload(self):
        self.statusLabel.setText('Uploading')
        local = str(self.uploadLineEdit.text())
        filename = os.path.basename(local)
        remote = "/home/" + self.userName + "/testdata/" + filename
        self.ftp.put(local, remote)
        self.uploadedFileName = filename
        self.statusLabel.setText('Upload Finished')

Notice that before starting the upload I change the statusbar to uploading, and when upload is done, I change it to upload finished.

However, what actually happens is that the "Uploading" message is never displayed on the label - it just goes straight to "Upload Finished". I suspect this is because the changes happen only after the function returns.

How do I get the label to change at the start of the upload process?

like image 734
hoodakaushal Avatar asked Feb 10 '23 22:02

hoodakaushal


1 Answers

You might need to force processing of events after changing the label text. Try adding:

QApplication.processEvents()

after setting the label text.

Please note that for a reason not known to me, PyQt and PySide both tend to have problems with processEvents, which sometimes needs to be executed multiple times to take effect. So, if it does not work after adding a single processEvents(), try adding it twice, or even multiple times.

like image 131
Andrzej Pronobis Avatar answered Feb 15 '23 10:02

Andrzej Pronobis