Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

python - PyQt5 loading spinner halts on post request

I'm using the QtWaitingSpinner found here: https://github.com/snowwlex/QtWaitingSpinner. You can create and start a spinner like so: spinner = QtWaitingSpinner(self); spinner.start(). Unfortunately when I try to make a POST request from my GUI, the spinner halts until a response has been returned. Consequently I don't see the spinner at all, or if I start the spinner prematurely it stops spinning while it waits for the response. I think I'll have to use some sort of asynchronous method like QThread or asyncio but it's unclear what the best way of getting around this is. If anyone can show me the best way to handle it I'd be grateful. Here is a simplified version of what I'm doing:

class Obj(QDialog):
    # some button calls this function when pressed
    def submit(self):
        #start spinner
        spinner = QtWaitingSpinner(self)
        spinner.start()

        # post some data to some url, spinner should spin
        r = requests.post('some_url.com', json=some_data)

        # stop spinner
        spinner.stop()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem you are requests is blocking the Qt loop, so elements like QTimer do not work. One solution is to run that task on another thread, a simple way to do it is using QRunnable and QThreadPool.

class RequestRunnable(QRunnable):
    def __init__(self, url, json, dialog):
        QRunnable.__init__(self)
        self.mUrl = url
        self.mJson = json
        self.w = dialog

    def run(self):
        r = requests.post(self.mUrl, json=self.mJson)
        QMetaObject.invokeMethod(self.w, "setData",
                                 Qt.QueuedConnection,
                                 Q_ARG(str, r.text))


class Dialog(QDialog):
    def __init__(self, *args, **kwargs):
        QDialog.__init__(self, *args, **kwargs)
        self.setLayout(QVBoxLayout())
        btn = QPushButton("Submit", self)
        btn.clicked.connect(self.submit)
        self.spinner = QtWaitingSpinner(self)

        self.layout().addWidget(btn)
        self.layout().addWidget(self.spinner)

    def submit(self):
        self.spinner.start()
        runnable = RequestRunnable("https://api.github.com/some/endpoint",
                                   {'some': 'data'},
                                   self)
        QThreadPool.globalInstance().start(runnable)

    @pyqtSlot(str)
    def setData(self, data):
        print(data)
        self.spinner.stop()
        self.adjustSize()

A complete example can be found in the following link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...