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 Library to realize dynamic Water Wave Progress Bar in Python programming

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

Share

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

This article mainly introduces "Python programming how to use PyQt5 library to achieve dynamic water wave progress bar", in daily operation, I believe many people in Python programming how to use PyQt5 library to achieve dynamic water wave progress bar problems there are doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Python programming how to use PyQt5 library to achieve dynamic water wave progress bar" doubts help! Next, please follow the small series to learn together!

directory

introduction of the principle

practical operation of code

introduction of the principle

If you look carefully, the static image effect is presented by the drawing of two lines in succession, and the waves are mainly caused by the horizontal dislocation of the two lines and the different transparency settings;

To create the final rippling visual effect, you only need to stitch together several static images of continuous lines.

The wave effect in the line, here used is the trend of the sine function, the two lines in the tutorial code mainly with the help of

y = Asin(wx+l)+k function plotting

y = Asin(wx+l)+k function

Two lines can achieve the effect of cross undulation, because the l value set in the two sine functions is different,

After explaining the basic principles, the following is the code implementation part. The image rendering effect is realized by QPellete(drawing board) in Qt. The line area in the image mainly uses the QPainterPath() function, which can surround an area with drawn lines.

The implementation of dynamic image effect is mainly to add a timer () function to the code, which plays the timer effect: the interface will be updated once every certain time (the static image obtained is also different, and the visual difference of this static image realizes dynamic effect);

The percentage of the image is automatically filled because the progress is increased by one at the same time as the page is updated;

practical operation of code

Function initialization: define background color, progress bar color, progress bar start and page refresh time, etc.

self.resize(200,60) self.layout = QGridLayout(self) #Background filled with grey self.setAutoFillBackground(True) p = QPalette() p.setColor(QPalette.Background,Qt.gray) self.setPalette(p) #Set progress bar color self.bg_color = QColor(255, 0, 0) #Set interface refresh time self.startTimer(80) self.m_waterOffset = 0.05 self.m_offset = 50 self.m_borderwidth = 10 #Progress bar Progress range 0-100 self.per_num = 0

painEvent function mainly completes the rendering of water wave effect and progress bar text, and is also the core of effect rendering.

def paintEvent(self, event): #jagged drawing board; painter = QPainter() painter.setRenderHint(QPainter.Antialiasing) painter.begin(self) #Get the width and height of the window width,height = self.width(),self.height() percentage = 1 - self.per_num/100 #Wave trend: sine function y = A(wx+l) + k # w represents the period, the larger the value, the higher the density w = 2 * math.pi / (width) # A represents amplitude, understood as the upper and lower amplitude of water waves A = height * self.m_waterOffset # k represents the offset of y, which can be understood as progress k = height *percentage water1 = QPainterPath() water2 = QPainterPath() #Starting point water1.moveTo(5,height) water2.moveTo(5,height) self.m_offset += 0.6 if(self.m_offset >(width/2)): self.m_offset = 0 i = 5 while(i < width-5): waterY1 = A*math.sin(w*i +self.m_offset ) + k waterY2 = A*math.sin(w*i + self.m_offset + width/2*w) + k water1.lineTo(i, waterY1) water2.lineTo(i, waterY2) i += 1 water1.lineTo(width-5,height) water2.lineTo(width-5,height) totalpath = QPainterPath() totalpath.addRect(QRectF(5, 5, self.width() - 10, self.height() - 10)) painter.setBrush(Qt.gray) painter.drawRect(self.rect()) painter.save() painter.setPen(Qt.NoPen) #Set transparency of waves watercolor1 =QColor(self.bg_color) watercolor1.setAlpha(100) watercolor2 = QColor(self.bg_color) watercolor2.setAlpha(150) path = totalpath.intersected(water1) painter.setBrush(watercolor1) painter.drawPath(path) path = totalpath.intersected(water2) painter.setBrush(watercolor2) painter.drawPath(path) painter.restore() '''Draw font''' m_font = QFont() m_font.setFamily('Microsoft YaHei') m_font.setPixelSize(int(self.width()/10)) painter.setPen(Qt.white) painter.setFont(m_font) painter.drawText(self.rect(),Qt.AlignCenter,"{}%".format(self.per_num)) painter.end()

Realize automatic progress increment function of progress bar, and update interface in real time to achieve dynamic effect.

def timerEvent(self, event): self.per_num +=1 if self.per_num ==101: self.per_num = 0 self.update() At this point, the study of "Python programming how to use PyQt5 library to implement dynamic water wave progress bar" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you 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.

Share To

Development

Wechat

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

12
Report