使用pyqt5编写一个七彩时钟

news2024/7/7 14:59:55

使用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_())

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1880169.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++ 数据库MySQL 学习笔记(3) - 数据库操作

C 数据库MySQL 学习笔记(3) - 数据库操作 视图操作 视图是从一个或多个表中导出来的表,是一种虚拟存在的表。视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据,这样用户可以不看整个数据库表中的数据,而只关心对自己有…

【热部署】✈️Springboot 项目的热部署实现方式

目录 🍸前言 🍻一、热部署和手动重启 🍺二、热部署的实现 2.1 手动启动热部署 2.2 自动检测热部署 2.3 关闭热部署 💞️三、章末 🍸前言 小伙伴们大家好,书接上文,通过Springboot 中的 actu…

【Python】已解决:IndexError: list index out of range

文章目录 一、分析问题背景二、可能出错的原因三、错误代码示例四、正确代码示例五、注意事项 已解决:IndexError: list index out of range 一、分析问题背景 在Python编程中,IndexError: list index out of range 是一个常见的错误。这个错误通常出现…

【Python从入门到进阶】59、Pandas库中Series对象的操作(二)

接上篇《58、Pandas库中Series对象的操作(一)》 上一篇我们讲解了Pandas库中Series对象的基本概念、对象创建和操作,本篇我们来继续学习Series对象的运算、函数应用、时间序列操作,以及Series的案例实践。 一、Series对象的运算 1. 数值型数据的算术运…

基于JSP的体育竞赛成绩管理系统

开头语:你好呀,我是计算机学长猫哥!如果有相关需求,文末可以找到我的联系方式。 开发语言:JSP 数据库:MySQL 技术:JSPJava 工具:MyEclipse, Tomcat, MySQL 系统展示 首页 管理…

Windows Ternimal

Windows Ternimal 安装 Windows 终端概述 | Microsoft Learn wt --help在当前目录打开 lextm/windowsterminal-shell: Install/uninstall scripts for Windows Terminal context menu items 打开指定目录 wt -d %USERPROFILE% ohmyposh 美化 1 安装 2 添加 ohmyposh bin…

数字签名解析

1. 概述 数字签名不是手写签名的数字图像; 数字签名是一种可以提供认证的加密形式,是转向完全无纸环境的一个途径; 数字签名机制用以解决伪造、抵赖、冒充和篡改、完整性保护等安全问题。 2. 公钥密码与数字签名的关系 要实现数字签名&#…

【python爬虫实战】爬取豆瓣top250(网站有反爬虫机制肿么办)

关于请求头headers: 值得注意的是,与上一篇 :​​​​​​【python爬虫实战】爬取书店网站的 书名&价格(注释详解)-CSDN博客 爬取书名不同,这次爬取豆瓣网站必须使用“请求头headers”,不然将没有输…

SSM学习2:依赖注入、依赖自动装配、集合注入、加载properties文件

依赖注入 依赖注入方式 setter注入——引用类型 setter注入——简单类型 public class BookDaoImpl implements BookDao {public void setDatabaseName(String databaseName) {this.databaseName databaseName;}public void setNum(int num) {this.num num;}private Stri…

【图像超分辨率】一个简单的总结

文章目录 图像超分辨率(Image Super-Resolution, ISR)1 什么是图像超分辨率?2 图像超分辨率通常有哪些方法?(1)基于插值的方法(2)基于重建的方法(3)基于学习的方法(LR im…

jenkins 发布服务到linux服务器

1.环境准备 1.1 需要一台已经部署了jenkins的服务器,上面已经集成好了,jdk、maven、nodejs、git等基础的服务。 1.2 需要安装插件 pusblish over ssh 1.3 准备一台额外的linux服务器,安装好jdk 2.流程描述 2.1 配置jenkins,包括p…

统计是一门艺术(参数假设检验)

1.参数假设检验 在总体分布已知的情况下,对分布中未知参数的检验。 (1)相关基本概念 零假设/原假设与对立假设/备择假设: 任务:根据样本作出是否接受H0 复合假设与简单假设: 否定域/拒绝域与接受域&…

Python:谈谈常规滤波器(带通、低通、高通、带阻)的用法

一、滤波器的作用 滤波器在信号处理中用于移除或减少信号中的噪声,同时保持信号的某些特性。滤波器通常用于音频、视频和图像处理等领域。滤波器根据其 designed for different purposes and can be divided into several types, such as lowpass filters, highpass…

【Unity设计模式】✨使用 MVC 和 MVP 编程模式

前言 最近在学习Unity游戏设计模式,看到两本比较适合入门的书,一本是unity官方的 《Level up your programming with game programming patterns》 ,另一本是 《游戏编程模式》 这两本书介绍了大部分会使用到的设计模式,因此很值得学习 本…

Linux rpm与yum

一、rpm包管理 rpm用于互联网下载包的打包及安装工具,它包含在某些Linux分发版中。它生成具有.RPM扩展名的文件。RPM是RedHat Package Manager (RedHat软件包管理工具)的缩写,类似windows的setup.exe,这一文件格式名称虽然打上了R…

Python pip install模块时C++编译环境问题

pip install模块时C编译环境问题 在接触和使用python后,常常会通过pip install命令安装第三方模块,大多数模块可以直接安装,但许多新同学仍会遇见某些模块需要实时编译后才能安装,如报错信息大概是缺乏C编译环境,本文则…

Golang-GMP

GMP调度 golang-GMP语雀笔记整理 GMP调度设计目的,为何设计GMP?GMP的底层实现几个核心数据结构GMP调度流程 设计目的,为何设计GMP? 无论是多进程、多线程目的都是为了并发提高cpu的利用率,但多进程、多线程都存在局限性。比如多进程通过时…

Python变量的命名规则与赋值方式

第二章:Python 基础语法 第一节:变量的命名规则与赋值方式 2.1.1 引言 在编程中,变量是存储数据的基本单元。变量的命名和赋值是编程语言中表达和操作数据的基础。了解和遵循变量命名规则对于编写清晰、可维护的代码至关重要。 2.1.2 变量…

嵌入式Linux系统编程 — 5.2 Linux系统时间与日期

目录 1 了解Linux系统时间 1.1 几种常用的时间 1.2 如何查看几种常用的时间 1.3 Linux 系统中的时间 2 time、gettimeofday获取时间 2.1 time函数 2.2 ​​​​​​​gettimeofday函数: 2.3 示例程序 3 时间转换函数 3.1 ctime与ctime_r函数 3.2 localti…

小白学python(第四天)顺序与分支篇

这几天因为个人原因,python篇会更新比较慢,还望大家谅解,那么废话不多说,我们现在就进入正题 顺序篇 这个没啥好说的,就是自上而下,依次执行 分支篇 条件(if)语句语法格式&#…