Pyqt GUI not responding

后端 未结 1 943
萌比男神i
萌比男神i 2021-01-07 15:24

pyqt gui not responding

\"\"

I\'m trying to make a gui for my linkedin scraper program. But the GUI bec

1条回答
  •  借酒劲吻你
    2021-01-07 15:44

    A GUI app is built around an event loop: Qt sits there taking events from the user, and calling your handlers. Your handlers have to return as quickly as possible, because Qt can't take the next event until you return.

    That's what it means for a GUI to be "not responding": the events are just queuing up because you're not letting Qt do anything with them.

    There are a few ways around this, but, with Qt in particular, the idiomatic way is to start a background thread to do the work.

    You really need to read a tutorial on threading in Qt. From a quick search, this one looks decent, even though it's for PyQt4. But you can probably find a good one for PyQt5.

    The short version is:

    class MainBackgroundThread(QThread):
        def __init__(self, keyword, sector):
            QThread.__init__(self)
            self.keyword, self.sector = keyword, sector
        def run(self):
            main(self.keyword, self.sector)
    

    And now, your doAction method changes to this:

     def doAction(self):
         self.worker = MainBackgroundThread(self.keyword.text(), self.sector.text())
         self.worker.start()
    

    0 讨论(0)
提交回复
热议问题