python3.x+pyqt5实现主窗口状态栏里(嵌入)显示进度条功能

yipeiwu_com6年前Python基础

1、代码1:

(1)进度条等显示在主窗口状态栏的右端,代码如下:

from PyQt5.QtWidgets import QMainWindow, QProgressBar, QApplication, QLabel
import sys
class SampleBar(QMainWindow):
  """Main Application"""
  def __init__(self, parent = None):
    print('Starting the main Application')
    super(SampleBar, self).__init__(parent)
    self.initUI()
  def initUI(self):
    # Pre Params:
    self.setMinimumSize(800, 600)
    # File Menus & Status Bar:
    self.statusBar().showMessage('准备中...')
    self.progressBar = QProgressBar()
    self.label = QLabel()
    self.label2 = QLabel()
    self.label.setText("正在计算: ")
    self.label2.setText("正在计算: ")
    self.statusBar().addPermanentWidget(self.label)
    self.statusBar().addPermanentWidget(self.label2)
    self.statusBar().addPermanentWidget(self.progressBar)
    # self.statusBar().addWidget(self.progressBar)
    # This is simply to show the bar
    self.progressBar.setGeometry(0, 0, 100, 5)
    self.progressBar.setRange(0, 500) # 设置进度条的范围
    self.progressBar.setValue(100)
if __name__ == '__main__':
  app = QApplication(sys.argv)
  main2 = SampleBar()
  main2.show()
  sys.exit(app.exec_())

(2)实现的界面如下图1红框:

                                                                                           图1

2、代码2:

(1)进度条等显示在主窗口状态栏的左端,代码如下:

from PyQt5.QtWidgets import QMainWindow, QProgressBar, QApplication, QLabel, \
  QStatusBar, QPushButton
import sys
class SampleBar(QMainWindow):
  """Main Application"""
  def __init__(self, parent = None):
    # print('Starting the main Application')
    super(SampleBar, self).__init__(parent)
    self.initUI()
  def initUI(self):
    # Pre Params:
    self.setMinimumSize(800, 600)
    # File Menus & Status Bar:
    self.statusBar = QStatusBar()
    self.statusBar.setStyleSheet('QStatusBar::item {border: none;}')
    self.setStatusBar(self.statusBar)
    self.statusBar.showMessage('准备')
    self.progressBar = QProgressBar()
    self.pushbutton = QPushButton("点这里")
    self.label = QLabel()
    self.label2 = QLabel()
    self.label.setText("开始计算 ")
    self.label2.setText("正在计算: ")
    # self.statusBar.addWidget(self.label, 0)
    self.statusBar.addPermanentWidget(self.label, stretch=2)
    self.statusBar.addPermanentWidget(self.label2, stretch=0)
    self.statusBar.addPermanentWidget(self.progressBar, stretch=4)
    # self.statusBar().addWidget(self.progressBar)
    # This is simply to show the bar
    # self.progressBar.setGeometry(0, 0, 100, 5)
    self.progressBar.setRange(0, 500) # 设置进度条的范围
    self.progressBar.setValue(20)
if __name__ == '__main__':
  app = QApplication(sys.argv)
  main2 = SampleBar()
  main2.show()
  sys.exit(app.exec_())

2)实现的界面如下图2红框:

总结

以上所述是小编给大家介绍的python3.x+pyqt5实现主窗口状态栏里(嵌入)显示进度条功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

Python中的迭代器漫谈

问题是在Python中进行循环的时候产生的,熟悉Python的都知道,它没有类似其它语言中的for循环, 只能通过for in的方式进行循环遍历。最典型的应用就是通过range函数产生一...

Python基本数据结构之字典类型dict用法分析

本文实例讲述了Python基本数据结构之字典类型dict用法。分享给大家供大家参考,具体如下: 词典类型 dict 字典由键(key)和对应值(value)成对组成。字典也被称作关联数组...

Python开发虚拟环境使用virtualenvwrapper的搭建步骤教程图解

Python开发虚拟环境使用virtualenvwrapper的搭建步骤教程图解

virtualenv是一个创建隔绝的Python环境的工具。virtualenv创建一个包含所有必要的可执行文件的文件夹,用来使用Python工程所需的包。创建的环境是独立的,互不干扰,...

关于Python中浮点数精度处理的技巧总结

关于Python中浮点数精度处理的技巧总结

前言 最近在使用Python的时候遇到浮点数运算,发现经常会碰到如下情况: 出现上面的情况,主要还是因浮点数在计算机中实际是以二进制保存的,有些数不精确。 比如说: 0.1是十进制,...

Python寻找路径和查找文件路径的示例

Sys.path 指定用于模块搜索路径的字符串列表 也可以通过sys模块的append方法在Python环境中增加搜索路径。 Sys.path.append(‘/usr/bin/') /...