Home > Backend Development > Python Tutorial > Progress bar effect that you must learn every day in PyQt5

Progress bar effect that you must learn every day in PyQt5

不言
Release: 2018-04-19 10:55:10
Original
5487 people have browsed it

This article mainly introduces the progress bar effect of PyQt5 in detail, which has certain reference value. Interested friends can refer to it

The progress bar is when we deal with lengthy tasks The controls used. It is animated to let the user know that the task is progressing. The QProgressBar control provides a horizontal or vertical progress bar. Programmers can set the minimum and maximum values ​​of the progress bar. The default value is 0 to 99.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

这个例子显示了一个进度条控件。

作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月3日
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QProgressBar, QPushButton
from PyQt5.QtCore import QBasicTimer

class Example(QWidget):

 def __init__(self):
  super().__init__()

  self.initUI()

 def initUI(self):

  self.pbar = QProgressBar(self)
  self.pbar.setGeometry(30, 40, 200, 25)

  self.btn = QPushButton('开始', self)
  self.btn.move(40, 80)
  self.btn.clicked.connect(self.doAction)

  self.timer = QBasicTimer()
  self.step = 0

  self.setGeometry(300, 300, 280, 170)
  self.setWindowTitle('进度条')  
  self.show()

 def timerEvent(self, e):

  if self.step >= 100:
   self.timer.stop()
   self.btn.setText('完成')
   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('开始')
  else:
   self.timer.start(100, self)
   self.btn.setText('停止')

if __name__ == '__main__':

 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())
Copy after login

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)
Copy after login

Use QProgressBar to create a progress bar

self.timer = QBasicTimer()
Copy after login

To activate the progress bar, we need to use A timer object.

self.timer.start(100, self)
Copy after login

To start the timer event, we need to call its start() method. This method takes two parameters: the timeout and the object of the event that will be received.

 def timerEvent(self, e):

  if self.step >= 100:
   self.timer.stop()
   self.btn.setText('完成')
   return
  self.step = self.step+1
  self.pbar.setValue(self.step)
Copy after login

Every object that inherits from QObject has a timerEvent() event handler. In order for the timer event to apply to the progress bar, we rewrote this event handler.

 def doAction(self, value):

  if self.timer.isActive():
   self.timer.stop()
   self.btn.setText('开始')
  else:
   self.timer.start(100, self)
   self.btn.setText('停止')
Copy after login

Use the doAction() method to start and stop the timer.

After the program is executed

Progress bar effect that you must learn every day in PyQt5Progress bar effect that you must learn every day in PyQt5Progress bar effect that you must learn every day in PyQt5

Related recommendations:

PyQt5 must be learned every day QSplitter implements window separation

The tool tip function that PyQt5 must learn every day

The above is the detailed content of Progress bar effect that you must learn every day in PyQt5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template