Pysides6 Python3.10 Qt 画一个时钟

代码如下

python 复制代码
import sys
from math import pi, sin, cos
from PySide6.QtCore import Qt, QTimer, QTime, QPointF, QRect
from PySide6.QtGui import QPainter, QPolygonF, QColor, QRadialGradient, QFont
from PySide6.QtWidgets import QApplication, QWidget,QMainWindow,QVBoxLayout

class AnalogClock(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("PySide6 时钟")
        self.setMinimumSize(30, 30)
        #self.clock = QWidget()
        # 设置窗口背景透明
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setStyleSheet("background: transparent;")

        # 初始化定时器
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update)
        self.timer.start(1000)  # 每秒更新一次

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)

        # 计算尺寸参数
        side = min(self.width(), self.height())
        center = self.rect().center()
        radius = (side - 20) / 2

        # 绘制渐变背景
        gradient = QRadialGradient(center, radius, center)
        gradient.setColorAt(0, QColor(240, 240, 240))
        gradient.setColorAt(1, QColor(180, 180, 180))
        painter.setBrush(gradient)
        painter.drawEllipse(center, radius, radius)

        # 绘制外框
        painter.setPen(QColor(80, 80, 80))
        painter.drawEllipse(center, radius, radius)
        painter.drawEllipse(center, radius-5, radius-5)

        # 移动到中心点并旋转坐标系
        painter.translate(center)
        painter.rotate(-90)  # 0度从顶部开始

        # 绘制小时刻度
        painter.setPen(QColor(60, 60, 60))
        for i in range(12):
            painter.rotate(30)
            painter.drawLine(int(radius*0.85), 0, int(radius*0.95), 0)

        # 绘制分钟刻度
        painter.setPen(QColor(100, 100, 100))
        for i in range(90):
            if i % 5 != 0:  # 跳过小时刻度
                painter.rotate(6)
                painter.drawLine(int(radius*0.9), 0, int(radius*0.95), 0)

        # 获取当前时间
        time = QTime.currentTime()
        hour = time.hour() % 12
        minute = time.minute()
        second = time.second()

        painter.rotate(30)

        # 绘制小时数字
        painter.setPen(QColor(40, 40, 40))
        font = QFont("Arial", int(radius/8), QFont.Bold)
        painter.setFont(font)
        for i in range(12):
            angle = i * 30 + 180 + 75  # 每小时30度
            x = radius * 0.75 * cos(angle * pi / 180)
            y = radius * 0.75 * sin(angle * pi / 180)
            painter.drawText(int(x - 10), int(y - 10), 36, 36,
                            Qt.AlignCenter, str(12 if i == 0 else i))

        # 重置坐标系
        painter.resetTransform()
        painter.translate(center)
        painter.rotate(-90)

        # 绘制时针
        hour_angle = (hour + minute/60) * 30
        painter.setPen(Qt.NoPen)
        painter.setBrush(QColor(50, 50, 50))
        painter.save()
        painter.rotate(hour_angle)
        hour_hand = QPolygonF([
            QPointF(-0.02*radius, -0.02*radius),
            QPointF(0.02*radius, -0.02*radius),
            QPointF(0.05*radius, radius*0.5),
        ])
        painter.drawConvexPolygon(hour_hand)
        painter.restore()

        # 绘制分针
        minute_angle = (minute + second/60) * 6
        painter.setBrush(QColor(80, 80, 80))
        painter.save()
        painter.rotate(minute_angle)
        minute_hand = QPolygonF([
            QPointF(-0.015*radius, -0.015*radius),
            QPointF(0.015*radius, -0.015*radius),
            QPointF(0.03*radius, radius*0.7),
        ])
        painter.drawConvexPolygon(minute_hand)
        painter.restore()

        # 绘制秒针
        second_angle = second * 6
        painter.setPen(QColor(200, 50, 50))
        painter.setBrush(QColor(200, 50, 50))
        painter.save()
        painter.rotate(second_angle)
        painter.drawLine(0, 0, 0, -radius*0.8)
        painter.restore()

        # 绘制中心点
        painter.setPen(Qt.NoPen)
        painter.setBrush(QColor(200, 50, 50))
        painter.drawEllipse(QPointF(0, 0), 5, 5)
        # 主窗口

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("嵌入式绘图示例")
        # 计算中心坐标
        btn_width = 300
        btn_height = 300
        x = (self.width() - btn_width) // 2  # 水平居中
        y = self.height() * 0.75  # 垂直位置在75%处
        self.layout = QVBoxLayout()
        self.clock = AnalogClock(self)
        self.clock.setGeometry(x, y, btn_width, btn_height)
        self.clock.show()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()

    window.show()
    sys.exit(app.exec())

结果如下:

相关推荐
用户277844910499313 小时前
借助DeepSeek智能生成测试用例:从提示词到Excel表格的全流程实践
人工智能·python
JavaEdge在掘金16 小时前
ssl.SSLCertVerificationError报错解决方案
python
我不会编程55516 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
CoderIsArt17 小时前
QT中已知4个坐标位置求倾斜平面与倾斜角度
qt·平面
老歌老听老掉牙17 小时前
平面旋转与交线投影夹角计算
python·线性代数·平面·sympy
满怀101517 小时前
Python入门(7):模块
python
无名之逆17 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
你觉得20517 小时前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义下载方法
大数据·人工智能·python·gpt·学习·机器学习·aigc
啊喜拔牙17 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala