Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make a dynamic pointer clock based on PyQt5

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to make a dynamic pointer clock based on PyQt5, which is very detailed and has a certain reference value. Friends who are interested must finish it!

Want to achieve such a function, and then there are no off-the-shelf components available in pyqt5, so I think it can only be achieved by drawing. When it comes to drawing, the turtle framework is undoubtedly the most common choice, but it can also be achieved through the QPainter component of pyqt5. And in the end, the effect is quite beautiful.

Implementation idea: by using the QPainter component of pyqt5 to draw a good clock chart, and finally through the timer to constantly change the current time display position on the chart. In this way, the process of constantly moving the pointer clock is finally realized.

Like the previous UI applications, we still use these three UI-related component libraries.

From PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import *

This time a new mathematical library is used because it involves the relevant parts of the data calculation.

From math import *

Apply operation-related modules

Import sys

I have put the main implementation process of the dynamic clock below, friends in need can study it by themselves.

Class PointerClock (QWidget): def _ init__ (self): super (). _ init__ () self.setWindowTitle ("dynamic pointer clock official account: [Python concentration Camp]") self.setWindowIcon (QIcon ('clock.ico')) self.timer = QTimer () # set window timer self.timer.timeout.connect (self.update) Self.timer.start (1000) def paintEvent (self Event): real-time refresh pointer image: param event:: return:''QPoint (int x, int y) define the coordinate points of hours, minutes and seconds, respectively. Create coordinate points X and y represent Abscissa and ordinate''hour_point = [QPoint (7,8), QPoint (- 7,8), QPoint (0,-30)] min_point = [QPoint (7,8), QPoint (- 7,8), QPoint (0,-65)] secn_point = [QPoint (7,8), QPoint (- 7,8), QPoint (0) Hour_color = QColor (182,98,0182) min_color = QColor (0130,130,155) sec_color = QColor (0,155,227,155)''get the minimum width and length of the QWidget object' 'min_size = min (self.width ()) Self.height () painter = QPainter (self) # create coordinate system Image drawing object painter.setRenderHint (QPainter.Antialiasing) # take the central position of the QWidget object as the central coordinate point of the drawing painter.translate (self.width () / 2, self.height () / 2) # scale the size painter.scale (int (min_size / 200) Int (min_size / 200) # Save status painter.save ()''draw the timescale of the clock dial' 'for ain range (0 60): if (a% 5)! = 0: # draw a tick mark as a minute tick line painter.setPen (min_color) painter.drawLine (92,0,96) 0) else: # draw a tick mark for every 5 lap 60 as an hourly tick line painter.setPen (hour_color) painter.drawLine (88,0,96 0) # draw hourly tick # rotate 6 degrees painter.rotate per minute # restore state painter.restore () 'draw the number on the clock dial' # Save state painter.save () # get the font object font = painter. Font () # set bold font.setBold (True) painter.setFont (font) # get font size font_size = font.pointSize () # set the previously defined color painter.setPen (hour_color) hour_num = 0 radius = 100 for i in range (0 12): # according to the 12-hour system If you draw an hour number every three hours, you need to traverse hour_num = I + 3 # 4 times and convert it according to the coordinate system of QT-Qpainter. The 3-hour tick line corresponds to the axis 0 degrees if hour_num > 12: hour_num = hour_num-12 # calculate the x, the number of hours written according to the font size. Position y x = radius * 0.8 * cos (I * 30 * pi / 180.0)-font_size y = radius * 0.8 * sin (I * 30 * pi / 180.0)-font_size / 2.0 width = font_size * 2 height = font_size painter.drawText (QRectF (x) Y, width, height), Qt.AlignCenter Str (hour_num)) # restore state painter.restore ()''draw the hours, minutes, Second pointer''# get current time time = QTime.currentTime () # draw hour pointer painter.save () # Unoutline painter.setPen (Qt.NoPen) # set hour pointer color painter.setBrush (hour_color) # hour pointer rotates counterclockwise Painter.rotate (30 * (time.hour () + time.minute () / 60)) # draw clock pointer painter.drawConvexPolygon (QPolygonF (hour_point)) # restore state painter.restore () # draw minute pointer painter.save () # Unoutline painter.setPen (Qt.NoPen) # Set the color of the minute pointer painter.setBrush (min_color) # rotate the minute pointer counterclockwise painter.rotate (6 * (time.minute () + time.second () / 60)) # draw the minute pointer painter.drawConvexPolygon (QPolygonF (min_point)) # restore the state painter.restore () # draw seconds Pointer painter.save () # Unoutline painter.setPen (Qt.NoPen) # set second hand color painter.setBrush (sec_color) # second pointer rotates counterclockwise painter.rotate (6 * time.second ()) # draw second pointer painter.drawConvexPolygon (QPolygonF (secn_point)) # restore status painter.restore ()

Finally, the entire App is started directly through the main () function.

