In this PyQT GUI application development tutorial, we're going to cover how to add a progress bar to your window.
There are many reasons why you may want a progress bar. Maybe you have a small installation GUI, or maybe you have some update taking place. Maybe you're downloading NSA files. Who knows.
To add a progress bar, the code is painstakingly simple:
self.progress = QtGui.QProgressBar(self) self.progress.setGeometry(200, 80, 250, 20)
That's all there is to it, but, if you run the window, you just have a boring progress bar. What do we actually do with this thing?
Let's make ours a "download" progress. So first we will add a button that initiates the download. After that, we will also need some sort of method that actually "downloads" something. We'll just fake it, but you will be able to modify the code to your liking. Generally with downloads, if the download is 100mb, each mb = 1%. Maybe it's an install, with 1000 files. Every 10 files is 1%, and so on.
So first, we need a simple button:
self.btn = QtGui.QPushButton('Download', self) self.btn.move(200, 120) self.btn.clicked.connect(self.download)
Now that we have this call to self.download, it sounds like we had better make that method:
def download(self): self.completed = 0 while self.completed < 100: self.completed += 0.0001 self.progress.setValue(self.completed)
In the while loop, we would, in theory, keep checking where we were in the download process. Maybe seeing how many megabytes complete out of total megabytes, and then assigning that value to the completed variable.
Now that we have the progress bar, a download initiation button, and the download method, we should be all set. Here's the full code:
import sys from PyQt4 import QtGui, QtCore class Window(QtGui.QMainWindow): def __init__(self): super(Window, self).__init__() self.setGeometry(50, 50, 500, 300) self.setWindowTitle("PyQT tuts!") self.setWindowIcon(QtGui.QIcon('pythonlogo.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.home() def home(self): btn = QtGui.QPushButton("Quit", self) btn.clicked.connect(self.close_application) btn.resize(btn.minimumSizeHint()) btn.move(0,100) 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(200, 80, 250, 20) self.btn = QtGui.QPushButton("Download",self) self.btn.move(200,120) self.btn.clicked.connect(self.download) self.show() def download(self): self.completed = 0 while self.completed < 100: 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 run(): app = QtGui.QApplication(sys.argv) GUI = Window() sys.exit(app.exec_()) run()
The result is:
Now that we've got that, let's a about drop-down buttons and quick window styling!