# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import math
class Clock_paint(QLabel):
# 老式钟表
def __init__(self,parent =None):
super(Clock_paint,self).__init__(parent)
self.timer = QTimer() # 定时器
self.timer.timeout.connect(self.update)
self.timer.start(1000) # 每1s 更新一次
# 指南针偏移角度
self.compass_angle = 0
# 水平仪
self.leve_pitch = 0
self.leve_roll = 0
def textRectF(self,radius,pointsize,angle):
recf = QRectF()
recf.setX(radius*math.cos(angle*math.pi/180.0)-pointsize*2)
recf.setY(radius*math.sin(angle*math.pi/180.0)-pointsize/2.0)
recf.setWidth(pointsize*4) #宽度、高度
recf.setHeight(pointsize)
return recf
def paintEvent(self, event):
# 坐标集合
level_points = [QPoint(5, 96), QPoint(-5, 96), QPoint(0, 80)]
# 颜色表
sky_blue = QColor(0, 128, 255, 255)
yellow_color = QColor(255, 255, 0, 255)
pale_brown_color = QColor(128, 64, 64, 255)
bule_color = QColor(0, 0, 255, 255)
white_color = QColor(255, 255, 255, 255)
red_color = QColor(255, 0, 0, 255)
# 画笔初始化
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(bule_color)
painter.translate(1200, 600)
# 绘制水平仪
painter.setBrush(sky_blue)
painter.drawPie(-100, -100, 200, 200, 0, 180 * 16)
painter.setBrush(pale_brown_color)
painter.drawPie(-100, -100, 200, 200, 0, -180 * 16)
# 绘制刻度
painter.setPen(white_color)
painter.rotate(210)
painter.drawLine(80, 0, 90, 0)
painter.rotate(20)
painter.drawLine(80, 0, 90, 0)
painter.rotate(20)
painter.drawLine(80, 0, 90, 0)
painter.rotate(20)
painter.drawLine(80, 0, 90, 0)
painter.rotate(20)
painter.drawLine(80, 0, 90, 0)
painter.rotate(20)
painter.drawLine(80, 0, 90, 0)
painter.rotate(20)
painter.drawLine(80, 0, 90, 0)
painter.rotate(30)
# 绘制水平线
painter.rotate(self.leve_roll)
# 绘制刻度
painter.setBrush(red_color)
painter.rotate(180)
painter.drawConvexPolygon(QPolygon(level_points))
painter.rotate(-180)
painter.translate(0, 0 - self.leve_pitch)
painter.setPen(yellow_color)
painter.drawLine(-60, 0, 60, 0) # 绘制水平线
painter.setPen(white_color)
painter.drawLine(-30, 10, 30, 10) # 绘制水平线
painter.drawLine(-30, -10, 30, -10) # 绘制水平线
painter.drawLine(-40, 20, 40, 20) # 绘制水平线
painter.drawLine(-40, -20, 40, -20) # 绘制水平线
painter.drawLine(-30, 30, 30, 30) # 绘制水平线
painter.drawLine(-30, -30, 30, -30) # 绘制水平线
painter.setFont(QFont('SimSun', 5))
painter.drawText(self.textRectF(65, 100, 0), Qt.AlignCenter, str("0"))
painter.drawText(self.textRectF(-65, 100, 0), Qt.AlignCenter, str("0"))
painter.drawText(self.textRectF(53, 100, -22), Qt.AlignCenter, str("20"))
painter.drawText(self.textRectF(53, 100, 202), Qt.AlignCenter, str("20"))
painter.drawText(self.textRectF(53, 100, 22), Qt.AlignCenter, str("20"))
painter.drawText(self.textRectF(53, 100, 158), Qt.AlignCenter, str("20"))
class My_Widget(QWidget):
def __init__(self,parent = None):
super(My_Widget,self).__init__(parent)
self.label1 = Clock_paint()
self.horizon_layout = QHBoxLayout()
self.horizon_layout.addWidget(self.label1)
self.setLayout(self.horizon_layout)
self.setWindowTitle('时钟--《公号:小王搬运工》')
if __name__ =='__main__':
import sys
app = QApplication(sys.argv)
my_widget = My_Widget()
my_widget.setGeometry(50, 50, 2400, 1300)
my_widget.show()
sys.exit(app.exec())
评论0