首页 > 其他分享 >使用pyqt5编写一个七彩时钟

使用pyqt5编写一个七彩时钟

时间:2024-06-30 19:57:07浏览次数:3  
标签:七彩 layout self pyqt5 label second time minute 时钟

使用pyqt5编写一个七彩时钟

  • 效果
  • 代码解析
    • 定义 RainbowClockWindow 类
    • 初始化用户界面
    • 显示时间方法
  • 完整代码

在这篇博客中,我们将使用 PyQt5 创建一个简单的七彩数字时钟。

效果

在这里插入图片描述

代码解析

定义 RainbowClockWindow 类

class RainbowClockWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Rainbow Digital Clock')
        self.setGeometry(100, 100, 400, 200)

        self.initUI()

初始化用户界面

    def initUI(self):
        layout = QVBoxLayout()

        self.time_layout = QHBoxLayout()
        self.time_layout.setSpacing(0)  # 设置标签之间的间距为0

        self.hour_label = QLabel(self)
        self.hour_label.setAlignment(Qt.AlignCenter)
        self.hour_label.setStyleSheet("font-size: 48px;")

        self.colon1_label = QLabel(self)
        self.colon1_label.setAlignment(Qt.AlignCenter)
        self.colon1_label.setStyleSheet("font-size: 48px;")
        self.colon1_label.setText(":")

        self.minute_label = QLabel(self)
        self.minute_label.setAlignment(Qt.AlignCenter)
        self.minute_label.setStyleSheet("font-size: 48px;")

        self.colon2_label = QLabel(self)
        self.colon2_label.setAlignment(Qt.AlignCenter)
        self.colon2_label.setStyleSheet("font-size: 48px;")
        self.colon2_label.setText(":")

        self.second_label = QLabel(self)
        self.second_label.setAlignment(Qt.AlignCenter)
        self.second_label.setStyleSheet("font-size: 48px;")

        self.time_layout.addWidget(self.hour_label)
        self.time_layout.addWidget(self.colon1_label)
        self.time_layout.addWidget(self.minute_label)
        self.time_layout.addWidget(self.colon2_label)
        self.time_layout.addWidget(self.second_label)

        layout.addLayout(self.time_layout)
        layout.setAlignment(Qt.AlignCenter)  # 居中对齐

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        timer = QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(1000)

        self.showTime()

  • 创建一个垂直布局 QVBoxLayout 和一个水平布局 QHBoxLayout,并设置水平布局的标签间距为0。
  • 创建五个标签:hour_label、colon1_label、minute_label、colon2_label 和
    second_label,并设置标签的对齐方式和样式,使其在窗口中居中并且字体大小为 48 像素。
  • 将五个标签添加到水平布局中。
  • 将水平布局添加到垂直布局中,并设置垂直布局居中对齐。
  • 创建一个容器 QWidget,将布局设置为该容器的布局,并将容器设置为主窗口的中央控件。
  • 创建一个 QTimer 对象,每秒触发一次 timeout 事件,连接到 showTime 方法。
  • 调用 showTime 方法显示当前时间。

显示时间方法

    def showTime(self):
        current_time = QTime.currentTime()
        hour = current_time.toString('hh')
        minute = current_time.toString('mm')
        second = current_time.toString('ss')

        # Generate random colors for hour, minute, and second
        hour_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        minute_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        second_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

        self.hour_label.setText(hour)
        self.hour_label.setStyleSheet(f"font-size: 48px; color: {hour_color.name()};")

        self.minute_label.setText(minute)
        self.minute_label.setStyleSheet(f"font-size: 48px; color: {minute_color.name()};")

        self.second_label.setText(second)
        self.second_label.setStyleSheet(f"font-size: 48px; color: {second_color.name()};")

        # Colon colors
        self.colon1_label.setStyleSheet(f"font-size: 48px; color: #000000;")
        self.colon2_label.setStyleSheet(f"font-size: 48px; color: #000000;")

  • showTime 方法获取当前时间并将其格式化为小时、分钟和秒。
  • 为小时、分钟和秒生成随机颜色,并将这些颜色应用到相应的标签上。
  • 将两个冒号标签的颜色固定为黑色。

完整代码

import sys
import random
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QHBoxLayout, QVBoxLayout, QWidget
from PyQt5.QtCore import QTimer, QTime, Qt
from PyQt5.QtGui import QColor

class RainbowClockWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Rainbow Digital Clock')
        self.setGeometry(100, 100, 400, 200)

        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()

        self.time_layout = QHBoxLayout()
        self.time_layout.setSpacing(0)  # 设置标签之间的间距为0

        self.hour_label = QLabel(self)
        self.hour_label.setAlignment(Qt.AlignCenter)
        self.hour_label.setStyleSheet("font-size: 48px;")

        self.colon1_label = QLabel(self)
        self.colon1_label.setAlignment(Qt.AlignCenter)
        self.colon1_label.setStyleSheet("font-size: 48px;")
        self.colon1_label.setText(":")

        self.minute_label = QLabel(self)
        self.minute_label.setAlignment(Qt.AlignCenter)
        self.minute_label.setStyleSheet("font-size: 48px;")

        self.colon2_label = QLabel(self)
        self.colon2_label.setAlignment(Qt.AlignCenter)
        self.colon2_label.setStyleSheet("font-size: 48px;")
        self.colon2_label.setText(":")

        self.second_label = QLabel(self)
        self.second_label.setAlignment(Qt.AlignCenter)
        self.second_label.setStyleSheet("font-size: 48px;")

        self.time_layout.addWidget(self.hour_label)
        self.time_layout.addWidget(self.colon1_label)
        self.time_layout.addWidget(self.minute_label)
        self.time_layout.addWidget(self.colon2_label)
        self.time_layout.addWidget(self.second_label)

        layout.addLayout(self.time_layout)
        layout.setAlignment(Qt.AlignCenter)  # 居中对齐

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        timer = QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(1000)

        self.showTime()

    def showTime(self):
        current_time = QTime.currentTime()
        hour = current_time.toString('hh')
        minute = current_time.toString('mm')
        second = current_time.toString('ss')

        # Generate random colors for hour, minute, and second
        hour_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        minute_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        second_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

        self.hour_label.setText(hour)
        self.hour_label.setStyleSheet(f"font-size: 48px; color: {hour_color.name()};")

        self.minute_label.setText(minute)
        self.minute_label.setStyleSheet(f"font-size: 48px; color: {minute_color.name()};")

        self.second_label.setText(second)
        self.second_label.setStyleSheet(f"font-size: 48px; color: {second_color.name()};")

        # Colon colors
        self.colon1_label.setStyleSheet(f"font-size: 48px; color: #000000;")
        self.colon2_label.setStyleSheet(f"font-size: 48px; color: #000000;")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = RainbowClockWindow()
    window.show()
    sys.exit(app.exec_())

标签:七彩,layout,self,pyqt5,label,second,time,minute,时钟
From: https://blog.csdn.net/summerriver1/article/details/140082461

相关文章

  • verilog写12 小时时钟(带上午/下午指示器)计数器(HDLbits Count clock)
    Createasetofcounterssuitableforuseasa12-hourclock(witham/pmindicator).Yourcountersareclockedbyafast-running clk,withapulseon ena wheneveryourclockshouldincrement(i.e.,oncepersecond).reset resetstheclockto12:00AM.......
  • pyqt5创建主窗口(介绍窗口类型)
    创建主窗口(介绍窗口类型)我们用QMainWindow主窗口代码来创建UIQMainWindow.setWindowTitle(字符串),设置窗口标题QMainWindow.resize(800,400),设置窗口尺寸QMainWindow.statusBar(),获取状态栏对象QMainWindow.showMessage('停留5秒消息',5000)QApplication.setWindowIcon(QIco......
  • 桌面时钟APP的简单开发(Android开发)
    开发目的想打造个性化的私人闹钟APP,放到桌面上提示时间,但是感觉应用商店中的相关软件不好用,有些有广告,就难受。而且没有办法DIY自己想要的时钟样式。所以,开搞!(初学者入门,慢慢摸索呗)开发环境1、windows操作系统2、Android Studio20243、JDK1.8(已配置的jdk环境,因为Andro......
  • STM32基础篇--复位和时钟控制RCC
    1.时钟树1.1时钟问:什么是时钟?为什么要有时钟?时钟是怎么产生的?(1)什么是时钟?时钟就是具有周期性的脉冲信号,相当于单片机的心脏,给单片机提供一个统一的信号,要想使用单片机的外设必须开启相应的时钟。对CPU来说,假设CPU在一个时钟周期内执行一条指令,若时钟频率越高,则时钟周期......
  • 【Power Compiler手册】9.时钟门控(3时钟门控风格)
    时钟门控风格PowerCompiler工具根据您指定的风格在设计中插入时钟门控单元。当没有指定时钟门控风格时,工具会使用一组预定义的风格用于时钟门控。set_clock_gating_style命令的默认设置适用于大多数设计。以下部分将详细讨论默认时钟门控风格和使用特定时钟门控风格:•......
  • 【Power Compiler手册】9.时钟门控(2)
    指定时钟门控延迟在综合过程中,DesignCompiler假设时钟是理想的。理想时钟在时钟网络中不产生任何延迟。这种假设是因为直到时钟树综合之后,实际的时钟网络延迟才为人所知。实际上,时钟并不是理想的,并且通过时钟网络存在非零延迟。对于具有时钟门控的设计,寄存器处的时钟网络延......
  • 【Power Compiler手册】9.时钟门控(1)
    在更高层次上的功耗优化对最终门级设计的功耗降低有显著影响。时钟门控是降低设计功耗的重要技术。有关PowerCompiler工具中时钟门控的信息,请参阅以下主题:-时钟门控简介-使用时钟门控条件-插入时钟门控-时钟门控流程-指定时钟门控延迟-从时钟门控单元到寄存......
  • PyQt5和Eric7的安装使用 —— Python篇
    需要安装Python的朋友请看另一篇文章:windows系统安装Python-----并安装使用Pycharm编辑器一、安装PyQt5:1、方法一:使用pip命令在线安装。输入以下命令可以直接安装:pipinstallPyQt5由于安装默认使用国外的镜像,可能因为网络问题会导致下载慢或者失败的现象。所以我们可以......
  • nalog_clock 时钟
    analog_clock:^0.1.0#时钟 classMyApp1extendsStatelessWidget{@overrideWidgetbuild(BuildContextcontext){//获取当前北京时间returnCenter(child:AnalogClock(//时钟的装饰,指定边框、背景色和形状decoration:BoxDecorat......
  • 移除时钟/阻止时钟传播的几个思路
    1.如果clk在mux输出端,可以将case值(clk_en)设为02.set_sense -typeclock -stop_propagation -clocks[get_clockclkA] [get_pinsclkB]3.remove_generated_clockclkB(ptcommand)  reset_generated_clockclkB(innovouscommand)例:如下图的clkmux,阻止clkB。......