Adrian P. Adrian P. - 10 months ago 51
Python Question

Create "loading window" in PyQt4

I have a program that graphs data using an embedded matplotlib plot and adds helpful visualizations. The problem is matplotlib can be very slow when these visualizations are heavy, to the point where the main window hangs. I can't add these graphics in a different thread or process because PyQt forbids updating the main window from anywhere but the main thread (for good reason), so instead I would like to have another window with basically a perpetual progress bar that I can kill when the graphing is done:

# Creates a new window with an endless progress bar
class LoadingWindow(QtGui.QMainWindow):
def __init__(self):
super(LoadingWindow, self).__init__()

main_widget = QtGui.QWidget(self)
layout = QtGui.QVBoxLayout()
layout.addWidget(QtGui.QLabel("Graphing now. Please wait...", self))
progress = QtGui.QProgressBar(self)


... but when I create an instance of this class the loading bar doesn't show up and both windows hang. Is there a way to make this window come up in a new thread or process? Or is there a better way to fix this problem in general?


I figured out my problem. It's not a total fix but I had to go back through to where the visualizations are being added and periodically call this function:


This let my UI update every once in a while so that the window at least didn't hang. I also thought it best to disable the main window so that the user can't mess things up while the graphs are being generated, and I added the Loading Window from my original post so that the user knows whats happening.