Qt小组件 - 9 时间卡片

python 复制代码
# coding: utf-8
import sys
from datetime import datetime

from PySide6.QtCore import Qt, QTimer, Property
from PySide6.QtGui import QFont, QColor, QPaintEvent, QPainter
from PySide6.QtWidgets import QApplication, QFrame, QVBoxLayout, QLabel, QHBoxLayout, QGraphicsDropShadowEffect

qss = '''
#hourLabel {
    background-color: transparent;
    color: qlineargradient(spread:pad,
    x1: 0,
    y1: 0,
    x2: 0,
    y2: 1,
    stop: 0 #634dff,
    stop: 1 #5fd4ff);
}

#dotsLabel {
    background-color: transparent;
    color: #929292;
    padding: 10px 10px 20px 10px;
}

#minuteLabel {
    background-color: transparent;
    color: qlineargradient(spread:pad,
    x1: 0,
    y1: 0,
    x2: 0,
    y2: 1,
    stop: 0 #ff5e9e,
    stop: 1 #ffb960);
}

#periodLabel {
    background-color: transparent;
    color: qlineargradient(spread:pad,
    x1: 0,
    y1: 0,
    x2: 0,
    y2: 1,
    stop: 0 #f7b63f,
    stop: 1 #faf879);

}

#secondsLabel {
    background-color: transparent;
    color: qlineargradient(spread:pad,
    x1: 0,
    y1: 0,
    x2: 0,
    y2: 1,
    stop: 0 #24ff6d,
    stop: 1 #2f93f1);

}

#dataLabel {
    background-color: transparent;
    color: qlineargradient(spread:pad,
    x1: 0,
    y1: 0,
    x2: 0,
    y2: 1,
    stop: 0 #ff98d1,
    stop: 1 #ae4af6);
}
'''


