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 use PyQt5 to make dynamic clock in Python programming

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "Python programming how to use PyQt5 to make dynamic watches". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

Environment configuration

Realization idea

Old-fashioned watch making

Electronic watch making

Merge two tables interface

Core code

Realization idea

The idea of realization is roughly divided into three parts: old-fashioned watch making, electronic watch making, and two watches merging into one interface.

Old-fashioned watch making

Looking at the old watch interface as a whole, there are the following patterns: hour hand, minute hand, second hand, 1-12 hour scale, each small scale, one large scale every five frames.

When the clock is running, the hour hand, minute hand and second hand change with time and rotate counterclockwise with the center of the interface as the dot. This angle can be calculated.

For convenience, I use the QPainter control in PyQt5 to achieve the entire interface effect, which is actually a painter used to draw elements on the interface.

The clock track actually splices the pictures of each second together to form a coherent time series image, so the paintEvent function is rewritten to draw the real-time image of each second.

Def paintEvent (self, event): hour_points = [QPoint (5pje 8), QPoint (- 5pje 8), QPoint (0je Lay 30)] minute_points = [QPoint (5je 8), QPoint (- 5le 8), QPoint (0je Ling 65)] second_points = [QPoint (5je 8), QPoint (- 5je 8), QPoint (0le le 80)] hour_color = QColor (200Q 100m 0) Minute_color = QColor (0127127150) second_color = QColor (0160230150) min_len = min (self.width (), self.height ()) time = QTime.currentTime () # get the current time painter = QPainter (self) painter.setRenderHint (QPainter.Antialiasing) painter.translate (self.width () / 2 Self.height () / 2) # Pan to the center of the window painter.scale (min_len/200.0,min_len/200.0) # Zoom #-draw clockwise-painter.setPen (Qt.NoPen) painter.setBrush (hour_color) # Color painter.save () # according to 1 hour = 30 ° Painter.rotate (30.0 * ((time.hour () + time.minute () / 60.0)) painter.drawConvexPolygon (QPolygon (hour_points)) painter.restore () # save exit You can reset the brush painter.setPen (hour_color) # paint hourline (360,12 = 30 degrees) for i in range (12): painter.drawLine (88Let0Power96) # draw horizontal line painter.rotate (30.0) # rotate on the original rotation angle Radius = 100Radius font = painter.font () font.setBold (True) painter.setFont (font) pointSize = font.pointSize () # font size # print (pointSize) # drawing hour text for i in range (12): nhour = I + 3 # draw from horizontal 3 points if ( Nhour > 12): nhour-= 12 painter.drawText (self.textRectF (radius*0.8) PointSize,i*30), Qt.AlignCenter,str (nhour)) # draw minute hand Painter.setPen (Qt.NoPen) painter.setBrush (minute_color) painter.save () # 1 minute is 6 ° Painter.rotate (6.0 * (time.minute () + time.second () / 60.0) painter.drawConvexPolygon (QPolygon (minute_points)) painter.restore () # draw needle dividing line painter.setPen (minute_color) for i in range (60): if (I% 5! = 0): painter.drawLine (92) 96) painter.rotate (6) # draw the second hand painter.setPen (Qt.NoPen) painter.setBrush (second_color) painter.save () # draw the second line painter.rotate (6.0*time.second ()) painter.drawConvexPolygon (QPolygon (second_points)) painter.restore () painter.setPen (second_color ) for i in range: if (I% 5, or I% 30): # draw painter.drawLine (94) 96) painter.rotate (1.0) # rotation

Then set up a timing control, use the signal slot mechanism to connect the interface, and update the interface every second.

Self.timer = QTimer () # timer self.timer.timeout.connect (self.update) self.timer.start (1000) # update the electronic watch every 1s

Making electronic watches is much simpler than old-fashioned watch making, and QLCDNumber controls are used in the process.

The QLDNumber control is used to preview numbers, but the above style makes the numbers look more technological, so it is a very good choice to preview a spreadsheet!

You need to fine-tune the properties of the control before using it, such as font style, number of characters occupied in the control, border properties, etc.

Self.lcdNumber = QtWidgets.QLCDNumber (Form) self.lcdNumber.setGeometry (QtCore.QRect (0,0,250,50)) self.lcdNumber.setContextMenuPolicy (QtCore.Qt.DefaultContextMenu) self.lcdNumber.setFrameShape (QtWidgets.QFrame.NoFrame) self.lcdNumber.setSmallDecimalPoint (False) self.lcdNumber.setDigitCount (8) self.lcdNumber.setSegmentStyle (QtWidgets.QLCDNumber.Flat) self.lcdNumber.setProperty ("value" 2021.0) self.lcdNumber.setObjectName ("lcdNumber")

The running effect of the electronic watch is also with the help of the timing control to update the current interface every second, but QPainter is not used here.

Self.lcdNumber.display ('00self) # time_slot.setInterval (1000) # time_slot.start () time_slot.timeout.connect (self.event_1) time_slot.start (1000) def event_1 (self): time_format = QTime.currentTime () time_format = time_format.toString ("hh: Mm:ss ") self.lcdNumber.display (time_format) QApplication.processEvents () merge the interface of two tables

After the creation of the two table interfaces, we finally use a Widget as the base class and use the horizontal layout of Qt to join the two tables together horizontally to form the final effect.

Core code self.label1 = Clock_paint () self.label2 = MyWidget () self.horizon_layout = QHBoxLayout () self.horizon_layout.addWidget (self.label1) self.horizon_layout.addWidget (self.label2) self.setLayout (self.horizon_layout) self.setWindowTitle ('clock-"official account: Xiao Zhang Python"') self.setWindowIcon ( QIcon ('clock.jpg')) "how to make dynamic watches with PyQt5 in Python programming" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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