在Python中使用 PyQt5 创建模拟时钟
先决条件: pyqt-5简介
当时钟或手表有移动的指针和(通常)从数字 1 到 12 标记的小时以显示时间时,它被称为“模拟” 。有些有罗马数字(I、II、III 等),或者根本没有数字!换句话说:不是数字时钟。
PyQt5是一个跨平台的 GUI 工具包,一组用于 Qt v5 的Python绑定。由于该库提供的工具和简单性,人们可以非常轻松地开发交互式桌面应用程序。 GUI 应用程序由前端和后端组成。
方法 :
- 创建一个继承 QMainWindow 类的时钟类。
- 在 Clock 类中创建一个计时器对象,该对象每秒更新整个代码。
- 为时钟的每只指针创建三个多边形对象。
- 创建一个绘画事件方法。
- 在paint事件方法中获取当前时间和窗口宽度或高度的最小值。
- 创建一个画家对象和一个绘制手的方法。
- 在绘制手(指针)的方法中,接受颜色、旋转和多边形对象等参数。
- 旋转画家对象并绘制指针。
- 在paint事件方法内部,根据当前时间设置旋转值并调用draw指针方法。
- 绘制时钟的背景图像,即每小时的线条。
下面是实现:
Python3
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
# creating a clock class
class Clock(QMainWindow):
# constructor
def __init__(self):
super().__init__()
# creating a timer object
timer = QTimer(self)
# adding action to the timer
# update the whole code
timer.timeout.connect(self.update)
# setting start time of timer i.e 1 second
timer.start(1000)
# setting window title
self.setWindowTitle('Clock')
# setting window geometry
self.setGeometry(200, 200, 300, 300)
# setting background color to the window
self.setStyleSheet("background : black;")
# creating hour hand
self.hPointer = QtGui.QPolygon([QPoint(6, 7),
QPoint(-6, 7),
QPoint(0, -50)])
# creating minute hand
self.mPointer = QPolygon([QPoint(6, 7),
QPoint(-6, 7),
QPoint(0, -70)])
# creating second hand
self.sPointer = QPolygon([QPoint(1, 1),
QPoint(-1, 1),
QPoint(0, -90)])
# colors
# color for minute and hour hand
self.bColor = Qt.green
# color for second hand
self.sColor = Qt.red
# method for paint event
def paintEvent(self, event):
# getting minimum of width and height
# so that clock remain square
rec = min(self.width(), self.height())
# getting current time
tik = QTime.currentTime()
# creating a painter object
painter = QPainter(self)
# method to draw the hands
# argument : color rotation and which hand should be pointed
def drawPointer(color, rotation, pointer):
# setting brush
painter.setBrush(QBrush(color))
# saving painter
painter.save()
# rotating painter
painter.rotate(rotation)
# draw the polygon i.e hand
painter.drawConvexPolygon(pointer)
# restore the painter
painter.restore()
# tune up painter
painter.setRenderHint(QPainter.Antialiasing)
# translating the painter
painter.translate(self.width() / 2, self.height() / 2)
# scale the painter
painter.scale(rec / 200, rec / 200)
# set current pen as no pen
painter.setPen(QtCore.Qt.NoPen)
# draw each hand
drawPointer(self.bColor, (30 * (tik.hour() + tik.minute() / 60)), self.hPointer)
drawPointer(self.bColor, (6 * (tik.minute() + tik.second() / 60)), self.mPointer)
drawPointer(self.sColor, (6 * tik.second()), self.sPointer)
# drawing background
painter.setPen(QPen(self.bColor))
# for loop
for i in range(0, 60):
# drawing background lines
if (i % 5) == 0:
painter.drawLine(87, 0, 97, 0)
# rotating the painter
painter.rotate(6)
# ending the painter
painter.end()
# Driver code
if __name__ == '__main__':
app = QApplication(sys.argv)
# creating a clock object
win = Clock()
# show
win.show()
exit(app.exec_())
输出: