0

When I use the function time.sleep() in the terminal, it is no problem. When I use PyQt and want to use a label, it crashes and/or only shows the last number.

By the way: I want a function, that counts e.g. a year from 2020 to 2030 in various speed, that the user can change and the year should be shown in a label.

Thanks a lot for your help.

# timer that counts in the future with various speed // still crashing
def timer(self):
    x=datetime.datetime.now()
    z=x.ctime()
    self.ui.labelDateTime.setText(z)
    var=x.year
    while True:
        if var==2030:
            break
        else:
            var+=1
            y=x.replace(year=var)
            z=y.ctime()
            self.ui.labelDateTime.setText(z)
            time.sleep(0.5)
    print("You are dead")
wjandrea
  • 28,235
  • 9
  • 60
  • 81
GiantLab.de
  • 15
  • 2
  • 5

1 Answers1

0

You should call QtCore.QCoreApplication.processEvents() within your for loop to make the Qt's event loop proceed the incoming event (from keyboard or mouse or sleep).

Although calling QtCore.QCoreApplication.processEvents() works , I have read in many places on the web that it should be a last resort. Unfortunately none of the sources clearly explain why -- but see for example

So it does seem allowed, but in general, it seems to be better design to use QTimer or QThread.

Jasar Orion
  • 626
  • 7
  • 26