The full source code implementation is included at the end of the article...
I wanted to implement such a feature, and then there is no ready-made component in pyqt5 to use, so I thought I could only do it by drawing. When it comes to drawing, turtle framework is undoubtedly the most common choice, but in fact through the pyqt5 QPainter component can also be realized. And the final result is quite beautiful.
Idea: by using pyqt5 QPainter component to draw a good clock chart, and finally through the timer constantly change the current time in the chart above the display position. This ultimately realizes a pointer clock in the process of constant movement.
As with the previous UI application, the UI-related component libraries we use are the same three.
from import * from import * from import *
This time a new math calculation library was used, as it involved data calculation related parts.
from math import *
Application Operation Related Modules
import sys
I put the main implementation process of the dynamic clock below, if you need friends can study it themselves.
class PointerClock(QWidget): def __init__(self): super().__init__() ("Dynamic Pointer Clock") (QIcon('')) = QTimer() # Set the window timer () (1000) def paintEvent(self, event): ''' Refresh pointer image in real time :param event. :return. '''Define coordinate points for hours, minutes, and seconds respectively''' QPoint(int x, int y);create coordinate point, x, y represent horizontal and vertical coordinates respectively 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, -80)] '''Define three colors to be used later to set the color of the three pointers''' hour_color = QColor(182, 98, 0, 182) min_color = QColor(0, 130, 130, 155) sec_color = QColor(0, 155, 227, 155) '''Get the minimum values for the width and length of a QWidget object''' min_size = min((), ()) painter = QPainter(self) # Create coordinate system image drawing objects () # Use the center of the QWidget object as the center coordinate point of the drawing (() / 2, () / 2) # Scale the dimensions (int(min_size / 200), int(min_size / 200)) # Preservation status () '''Drawing the time scale of a clock dial''' for a in range(0, 60): if (a % 5) != 0: # One scale drawn every 1/60 as a minute scale (min_color) (92, 0, 96, 0) else: # One scale drawn every 5/60 as an hourly scale (hour_color) (88, 0, 96, 0) # Drawing the hourly scale # Rotate 6 degrees per minute (360 / 60) # Recovery status () '''Draw the numbers above the clock dial''' # Get the font object font = () # Setting Bold (True) (font) # Get font size font_size = () # Set the previously defined color (hour_color) hour_num = 0 radius = 100 for i in range(0, 12): # Hourly figures are plotted every three hours on a 12-hour basis, requiring four iterations. hour_num = i + 3 # Converted to QT-Qpainter's coordinate system, where the 3-hour scale corresponds to 0 degrees on the coordinate axis if hour_num > 12: hour_num = hour_num - 12 # Calculate the x, y position of the written hourly digits based on the size of the font 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 (QRectF(x, y, width, height), , str(hour_num)) '''Drawing the hands of a clock dial for hours, minutes and seconds''' # Get the current time time = () # Drawing the hour pointer # Cancel contour lines () # Set the color of the hour hand (hour_color) # Hour hand rotates counterclockwise (30 * (() + () / 60)) # Drawing clock hands (QPolygonF(hour_point)) # Drawing minute pointers # Set the color of the minute hand (min_color) # Minute hand rotates counterclockwise (6 * (() + () / 60)) (QPolygonF(min_point)) # Drawing the seconds pointer # Setting the second hand color (sec_color) # The second hand rotates counterclockwise (6 * ()) (QPolygonF(secn_point))
Finally, it's still straightforward to start the whole app via the main() function.
if __name__ == "__main__": app = QApplication() form = PointerClock() () app.exec_()
The complete source code for the dynamic clock is shown below:
http://xiazai./202202/yuanma/pythonshizh_jb51.rar
to this article on the use of pyqt5 in Python to create a pointer clock to show real-time time (dynamic pointer clock) of the article is introduced to this, more related pyqt5 pointer clock to show real-time content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!