This article is mainly for everyone to introduce the PYQT5 implementation progress bar effect, with a certain reference value, interested in small partners can refer to
A progress bar is a control that is used when we are dealing with lengthy tasks. It is animated to let the user know that the task is making progress. The Qprogressbar control provides a horizontal or vertical progress bar. The programmer can set the minimum and maximum values for the progress bar. The default value is 0 to 99.
#!/usr/bin/python3#-*-coding:utf-8-*-"" "PYQT5 Tutorial This example shows a progress bar control. Author: My World You've been to the blog: Http://blog.csdn.net/weiaitaowang Last edited: August 3, 2016 "" "Import sysfrom pyqt5.qtwidgets Import Qapplication, Qwidget, Qprogressbar, qpushbuttonfrom pyqt5.qtcore import qbasictimerclass Example (qwidget): Def __init_ _ (Self): Super (). __init__ () Self.initui () def initui (self): Self.pbar = Qprogressbar (self) self.pbar.setGeometry (30, 4 0, self.btn = Qpushbutton (' Start ', self) self.btn.move (+) Self.btn.clicked.connect (self.doaction) self.time R = Qbasictimer () self.step = 0 Self.setgeometry (+, 280,) Self.setwindowtitle (' Progress bar ') Self.show () def time Revent (Self, E): If Self.step >= 100:self.timer.stop () self.btn.setText (' Done ') return self.step = self.step+1 Self.pbar.setValue (Self.step) def doAction (self, value): If Self.timer.isActive (): Self.timer.stop () Self.btn.setText (' Start ') Else:self.timer.start (self) self.btn.setText (' Stop ') if __name__ = = ' __main__ ': app =Qapplication (SYS.ARGV) ex = Example () sys.exit (App.exec_ ())
In our example, we have a horizontal progress bar control and a button control. Click the button to start or stop the progress bar.
Self.pbar = Qprogressbar (self)
Create a progress bar using Qprogressbar
Self.timer = Qbasictimer ()
To activate the progress bar, we need to use a timer object.
Self.timer.start (self)
To start the Timer event, we need to call its start () method. This method has two parameters: a timeout and an object that will receive the event.
def timerevent (self, e): if Self.step >=: self.timer.stop () self.btn.setText (' Done ') return self.step = self.step+1 self.pbar.setValue (self.step)
Each object that inherits from Qobject has a timerevent () event handler. In order for the timer event to function to the progress bar, we have rewritten this event handler.
def doAction (self, value): if Self.timer.isActive (): self.timer.stop () self.btn.setText (' Start ') else: Self.timer.start (self) self.btn.setText (' Stop ')
Use the DoAction () method to start and stop the timer.
After the program executes