If _ _ name__ = = "_ main__": app = QApplication (sys.argv) form = PointerClock () form.show () app.exec_ ()

Complete code

#-*-coding:utf-8-*-# @ author Python concentration Camp # @ date 2022 Universe @ file test9.py# done# uses pyqt5 to make a pointer clock to show the implementation time # want to achieve such a function, and then there is no ready-made component in pyqt5 to use, so I think it can only be achieved by drawing. # when it comes to drawing, the turtle framework is undoubtedly the most common choice, but it can also be achieved through the QPainter component of pyqt5. And the final # effect is quite beautiful. # realization idea: draw a good chart of the clock by using the QPainter component of pyqt5, and finally constantly change the display position of the current time on the chart through the timer. # this finally realizes a process in which the pointer clock is constantly moving. # like the previous UI applications, we still use these three UI-related component libraries. From PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * # uses a new mathematical library this time because it involves the relevant parts of the data calculation. From math import * # Application operation related modules import sysclass PointerClock (QWidget): def _ _ init__ (self): super (). _ init__ () self.setWindowTitle ("dynamic pointer clock official account: [Python concentration Camp]") self.setWindowIcon (QIcon ('clock.ico')) self.timer = QTimer () # set window timer Self.timer.timeout.connect (self.update) self.timer.start (1000) def paintEvent (self Event): real-time refresh pointer image: param event:: return:''QPoint (int x, int y) define the coordinate points of hours, minutes and seconds, respectively. Create coordinate points X and y represent Abscissa and ordinate''hour_point = [QPoint (7,8), QPoint (- 7,8), QPoint (0,-30)] min_point = [QPoint (7,8), QPoint (- 7,8), QPoint (0,-65)] secn_point = [QPoint (7,8), QPoint (- 7,8), QPoint (0) Hour_color = QColor (182,98,0182) min_color = QColor (0130,130,155) sec_color = QColor (0,155,227,155)''get the minimum width and length of the QWidget object' 'min_size = min (self.width ()) Self.height () painter = QPainter (self) # create coordinate system Image drawing object painter.setRenderHint (QPainter.Antialiasing) # take the central position of the QWidget object as the central coordinate point of the drawing painter.translate (self.width () / 2, self.height () / 2) # scale the size painter.scale (int (min_size / 200) Int (min_size / 200) # Save status painter.save ()''draw the timescale of the clock dial' 'for ain range (0 60): if (a% 5)! = 0: # draw a tick mark as a minute tick line painter.setPen (min_color) painter.drawLine (92,0,96) 0) else: # draw a tick mark for every 5 lap 60 as an hourly tick line painter.setPen (hour_color) painter.drawLine (88,0,96 0) # draw hourly tick # rotate 6 degrees painter.rotate per minute # restore state painter.restore () 'draw the number on the clock dial' # Save state painter.save () # get the font object font = painter. Font () # set bold font.setBold (True) painter.setFont (font) # get font size font_size = font.pointSize () # set the previously defined color painter.setPen (hour_color) hour_num = 0 radius = 100 for i in range (0 12): # according to the 12-hour system If you draw an hour number every three hours, you need to traverse hour_num = I + 3 # 4 times and convert it according to the coordinate system of QT-Qpainter. The 3-hour tick line corresponds to the axis 0 degrees if hour_num > 12: hour_num = hour_num-12 # calculate the x, the number of hours written according to the font size. Position y x = radius * 0.8 * cos (I * 30 * pi / 180.0)-font_size y = radius * 0.8 * sin (I * 30 * pi / 180.0)-font_size / 2.0 width = font_size * 2 height = font_size painter.drawText (QRectF (x) Y, width, height), Qt.AlignCenter Str (hour_num)) # restore state painter.restore ()''draw the hours, minutes, Second pointer''# get current time time = QTime.currentTime () # draw hour pointer painter.save () # Unoutline painter.setPen (Qt.NoPen) # set hour pointer color painter.setBrush (hour_color) # hour pointer rotates counterclockwise Painter.rotate (30 * (time.hour () + time.minute () / 60)) # draw clock pointer painter.drawConvexPolygon (QPolygonF (hour_point)) # restore state painter.restore () # draw minute pointer painter.save () # Unoutline painter.setPen (Qt.NoPen) # Set the color of the minute pointer painter.setBrush (min_color) # rotate the minute pointer counterclockwise painter.rotate (6 * (time.minute () + time.second () / 60)) # draw the minute pointer painter.drawConvexPolygon (QPolygonF (min_point)) # restore the state painter.restore () # draw seconds Pointer painter.save () # Unoutline painter.setPen (Qt.NoPen) # set second hand color painter.setBrush (sec_color) # second pointer rotates counterclockwise painter.rotate (6 * time.second ()) # draw second pointer painter.drawConvexPolygon (QPolygonF (secn_point)) # recovery status painter.restore () if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) form = PointerClock () form.show () app.exec_ () these are all the contents of the article "how to make a dynamic pointer clock based on PyQt5" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report