简体   繁体   中英

Pyqt GUI not responding

pyqt gui not responding

I'm trying to make a gui for my linkedin scraper program. But the GUI becomes not responding once the main program starts execution. Its working fine till the main function is called . Gui code is

 class MainWindow(QMainWindow):
     def __init__(self):
         QMainWindow.__init__(self)

         self.setMinimumSize(QSize(720, 540))
         self.setWindowTitle("LinkedIn Scraper")

         self.nameLabel = QLabel(self)
         self.nameLabel.setText('Keywords:')
         self.keyword = QLineEdit(self)

         self.keyword.move(130, 90)
         self.keyword.resize(500, 32)
         self.nameLabel.move(70, 90)

         self.nameLabel = QLabel(self)
         self.nameLabel.setText('Sector:')
         self.sector = QLineEdit(self)

         self.sector.move(130, 180)
         self.sector.resize(500, 32)
         self.nameLabel.move(70, 180)

         self.btn = QPushButton('Download', self)
         self.btn.clicked.connect(self.doAction)
         self.btn.resize(200, 32)
         self.btn.move(270, 360)

         self.pbar = QProgressBar(self)
         self.pbar.setGeometry(110, 450, 550, 25)


     def doAction(self):
         print('Keyword: ' + self.keyword.text())
         print('Sector: ' + self.sector.text())
         main(self.keyword.text(),self.sector.text())

also want to link that progressbar with main , How can i do that ? main function is a long one ang has many sub functions. So i want to link that to each subfunctions

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM