[PyQt6] PartⅡ. QLCDNumber Class

QLCDNumber

QTimer
QTime

关键代码

        timer = QTimer() # 创建一个QTimer对象,是一个重复单次,触发信号的定时器
        timer.timeout.connect(self.showLCD) # 触发条件,即计时器到点,调用showLCD函数

        timer.start(1000) # 单位毫秒,即1s。1s更新一次
        

        def showLCD(self):
        vbox = QVBoxLayout()

        lcd = QLCDNumber()
        lcd.setStyleSheet('background: red')

        vbox.addWidget(lcd)

        time = QTime.currentTime()
        text = time.toString('hh:mm')

        lcd.display(text)

        self.setLayout(vbox)

完整代码

from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtCore import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.setGeometry(200, 200, 700, 400) # 设置窗口大小
        self.setWindowTitle("Python QLCDNumber")
        self.setWindowIcon(QIcon('python.png')) # 设置图片,没有的话不显示

        timer = QTimer() # 创建一个QTimer对象,是一个重复单次,触发信号的定时器
        timer.timeout.connect(self.showLCD) # 触发条件,即计时器到点,调用showLCD函数

        timer.start(1000) # 单位毫秒,即1s。1s更新一次

        self.showLCD()

    def showLCD(self):
        vbox = QVBoxLayout()

        lcd = QLCDNumber()
        lcd.setStyleSheet('background: red')

        vbox.addWidget(lcd)

        time = QTime.currentTime()
        text = time.toString('hh:mm')

        lcd.display(text)

        self.setLayout(vbox)


app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

用designer设计(1,500)随机数生成器

完整代码

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt6 UI code generator 6.3.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets
from random import randint

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(542, 300)
        Dialog.setStyleSheet("QLCDNumber{\n"
"    background: yellow\n"
"}\n"
"QPushButton{\n"
"    background: green\n"
"}\n"
"")
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.lcdNumber = QtWidgets.QLCDNumber(Dialog)
        self.lcdNumber.setObjectName("lcdNumber")
        self.verticalLayout.addWidget(self.lcdNumber)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setObjectName("pushButton")

        # connect signal
        self.pushButton.clicked.connect(self.random_generator)
        self.verticalLayout.addWidget(self.pushButton)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def random_generator(self):
        random = randint(1, 500)
        self.lcdNumber.display(random)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "Generate Random Number"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec())

程序截图

reference

Qt 之 QTimer

posted @ 2022-05-24 09:58  deadright  阅读(82)  评论(0编辑  收藏  举报