Update LCD Number countdown

10,550

The display function returns None, so doing None("%SS") obviously isn't allowed.

self.lcdNumber.display(i) is enough to show the countdown!


To let Qt paint the widgets while looping run the countdown from another thread. See an example.

import time
from threading import Thread
from PyQt4.QtGui import QApplication, QMainWindow, QLCDNumber

class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.lcdnumber = QLCDNumber(self)
        self.resize(400, 400)

        t = Thread(target=self._countdown)
        t.start()

    def _countdown(self):
         x = 10
         for i in xrange(x,0,-1):
             time.sleep(1)
             self.lcdnumber.display(i)

if __name__ == "__main__":
    app = QApplication([])
    window = Window()
    window.show()
    app.exec_()
Share:
10,550
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm very new to Python and I have made a very simple countdown timer. The GUI was created in Qt Designer. There is a spin box for input of seconds, a start button and an LCD number counter. The counter counts down fine using the code below:

         def start_btn_clicked(self):
             x = self.Minute_spinBox.value()
             for i in xrange(x,0,-1):
                 time.sleep(1)
                 print (i)
    

    So that I could see what was happening as I played around with it, I added the print instruction so that it shows the countdown in the Python console as it runs. I then thought I could maybe quite easily have the LCD number display the countdown with something like:

        self.lcdNumber.display(i)("%SS")
    

    But no matter what I try, I cant get it to show. With the line above, I get the first number displayed, but then I get an error saying:

        self.lcdNumber.display(i)("%SS")
        TypeError: 'NoneType' object is not callable
    

    I have tried so many variations that I no longer know where I started and here was me thinking it would be simple. I'd love to know why I cant get it to display the countdown.