简体   繁体   中英

Dynamically updating QLabel - pyqt

I'm having a bit of an issue. I'm trying to make a monitoring software that monitors a few voltages and other things. I'm using QT 4 Designer and pyqt to make my UI.

My code is structured like this (each one of these is a module):

Click go button ---> start hub ----> connect to device and return an array of info ---> use info and display appropriate values on to various QLabels.

I am setting the QLabels using self.label.setText(data in here).

The problem I have is now I'm trying to make the go button start a loop that repeatedly calls the start hub module (which in turn will grab information from the device, then call a module that prints that information to the QLabels) every 4 seconds or so.

As a test, I called the function 3-4 times in a row with a time.sleep(4) in between. I found that the UI only updates AFTER the last call to start hub.

I'm pretty sure that the issue is that the UI only updates when nothing is processing. But I'm not really sure how to force all the QLabels to update after each iteration. I've tried self.label.repaint() but that didn't do anything.

Can anybody help?

What I'm guessing is that you probably put all tasks(3-4 times hub module call, time.sleep and QLabels setText) into that UI update function, instead of doing that you should wrap the long-running tasks into a separate thread :

import threading

def LongRunningTask(self):
    for i in range(4):
        hub_module_call        
        time.sleep(4)
        self.label.setText

def OnButton(self, event):
    # Click go button
    thread = threading.Thread(target=self.LongRunningTask)
    thread.start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM