pyqt制作电子钟表

举报
风吹稻花香 发表于 2021/08/25 23:14:40 2021/08/25
【摘要】 pyqt制作电子钟表 https://github.com/n0bel/PiClock 自定义表盘背景:  效果图:  code: from PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import * ...

pyqt制作电子钟表

https://github.com/n0bel/PiClock

自定义表盘背景:

 效果图:

 code:


  
  1. from PyQt5.QtCore import *
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtWidgets import *
  4. class clockForm(QWidget):
  5. def __init__(self, parent=None):
  6. super(clockForm, self).__init__(parent)
  7. self.setWindowTitle("Clock")
  8. self.timer = QTimer()
  9. # 设置窗口计时器
  10. window_pale = QPalette()
  11. window_pale.setBrush(self.backgroundRole(), QBrush(QPixmap("clock.png")))
  12. self.setPalette(window_pale)
  13. self.timer.timeout.connect(self.update)
  14. self.timer.start(1000)
  15. def paintEvent(self, event):
  16. painter = QPainter(self)
  17. painter.setRenderHint(QPainter.Antialiasing)
  18. # 设置表盘中的文字字体
  19. font = QFont("Times", 6)
  20. fm = QFontMetrics(font)
  21. fontRect = fm.boundingRect("99") # 获取绘制字体的矩形范围
  22. # 分针坐标点
  23. minPoints = [QPointF(50, 25), QPointF(48, 50), QPointF(52, 50)]
  24. # 时钟坐标点
  25. hourPoints = [QPointF(50, 35), QPointF(48, 50), QPointF(52, 50)]
  26. side = min(self.width(), self.height())
  27. painter.setViewport((self.width() - side) / 2, (self.height() - side) / 2, side, side) # 始终处于窗口中心位置显示
  28. # 设置QPainter的坐标系统,无论窗体大小如何变化,
  29. # 窗体左上坐标为(0,0),右下坐标为(100,100),
  30. # 因此窗体中心坐标为(50,50)
  31. painter.setWindow(0, 0, 100, 100)
  32. transform = QTransform()
  33. # transform.reset()
  34. # 获取当前时间
  35. currentTime = QTime().currentTime()
  36. hour = currentTime.hour() if currentTime.hour() < 12 else currentTime.hour() - 12
  37. minite = currentTime.minute()
  38. second = currentTime.second()
  39. # 获取所需旋转角度
  40. hour_angle = hour * 30.0 + (minite / 60.0) * 30.0
  41. minite_angle = (minite / 60.0) * 360.0
  42. second_angle = second * 6.0
  43. # 时针
  44. transform.translate(50, 50)
  45. transform.rotate(hour_angle)
  46. transform.translate(-50, -50)
  47. painter.setWorldTransform(transform)
  48. painter.setPen(Qt.NoPen)
  49. painter.setBrush(QBrush(Qt.darkRed))
  50. painter.drawPolygon(QPolygonF(hourPoints))
  51. transform.reset()
  52. # 分针
  53. transform.translate(50, 50)
  54. transform.rotate(minite_angle)
  55. transform.translate(-50, -50)
  56. painter.setWorldTransform(transform)
  57. painter.setBrush(QBrush(Qt.darkGreen))
  58. painter.drawPolygon(QPolygonF(minPoints))
  59. transform.reset()
  60. # 秒针
  61. transform.translate(50, 50)
  62. transform.rotate(second_angle)
  63. transform.translate(-50, -50)
  64. painter.setWorldTransform(transform)
  65. painter.setPen(QPen(Qt.darkCyan, 1))
  66. painter.drawLine(50, 50, 50, 20)
  67. if __name__ == "__main__":
  68. import sys
  69. app = QApplication(sys.argv)
  70. form = clockForm()
  71. form.show()
  72. app.exec_()