class ClockWidget(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.horizontalLayout = QHBoxLayout()
        self.verticalLayout_2 = QVBoxLayout()
        self.hourLabel = QLabel('00', self)
        self.dotsLabel = QLabel(':', self)
        self.minuteLabel = QLabel('00', self)
        self.periodLabel = QLabel('AM', self)
        self.secondsLabel = QLabel('00', self)
        self.dataLabel = QLabel('2025 年 01 月 01 日 星期一', self)
        self.timer = QTimer(self)

        self.__backgroundColor = QColor(46, 46, 68)
        self.__radius = 0
        self.__borderColor = QColor(0, 0, 0, 100)

        self.__initWidgets()

    def __initWidgets(self):
        font = QFont('微软雅黑', 60, QFont.Weight.Bold)
        self.hourLabel.setFont(font)
        self.dotsLabel.setFont(font)
        self.minuteLabel.setFont(font)
        font.setPointSize(16)
        self.periodLabel.setFont(font)
        self.secondsLabel.setFont(font)
        self.dataLabel.setFont(font)

        self.timer.timeout.connect(self.updateTime)
        self.timer.start(1000)
        self.updateTime()
        self.__initLayout()
        self.__setQss()
        self.setShadowEffect()

    def __initLayout(self):
        self.hourLabel.setFixedSize(125, 125)
        self.dotsLabel.setFixedSize(45, 125)
        self.minuteLabel.setFixedSize(125, 125)

        self.hourLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.dotsLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.minuteLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.dataLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.verticalLayout.setContentsMargins(45, 20, 45, 20)
        self.verticalLayout.addStretch(1)

        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setSpacing(0)
        self.horizontalLayout.addWidget(self.hourLabel)
        self.horizontalLayout.addWidget(self.dotsLabel)
        self.horizontalLayout.addWidget(self.minuteLabel)
        self.horizontalLayout.addSpacing(20)

        self.verticalLayout_2.addStretch(1)
        self.verticalLayout_2.addWidget(self.periodLabel)
        self.verticalLayout_2.addSpacing(20)
        self.verticalLayout_2.addWidget(self.secondsLabel)
        self.verticalLayout_2.addStretch(1)
        self.horizontalLayout.addLayout(self.verticalLayout_2)

        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addWidget(self.dataLabel, 0, Qt.AlignmentFlag.AlignHCenter)
        self.verticalLayout.addStretch(1)

    def __setQss(self):
        self.hourLabel.setObjectName('hourLabel')
        self.dotsLabel.setObjectName('dotsLabel')
        self.minuteLabel.setObjectName('minuteLabel')
        self.periodLabel.setObjectName('periodLabel')
        self.secondsLabel.setObjectName('secondsLabel')
        self.dataLabel.setObjectName('dataLabel')
        self.setStyleSheet(qss)

    def setShadowEffect(self, blurRadius=60, offset=(0, 0), color=QColor(0, 0, 0, 100)):
        """ 为对话框添加阴影 """
        shadowEffect = QGraphicsDropShadowEffect(self)
        shadowEffect.setBlurRadius(blurRadius)
        shadowEffect.setOffset(*offset)
        shadowEffect.setColor(color)
        self.setGraphicsEffect(None)
        self.setGraphicsEffect(shadowEffect)

    def updateTime(self):
        now = datetime.now()
        hour = now.hour
        minute = now.minute
        second = now.second

        self.hourLabel.setText(str(hour).rjust(2, '0'))
        self.minuteLabel.setText(str(minute).rjust(2, '0'))
        self.secondsLabel.setText(str(second).rjust(2, '0'))
        self.periodLabel.setText(now.strftime('%p'))

        weekdays = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
        data = now.strftime('%Y 年 %m 月 %d 日') + ' ' + weekdays[now.weekday()]
        self.dataLabel.setText(data)

    def setRadius(self, radius: int):
        self.__radius = radius
        self.update()

    def getRadius(self) -> int:
        return self.__radius

    def setBackgroundColor(self, color: QColor):
        self.__backgroundColor = color
        self.update()

    def getBackgroundColor(self) -> QColor:
        return self.__backgroundColor

    def setBorderColor(self, color: int):
        self.__borderColor = color
        self.update()

    def getBorderColor(self) -> int:
        return self.__borderColor

    def paintEvent(self, event: QPaintEvent):
        # super().paintEvent(event)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        painter.setPen(self.borderColor)
        painter.setBrush(self.backgroundColor)
        painter.drawRoundedRect(self.rect(), self.radius, self.radius)
        painter.end()

    backgroundColor = Property(QColor, getBackgroundColor, setBackgroundColor)
    radius = Property(int, getRadius, setRadius)
    borderColor = Property(QColor, getBorderColor, setBorderColor)


class BackgroundClock(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)

        self.clock = ClockWidget(self)
        self.clock.setRadius(10)
        # self.clock.setBackgroundColor(QColor(255, 255, 255))

        self.verticalLayout.addWidget(self.clock, 0, Qt.AlignmentFlag.AlignCenter)
        self.setStyleSheet('BackgroundClock{background-color: white;}')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    clock = BackgroundClock()
    clock.resize(1200, 600)
    clock.show()
    sys.exit(app.exec())
相关推荐
charlie11451419125 分钟前
嵌入式现代C++教程: 构造函数优化:初始化列表 vs 成员赋值
开发语言·c++·笔记·学习·嵌入式·现代c++
wjs202427 分钟前
Bootstrap5 消息弹窗
开发语言
资生算法程序员_畅想家_剑魔34 分钟前
Kotlin常见技术分享-02-相对于Java 的核心优势-协程
java·开发语言·kotlin
IDC02_FEIYA1 小时前
SQL Server 2025数据库安装图文教程(附SQL Server2025数据库下载安装包)
数据库·windows
IT=>小脑虎1 小时前
C++零基础衔接进阶知识点【详解版】
开发语言·c++·学习
辞砚技术录1 小时前
MySQL面试题——联合索引
数据库·面试
nbsaas-boot1 小时前
Go vs Java 的三阶段切换路线图
java·开发语言·golang
码农小韩1 小时前
基于Linux的C++学习——指针
linux·开发语言·c++·学习·算法
微露清风1 小时前
系统性学习C++-第十九讲-unordered_map 和 unordered_set 的使用
开发语言·c++·学习
BBBBBAAAAAi1 小时前
Claude Code安装记录
开发语言·前端·javascript