[PySide2] How come this code is not getting executed in a line by line order?
-
Hello,
I am using a code to connect my Raspberry Pi to the WiFi and it works.
But changing the text on the labels, that doesn't work in t he correct order.For example, this is the code to view a list of WiFi networks and add them to a combobox:
self.label_connectionStatus2.setText("Refreshing...") out = subprocess.Popen('sudo rfkill unblock 0', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out = subprocess.Popen('ifconfig wlan0 up', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out = subprocess.Popen('sudo /sbin/iwlist wlan0 scan | grep ESSID', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout = out.communicate()[0] stdout = stdout.decode("utf-8") wifilist = stdout.split('"')[1::2] self.comboBox_WiFiName.clear() self.comboBox_WiFiName.addItems(wifilist)
The code works but the part where I use 'setText("Refreshing...")' on the label, that part is executed at the end, it shows up after the code below it has been executed.
I even tried adding a 'time.sleep(10)' after the 'self.label_connectionStatus2.setText("Refreshing...")' line, but nothing changed.
Why is this happening?
-
Hey
Some GUI's are only drawn after code blocks are executed or if you call a function for drawing.
In Qt you could try running this to signal Qt to process pending events, or emit a signal for change.self.label_connectionStatus2.setText("Refreshing...") app.processEvents()
-
@MEMekaniske Thank you! Works perfectly!
-
@lolcocks said in [PySide2] How come this code is not getting executed in a line by line order?:
@Denni-0 Thank you so much good sir, this does clarify how it works internally. Thank you for sharing and improving my knowledge. :)
You would have learned what dennis said + a lot more from following the link I gave to the documentation.. :)
-
I will throw in: personally, I would have tried
self.label_connectionStatus2.repaint()
https://doc.qt.io/qt-5/qwidget.html#repaint, first, unless a Qt expert says otherwise. (Not
update()
here, that requires aprocessEvents()
.) Provided it works, no need to spin aprocessEvents()
with unknown consequences if all you want to achieve is making text immediately visible to user. If it does not work, or if you see any unacceptable "flicker" (which I doubt you will here), then I would have gone forprocessEvents()
.Of course, really the subprocess should be run in a separate thread, or asynchronously, and this whole matter of UI updates being blocked would not be an issue. If the OP chose to use Qt's
QProcess
to do the commands it would all be asynchronous with signals/slots and there would be no issue. This is a better choice than Pythonsubprocess
in a Qt GUI application, but that is a matter of preference. (Incidentally, your code for running the 3 sub-processes is subject to race conditions: it runs them in parallel not in series.) -
@MEMekaniske
I'm just a purist :)repaint()
has no side-effects and is self-contained. If you do aprocessEvents()
you do not know that only that label will be updated, there could be other pending events/signals which could do potentially other things.