Function that is connected to button opens and regexp replace some text:
def process(self):
# first do this!!
self.label.setText('Processing for 5-10 sec.....')
self.button.setEnabled(0)
# and only then heavy files operations
file = open(self.filename, mode='r', encoding='utf-8')
text = re.sub(r'^ (.+)\n ', r' \[\1\]\n ', file.read(), flags=re.MULTILINE)
newfile = open(self.filename+'temp', mode='w', encoding='utf-8')
file_new.write(text)
self.label.setText('Ready')
Python 3.1, PyQt 4.8.2
File is big enough, operation takes ~10sec.
I want, when button is pressed, fist replace some text in label and disable button. So user can see, that he should wait some time.
But nothing happens. System just hangs for 10 seconds, and then “ready” label appeared and button disabled.
How can I make Qt first do label change and button disabled, and only after this do file operation?
I can imagine two solutions.
First, try calling QCoreApplication.processEvents() after you set the label’s text and disable the button.
Second, if the first solution doesn’t work, split your method in two. Then, in the first method, set the label’s text, disable the button, and use QTimer.singleShot() to call your second method. It will be called asynchronously, so Qt event processing loop will have a chance to update the GUI.