0

I'm trying to create a GUI for my python code. I ran into a problem, where i'd like to show a label with text "Please wait" and paralel with this i'd like to sleep my code for 1.25s. However, it is going into sleep without showing the label. It seems to me it somehow "skips" the .show()

Here is the snippet from the code:

def status_check(self):
            self.varakozas.setText("Please wait...")
            self.varakozas.show()
            time.sleep(1.25)
            sudoPassword = self.sudopassword.text()
            command = "some command"
            passtmeg = "echo '"+sudoPassword+"' | sudo -S "+command
            line = subprocess.Popen(["bash", "-c", passtmeg],stdout=subprocess.PIPE,shell=True)
            status_of = str(line.communicate()[0])
            status_of_to_log = status_of.translate({ord(translate_table): "" for translate_table in "'b"})
            logging.info('Status: '+ status_of_to_log[:-2])
            if ("xy" in status_of) or ("Starting" in status_of):
                self._status.setText("xy is running")
                self.varakozas.hide()
            else:
                self._status.setText("xy is stopped")
                self.varakozas.hide()
Jazz
  • 13
  • 1

1 Answers1

-1

As you said self.varakozas is QLabel so i predict it is child of your window.

If you call show() Qt will only schedule paint event for this widget and paint event is called by event loop. Event loop is executed once the execution of this method is done. So the sleep is called first because it is before the end of scope. This is standard behaviour of Qt event loop environments.

But there is a workaround. You can call paint event manually by repaint() method.

Try this:

def status_check(self):
            self.varakozas.setText("Please wait...")
            self.varakozas.show()
            self.repaint()
            time.sleep(1.25)
Erik Šťastný
  • 1,487
  • 1
  • 15
  • 41