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())