0

I'm fairly new to python(only couple weeks learning) and I'm finishing my first application using python 3.6 and Pyqt5 for GUI. It consists in a blockchain wallet for my boss' company's private blockchain. I have everything working right(miracle if You ask me). I'm using threads so the GUI doesn't get frozen when the blockchain's API takes long to process. So I have this piece of code

def modalCreateAddress():
    name, ok = QInputDialog.getText(None, 'Name the address', 'Enter the address name:')
    if ok:
        _thread.start_new_thread(createAddress, (password, name))

This function createAddress as it seems, pretty obviously, calls the API for a new address creation, it sometimes takes a couple seconds but as it is on a thread the user is free to keep using the GUI and the function just looks like its not working. I wonder how I can manage to have a sort of loading indicator like "Generating new address..." or something like that. What's the best way to approach this situation? Maybe a little progress bar, or loading gif overlay? Would I have to get some sort of callback from the thread so I know when to hide the loading warning? If so, how can I do it?

Marciel Fonseca
  • 371
  • 1
  • 4
  • 19

1 Answers1

1

For this type of tasks you can use a QProgressDialog, in the following part there is an example:

import sys
import time
import thread

from PyQt5.QtCore import QObject, pyqtSignal, Qt
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QInputDialog, QApplication, QProgressDialog

class Message(QObject):
    finished = pyqtSignal()

def createAddress(password, name, obj):
    time.sleep(5)
    obj.finished.emit()


class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        lay = QVBoxLayout(self)
        button = QPushButton("Start processing")
        lay.addWidget(button)
        button.clicked.connect(self.start_task)
        self.message_obj = Message()

    def start_task(self):
        password = "password"
        name, ok = QInputDialog.getText(None, 'Name the address', 'Enter the address name:')
        if ok:
            self.progress_indicator = QProgressDialog(self)
            self.progress_indicator.setWindowModality(Qt.WindowModal)
            self.progress_indicator.setRange(0, 0)
            self.progress_indicator.setAttribute(Qt.WA_DeleteOnClose)
            self.message_obj.finished.connect(self.progress_indicator.close, Qt.QueuedConnection)
            self.progress_indicator.show()
            thread.start_new_thread(createAddress, (password, name, self.message_obj))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle("fusion")
    w = Widget()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241