PYQT制作动态时钟

所有代码:

import sys
from PyQt5.QtCore import Qt, QTimer, QRect
from PyQt5.QtGui import QPixmap, QTransform, QPainter, QImage
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5 import uic
import new


def adder():
    global i
    global angle_s, angle_m, angle_h
    i += 1
    angle_s += 6
    if angle_s == 360:
        angle_s = 0
        angle_m = i / 10
        angle_h = i / 120
        if angle_m == 360:
            angle_m = 0
        if angle_h == 360:
            angle_h = 0
            i = 0


if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = uic.loadUi('clock.ui')  # 导入ui
    form.show()

    form.label_second.setAlignment(Qt.AlignCenter)
    form.label_minute.setAlignment(Qt.AlignCenter)
    form.label_hour.setAlignment(Qt.AlignCenter)

    # 加载并设置图片
    image_second = QPixmap("second300")
    form.label_second.setPixmap(image_second)
    image_minute = QPixmap("minute300")
    form.label_minute.setPixmap(image_minute)
    image_hour = QPixmap("hour300")
    form.label_hour.setPixmap(image_hour)

    i = 0
    angle_s = 0
    angle_m = 0
    angle_h = 0

    timer = QTimer()
    timer.start(100)
    timer.timeout.connect(adder)
    timer.timeout.connect(lambda: form.label_second.setPixmap(image_second.transformed(QTransform().rotate(angle_s))))
    timer.timeout.connect(lambda: form.label_minute.setPixmap(image_minute.transformed(QTransform().rotate(angle_m))))
    timer.timeout.connect(lambda: form.label_hour.setPixmap(image_hour.transformed(QTransform().rotate(angle_h))))

    sys.exit(app.exec_())

你可能感兴趣的:(pyqt)