以LCD形式显示系统时钟时间

2024-10-04 01:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想以LCD格式显示系统时钟时间。我还希望使用hh:mm:ss格式显示时间。我的代码在下面。但当我运行它时,它并不像我所期望的那样。有人能解释一下为什么吗?在

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.showlcd)
        timer.start(1000)
        self.showlcd()

    def initUI(self):

        self.lcd = QtGui.QLCDNumber(self)
        self.setGeometry(30, 30, 800, 600)
        self.setWindowTitle('Time')

        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.lcd)
        self.setLayout(vbox)

        self.show()

    def showlcd(self):
        time = QtCore.QTime.currentTime()
        text = time.toString('hh:mm:ss')
        self.lcd.display(text)


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Tags: selflcdmainexampledef格式hhsys
1条回答
网友
1楼 · 发布于 2024-10-04 01:33:02

QLCDNumber具有固定数字显示(^{}),其默认值为5。所以你的文本被截断到最后5个字符。您应该设置一个适当的值(在您的情况下是8)。在

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.showlcd)
        timer.start(1000)
        self.showlcd()

    def initUI(self):

        self.lcd = QtGui.QLCDNumber(self)
        self.lcd.setDigitCount(8)          # change the number of digits displayed
        self.setGeometry(30, 30, 800, 600)
        self.setWindowTitle('Time')

        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.lcd)
        self.setLayout(vbox)

        self.show()

    def showlcd(self):
        time = QtCore.QTime.currentTime()
        text = time.toString('hh:mm:ss')
        self.lcd.display(text)


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

相关问题 更多 >