python 进度条百分比_如何使用PyQT4进度条以“目标”值的百分比显示当前值?

这篇博客介绍如何在PyQT4应用中使用进度条显示数据导入过程中的净销售额相对于目标销售额的百分比。通过创建一个窗口,设置菜单、按钮、进度条,并解析XML文件获取数据,博主展示了如何更新进度条的值来反映当前进度。读者可以学习到如何结合Qt库和XML处理实现这一功能。
摘要由CSDN通过智能技术生成

我从一个xml文件导入一些数据(netSales),我想将这个数字与指定为100的“目标值”进行比较。在

我的代码如下(来自PyQT进度条教程和我自己的一些代码):import sys

from PyQt4 import QtGui, QtCore

import xml.etree.ElementTree as ET

class Window(QtGui.QMainWindow):

def __init__(self,goal=100000):

super(Window, self).__init__()

self.setGeometry(50, 50, 500, 300)

self.setWindowTitle("Maar Dis Lekker!")

self.setWindowIcon(QtGui.QIcon('sales.png'))

extractAction = QtGui.QAction("&GET TO THE CHOPPAH!!!", self)

extractAction.setShortcut("Ctrl+Q")

extractAction.setStatusTip('Leave The App')

extractAction.triggered.connect(self.close_application)

self.statusBar()

mainMenu = self.menuBar()

fileMenu = mainMenu.addMenu('&File')

fileMenu.addAction(extractAction)

self.goal = goal

self.home()

def home(self):

btn = QtGui.QPushButton("Quit", self)

btn.clicked.connect(self.close_application)

btn.resize(btn.minimumSizeHint())

btn.move(0,100)

label = QtGui.QLabel(self)

pixmap = QtGui.QPixmap('BuildIt.png')

label.setPixmap(pixmap)

label.resize(label.minimumSizeHint())

label.move(20,175)

extractAction = QtGui.QAction(QtGui.QIcon('todachoppa.png'), 'Flee the Scene', self)

extractAction.triggered.connect(self.close_application)

self.toolBar = self.addToolBar("Extraction")

self.toolBar.addAction(extractAction)

checkBox = QtGui.QCheckBox('Shrink Window', self)

checkBox.move(100, 25)

checkBox.stateChanged.connect(self.enlarge_window)

self.progress = QtGui.QProgressBar(self)

self.progress.setGeometry(100, 80, 350, 80)

self.show()

def calculate(self):

currentNetSales = self.getSalesData()

while self.completed < self.goal:

self.completed += 0.0001

self.progress.setValue(self.completed)

def enlarge_window(self, state):

if state == QtCore.Qt.Checked:

self.setGeometry(50,50, 1000, 600)

else:

self.setGeometry(50, 50, 500, 300)

def close_application(self):

choice = QtGui.QMessageBox.question(self, 'Extract!',

"Get into the chopper?",

QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)

if choice == QtGui.QMessageBox.Yes:

print("Extracting Naaaaaaoooww!!!!")

sys.exit()

else:

pass

def getSalesData(self):

tree = ET.parse('barSales.xml')

root = tree.getroot()

for row in root.findall(".//RECORD/ROW"):

netSales = [float(row.attrib["nettsales"])]

return netSales

def run():

app = QtGui.QApplication(sys.argv)

GUI = Window()

sys.exit(app.exec_())

run()

问:如何使栏显示净销售额相对于目标销售额的百分比?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 PyQt5 和 tqdm 模块实现的更新进度条示例代码: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QProgressBar, QPushButton from PyQt5.QtCore import QThread, pyqtSignal from tqdm import tqdm class ProgressThread(QThread): progress_signal = pyqtSignal(int) def __init__(self, total): super().__init__() self.total = total def run(self): for i in tqdm(range(self.total)): self.progress_signal.emit(i) class ProgressBar(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(100, 100, 300, 100) self.setWindowTitle('Progress Bar') self.progress_bar = QProgressBar(self) self.progress_bar.setGeometry(20, 20, 260, 20) self.start_button = QPushButton('Start', self) self.start_button.setGeometry(20, 60, 75, 23) self.start_button.clicked.connect(self.start_progress) self.stop_button = QPushButton('Stop', self) self.stop_button.setGeometry(100, 60, 75, 23) self.stop_button.clicked.connect(self.stop_progress) self.show() def start_progress(self): self.thread = ProgressThread(100) self.thread.progress_signal.connect(self.update_progress) self.thread.start() def stop_progress(self): self.thread.terminate() def update_progress(self, value): self.progress_bar.setValue(value) if __name__ == '__main__': app = QApplication(sys.argv) bar = ProgressBar() sys.exit(app.exec_()) ``` 这个示例程序创建了一个简单的窗口,包含一个进度条和两个按钮。当用户点击“Start”按钮时,程序将启动一个后台线程来执行进度条更新任务。当用户点击“Stop”按钮时,程序将停止后台线程。进度条更新任务使用 tqdm 模块来显示进度条。通过使用 pyqtSignal,我们可以在后台线程和主线程之间进行通信,从而实现进度条的更新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值