Skip to content

[PyQt5] Tutorial(8) QTimer, QlcdNumber

Recently, the GUI I’m developing is often in a state of cessation. I think maybe I need “QThread” to help me. But when I learning how to use QThread, I was surprised to find one thing.

The tutorial in internet, it show me the LCD component is very slow, I need to use QThread to solve it ……

But, my computer seems to be great. The above phenomenon did not happen at all.

But again, after I researching QTimer, I finally found that using QTimer will open another thread.

So sad, I thought my notebook was great.

My notes maybe note QThread someday, but I used it in my program and it didn’t improve performance. I guess my problem is using too many tools filled the memory, so QThread has no effect is normal.

OK, today I will note how to use QTimer and QlcdNumber!

if you want to read the official tutorial, maybe you can refer here: https://www.riverbankcomputing.com/static/Docs/PyQt5/index.html?highlight=qicon


QTimer

As the name suggests, QTimer is a kind of timer in PyQt5. We can use it to time.

First we take a look for sample code. Today I give up to put on my ui file, you can put the components freely.

My interface has a LCD components, and two buttons.

I want to use Qtimer for timing, Add LCD display every 1 second.

# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets
from PyQt5.QtCore import QTimer
from thread_test import Ui_MainWindow
import sys


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # QTimer
        self.timer = QTimer()

        # QPushButton
        self.ui.pushButton.clicked.connect(self.timeGo)
        self.ui.pushButton_2.clicked.connect(self.timeStop)

        # Other
        self.timer.timeout.connect(self.LCDEvent)
        self.s = 0

    def timeGo(self):
        self.timer.start(100)

    def timeStop(self):
        self.timer.stop()

    def LCDEvent(self):
        self.s += 1
        self.ui.lcdNumber.display(self.s)


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


We can see:

def timeGo(self):
    self.timer.start(100)


timeGo() is start to time. It should be noted that the unit here is ms.

That is, one ms is equal to one thousandth of a second. Here is how fast I set it up, once every 0.1 second.

self.timer.timeout.connect(self.LCDEvent)


timeout will trigger LCDEvent() function.

def LCDEvent(self):
    self.s += 1
    self.ui.lcdNumber.display(self.s)


Display our LCD components:

timeStop() is let my timer sleep.


QLCDNumber

QLCDNumber is a LCD components, it can show a LCD in our GUI. And we can show number on it.

You just only use display() function.

  • setBinMode(): Binary
  • setOctMode(): Octal
  • setDecMode(): Decimal (Default)
  • setHexMode(): Hexadecimal

In addition, this component can also display text.

self.ui.lcdNumber.display('22:10')


Output:

So, if we want to create a alarm clock, we can use LCDNumber to make it.


Read More

Leave a Reply