美文网首页Python
Python黑科技系列08-电子时钟-PyQt5小Demo练手

Python黑科技系列08-电子时钟-PyQt5小Demo练手

作者: Tony_Pm | 来源:发表于2022-06-30 13:34 被阅读0次

序言

分享稻盛和夫的一句话:终有那么一天,当你的努力足以配得上你的梦想的时候,你的梦想也就实现了。因为,这个世界不会辜负努力追梦的人。

实战

安装模块: pip install PyQt5

展示效果

时钟.gif
# -*- coding: utf-8 -*-
# @Author  : Tony

from PyQt5.QtGui import *  
from PyQt5.QtCore import *  
from PyQt5.QtWidgets import QWidget,QDesktopWidget,QLCDNumber,QVBoxLayout,QApplication
import time,sys

class MyTime(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.init_timer()

    def update_time(self):
        #   再次调用 并获取本地的时间。    更新最终显示的时间。
        self.lcd.display(time.strftime('%X',time.localtime()))

    def init_timer(self): 
        self.timer=QTimer()
        self.timer.setInterval(1000) 
        self.timer.start() 
        self.timer.timeout.connect(self.update_time)
    def initUI(self):
        #调整组件的大小。250px宽,150px高。
        self.resize(250,150)
        # self.move(300,300)
        self.setWindowTitle("时钟") 
        self.move_center()
      
        #  创建一个QPalette类,其相当于调色板
        self.main_p=QPalette() 
        #设置背景颜色为深黄色
        self.main_p.setColor(QPalette.Background,Qt.darkYellow)
        #设置窗体自动填充背景颜色
        self.setAutoFillBackground(True)
        #把调色板设置给我们的顶级窗口
        self.setPalette(self.main_p)

        # LCD显示组件的设置    # 创建一个QLCDNumber
        self.lcd=QLCDNumber()
        self.lcd.setDigitCount(10)#设置显示数字个数
        self.lcd.setMode(QLCDNumber.Dec)# 设置显示模式为十进制
        self.lcd.setSegmentStyle(QLCDNumber.Flat) # 设置样式为平面模式

        # self.lcd_p=QPalette()  #设置字体颜色的
        # self.lcd_p.setColor(QPalette.Normal,QPalette.windowText,Qt.black)
        # self.lcd.setPalette(self.lcd_p)
       
        self.lcd.display(time.strftime('%X',time.localtime()))

        self.main_layout=QVBoxLayout() # 创建一个盒子布局
        self.main_layout.addWidget(self.lcd) # 把我们的显示组件添加到布局中。
        self.main_layout.setAlignment(Qt.AlignCenter) # 设置组件在布局的中心
        self.setLayout(self.main_layout) # 设置给我们的顶层布局
        self.show()
    def move_center(self): 
        #我们获得主窗口的一个矩形特定几何图形。
        m_rect=self.frameGeometry()
        w_center_point=QDesktopWidget().availableGeometry().center()
        m_rect.moveCenter(w_center_point)
        self.move(m_rect.topLeft())

if __name__ == '__main__':
    app=QApplication(sys.argv)
    m_timer=MyTime()
    sys.exit(app.exec_())

如果本文对你学习有所帮助-可以点赞👍+ 关注!将持续更新更多新的文章。

相关文章

网友评论

    本文标题:Python黑科技系列08-电子时钟-PyQt5小Demo练手

    本文链接:https://www.haomeiwen.com/subject/tdtbvrtx.html