PyQt5 模拟钟表

2020-02-10 11:09:13 浏览数 (1)

本程序由PyQt4官方demo升级而来,各指针会随着系统时间变化而转动。另加入了秒针和圆盘。

代码如下:

代码语言:javascript复制
#!/usr/bin/env python

from PyQt5 import QtCore, QtGui,QtWidgets

class AnalogClock(QtWidgets.QWidget):
    hourHand = QtGui.QPolygon([
        QtCore.QPoint(10, 8),
        QtCore.QPoint(-10, 8),
        QtCore.QPoint(0, -60)
    ])
    minuteHand = QtGui.QPolygon([
        QtCore.QPoint(8, 8),
        QtCore.QPoint(-8, 8),
        QtCore.QPoint(0, -70)
    ])
    secondHand = QtGui.QPolygon([
        QtCore.QPoint(4, 8),
        QtCore.QPoint(-4, 8),
        QtCore.QPoint(0, -90)
    ])
    hourColor = QtGui.QColor(255, 0, 0)
    minuteColor = QtGui.QColor(0, 255, 0)
    secondColor = QtGui.QColor(0, 0, 255)
代码语言:javascript复制
    def __init__(self, parent=None):
        super(AnalogClock, self).__init__(parent)
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.update)
        timer.start(1000) # 每1000ms 调用self.update, self.update会调用self.paintEvent
        self.setWindowTitle("Analog Clock")
        self.resize(200, 200)
        
    def paintEvent(self, event):
        side = min(self.width(), self.height())
        time = QtCore.QTime.currentTime()#获取系统当前时间
        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)#抗锯齿
        painter.translate(self.width() / 2, self.height() / 2)
        painter.scale(side / 200.0, side / 200.0)
        
        painter.setPen(QtGui.QColor(0, 0, 0))
        painter.drawEllipse(-100,-100,200,200)#画圆。参数是外接矩形左上点和长宽
        #painter.drawEllipse(-10,-10,20,20)
        
        painter.setPen(AnalogClock.hourColor)
        for i in range(12): #整点刻度
            painter.drawLine(88, 0, 96, 0)
            painter.rotate(30.0)
        painter.setPen(AnalogClock.minuteColor)    
        for j in range(60): #小刻度
            if (j % 5) != 0:
                painter.drawLine(92, 0, 96, 0)
            painter.rotate(6.0)
   
        painter.setPen(QtCore.Qt.NoPen)
        painter.setBrush(AnalogClock.hourColor)
        painter.save()
        painter.rotate(30.0 * ((time.hour()   time.minute() / 60.0)))
        painter.drawConvexPolygon(AnalogClock.hourHand)
        painter.restore()
        painter.setPen(QtCore.Qt.NoPen)
        painter.setBrush(AnalogClock.minuteColor)
        painter.save()
        painter.rotate(6.0 * (time.minute()   time.second() / 60.0))
        painter.drawConvexPolygon(AnalogClock.minuteHand)
        painter.restore()
        painter.setPen(QtCore.Qt.NoPen)
        painter.setBrush(AnalogClock.secondColor)
        painter.save()
        painter.rotate(6.0 * time.second() )
        painter.drawConvexPolygon(AnalogClock.secondHand)
        painter.restore()
        painter.setPen(QtGui.QColor(0, 0, 0))
        painter.drawEllipse(-5,-5,10,10)#画圆。参数是外接矩形左上点和长宽
代码语言:javascript复制
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    clock = AnalogClock()
    clock.show()
    sys.exit(app.exec_())

0 人点赞