In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use pyqt5 to make pointer clock display time in Python", in daily operation, I believe many people in Python how to use pyqt5 to make pointer clock display time problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Python how to use pyqt5 to make pointer clock display time" doubts helpful! Next, please follow the small series to learn together!
To achieve such a function, and then pyqt5 has no ready-made components can be used, so I think it can only be achieved through drawing. When it comes to drawing, the turtle framework is undoubtedly the most common choice, but it can also be implemented through the QPainter component of pyqt5. And the final result was pretty good.
Implementation idea: through the use of 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 eventually achieves a process in which the hands of the clock are constantly moving.
As with the previous UI application, the UI related component libraries we use are still these three.
from PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *
This time, a new mathematical calculation library was used, because it involved data calculation related parts.
from math import *
Application operation-related modules
import sys
Dynamic clock of the main implementation process I put in the following, there is a need for friends can look at their own.
class PointerClock(QWidget): def __init__(self): super().__ init__() self.setWindowTitle("Dynamic Pointer Clock") 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): ''' 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 and y represent abscissa and ordinate 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)] '''Defines three colors, colors used to set three pointers later'' hour_color = QColor(182, 98, 0, 182) min_color = QColor(0, 130, 130, 155) sec_color = QColor(0, 155, 227, 155) '''Gets 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 center position of the QWidget object as the center coordinate point of drawing painter.translate(self.width() / 2, self.height() / 2) #Scale size painter.scale(int(min_size / 200), int(min_size / 200)) #Save state painter.save() '''Draw time marks on clock dial''' for a in range(0, 60): if (a % 5) != 0: #Draw a tick mark every 1/60 as a minute tick mark painter.setPen(min_color) painter.drawLine(92, 0, 96, 0) else: #Draw a tick mark every 5/60 as an hour tick mark painter.setPen(hour_color) painter.drawLine(88, 0, 96, 0) #Draw hour tick marks #Rotate 6 degrees per minute painter.rotate(360 / 60) #Recovery Status painter.restore() '' Draw the numbers on the clock dial '' #Get font object font = painter.font() #Set bold font.setBold(True) painter.setFont(font) #Get font size font_size = font.pointSize() #Set pre-defined colors painter.setPen(hour_color) hour_num = 0 radius = 100 for i in range(0, 12): #Draw an hour number every three hours on a 12-hour clock, which takes 4 iterations hour_num = i + 3 #converted according to QT-Qpainter coordinate system, 3 hour scale line corresponds to coordinate axis 0 degree if hour_num > 12: hour_num = hour_num - 12 #Calculate the x and y positions of the hour numbers according to the font size 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)) '''Draw hands for hours, minutes and seconds of a clock dial''' Get the current time time = QTime.currentTime() #Draw hour pointer #Cancel Contour painter.setPen(Qt.NoPen) #Set the color of the hour hand painter.setBrush(hour_color) #Hour hands rotate counterclockwise painter.rotate(30 * (time.hour() + time.minute() / 60)) #Draw clock hands painter.drawConvexPolygon(QPolygonF(hour_point)) #Draw minute pointer #Set the color of the minute pointer painter.setBrush(min_color) #Minute hands rotate counterclockwise painter.rotate(6 * (time.minute() + time.second() / 60)) painter.drawConvexPolygon(QPolygonF(min_point)) #Draw seconds pointer #Set second hand color painter.setBrush(sec_color) #seconds hand rotates counterclockwise painter.rotate(6 * time.second()) painter.drawConvexPolygon(QPolygonF(secn_point))
Finally, launch the entire App directly via the main() function.
if __name__ == "__main__": app = QApplication(sys.argv) form = PointerClock() form.show() app.exec_() At this point, the study of "how to use pyqt5 to make a pointer clock display time in Python" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.