用PySide编写数字时钟

本文介绍如何使用 PySide 库实现一个显示当前日期和时间的数字时钟。通过 QTimer 定时更新时间,并利用 QLCDNumber 控件展示时间信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#Import required modules 
import sys
from PySide.QtCore import QDateTime, QTimer, SIGNAL
from PySide.QtGui import QApplication , QWidget, QLCDNumber, QDesktopWidget
class MyTimer(QWidget):
    """Our Main Window class for Timer"""
    def __init__(self):
        """Constructor Function"""
        super(MyTimer, self).__init__()
        self.initGUI()
    def initGUI(self):
        self.setWindowTitle('My Digital Clock')
        timer=QTimer(self)
        self.connect(timer, SIGNAL("timeout()"), self.updtTime)
        self.myTimeDisplay=QLCDNumber(self)
        self.myTimeDisplay.setSegmentStyle(QLCDNumber.Filled)
        self.myTimeDisplay.setDigitCount(24)
        self.myTimeDisplay.resize(500, 150)
        self.updtTime() #To Display the current time before call by timer event,Otherwise it will start with 0)
        self.center()
        timer.start(1000)
        self.show()
    def center(self):
        """Function to center the application """
        qRect=self.frameGeometry()
        centerPoint=QDesktopWidget().availableGeometry().center()
        qRect.moveCenter(centerPoint)
        self.move(qRect.topLeft())
    def updtTime(self):
        """Function to update current time """
        currentTime=QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss.ff')
        self.myTimeDisplay.display(currentTime)
    #Main Function
if __name__=='__main__':
        #Exception Handling
    try:
        myApp=QApplication(sys.argv)
        myWindow=MyTimer()
        myApp.exec_()
        sys.exit(0)
    except NameError:
        print("Name Error:", sys.exc_info()[1])
    except SystemExit:
        print("Closing Window...")
    except Exception:
        print(sys.exc_info()[1])
            
            

    
  
Formation of Timer
ConstantDescription
QLCDNumber.OutlineThis gives us raised segments that are filled with the background colo
QLCDNumber.FilledThis gives us raised segments that are filled with the windowText color
QLCDNumber.FlatThis gives us flat segments that are filled with the windowText color
formation of Timer
 
 
  
  
  
 
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值