绘制表盘,效果图:


  
  1. '''
  2. Created on 2013-7-2
  3. @author: badboy
  4. Email:lucky.haiyu@gmail.com
  5. QQ:43831266
  6. '''
  7. from PyQt5.QtCore import *
  8. from PyQt5.QtGui import *
  9. from PyQt5.QtWidgets import *
  10. class clockForm(QWidget):
  11. def __init__(self,parent=None):
  12. super(clockForm,self).__init__(parent)
  13. self.setWindowTitle("Clock")
  14. self.timer=QTimer()
  15. #设置窗口计时器
  16. self.timer.timeout.connect(self.update)
  17. self.timer.start(1000)
  18. def paintEvent(self,event):
  19. painter=QPainter(self)
  20. painter.setRenderHint(QPainter.Antialiasing)
  21. #设置表盘中的文字字体
  22. font=QFont("Times",6)
  23. fm=QFontMetrics(font)
  24. fontRect=fm.boundingRect("99")#获取绘制字体的矩形范围
  25. #分针坐标点
  26. minPoints=[QPointF(50,25),
  27. QPointF(48,50),
  28. QPointF(52,50)]
  29. #时钟坐标点
  30. hourPoints=[QPointF(50,35),
  31. QPointF(48,50),
  32. QPointF(52,50)]
  33. side=min(self.width(),self.height())
  34. painter.setViewport((self.width()-side)/2,(self.height()-side)/2,
  35. side,side)#始终处于窗口中心位置显示
  36. #设置QPainter的坐标系统,无论窗体大小如何变化,
  37. #窗体左上坐标为(0,0),右下坐标为(100,100),
  38. #因此窗体中心坐标为(50,50)
  39. painter.setWindow(0,0,100,100)
  40. #绘制表盘,使用环形渐变色
  41. niceBlue=QColor(150,150,200)
  42. haloGrident=QRadialGradient(50,50,50,50,50)
  43. haloGrident.setColorAt(0.0,Qt.lightGray)
  44. haloGrident.setColorAt(0.5,Qt.darkGray)
  45. haloGrident.setColorAt(0.9,Qt.white)
  46. haloGrident.setColorAt(1.0,niceBlue)
  47. painter.setBrush(haloGrident)
  48. painter.setPen(QPen(Qt.darkGray,1))
  49. painter.drawEllipse(0,0,100,100)
  50. transform=QTransform()
  51. #绘制时钟为0的字,以及刻度
  52. painter.setPen(QPen(Qt.black,1.5))
  53. fontRect.moveCenter(QPoint(50,10+fontRect.height()/2))
  54. painter.setFont(font)
  55. painter.drawLine(50,2,50,8)#
  56. painter.drawText(QRectF(fontRect),Qt.AlignHCenter|Qt.AlignTop,"0")
  57. for i in range(1,12):
  58. transform.translate(50, 50)
  59. transform.rotate(30)
  60. transform.translate(-50,-50)
  61. painter.setWorldTransform(transform)
  62. painter.drawLine(50,2,50,8)
  63. painter.drawText(QRectF(fontRect),Qt.AlignHCenter|Qt.AlignTop,"%d" % i)
  64. transform.reset()
  65. #绘制分钟刻度线
  66. painter.setPen(QPen(Qt.blue,1))
  67. for i in range(1,60):
  68. transform.translate(50,50)
  69. transform.rotate(6)
  70. transform.translate(-50,-50)
  71. if i%5!=0:
  72. painter.setWorldTransform(transform)
  73. painter.drawLine(50,2,50,5)
  74. transform.reset()
  75. #获取当前时间
  76. currentTime=QTime().currentTime()
  77. hour=currentTime.hour() if currentTime.hour()<12 else currentTime.hour()-12
  78. minite=currentTime.minute()
  79. second=currentTime.second()
  80. #获取所需旋转角度
  81. hour_angle=hour*30.0+(minite/60.0)*30.0
  82. minite_angle=(minite/60.0)*360.0
  83. second_angle=second*6.0
  84. #时针
  85. transform.translate(50,50)
  86. transform.rotate(hour_angle)
  87. transform.translate(-50,-50)
  88. painter.setWorldTransform(transform)
  89. painter.setPen(Qt.NoPen)
  90. painter.setBrush(QBrush(Qt.darkRed))
  91. painter.drawPolygon(QPolygonF(hourPoints))
  92. transform.reset()
  93. #分针
  94. transform.translate(50,50)
  95. transform.rotate(minite_angle)
  96. transform.translate(-50,-50)
  97. painter.setWorldTransform(transform)
  98. painter.setBrush(QBrush(Qt.darkGreen))
  99. painter.drawPolygon(QPolygonF(minPoints))
  100. transform.reset()
  101. #秒针
  102. transform.translate(50,50)
  103. transform.rotate(second_angle)
  104. transform.translate(-50,-50)
  105. painter.setWorldTransform(transform)
  106. painter.setPen(QPen(Qt.darkCyan,1))
  107. painter.drawLine(50,50,50,20)
  108. if __name__=="__main__":
  109. import sys
  110. app=QApplication(sys.argv)
  111. form=clockForm()
  112. form.show()
  113. app.exec_()

文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/119901975

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。