简体   繁体   English

Pyqt GUI 没有响应

[英]Pyqt GUI not responding

pyqt gui not responding pyqt gui没有响应

I'm trying to make a gui for my linkedin scraper program.我正在尝试为我的linkedin 刮板程序制作一个 gui。 But the GUI becomes not responding once the main program starts execution.但是一旦主程序开始执行,GUI 就会没有响应。 Its working fine till the main function is called .在调用 main 函数之前它工作正常。 Gui code is Gui代码是

 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 链接,我该怎么做? main function is a long one ang has many sub functions. main 函数很长,一个 ang 有很多子函数。 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. GUI 应用程序是围绕事件循环构建的:Qt 坐在那里从用户那里获取事件,并调用您的处理程序。 Your handlers have to return as quickly as possible, because Qt can't take the next event until you return.您的处理程序必须尽快返回,因为 Qt 在您返回之前无法处理下一个事件。

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.这就是 GUI“不响应”的含义:事件只是在排队,因为你没有让 Qt 对它们做任何事情。

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.有几种方法可以解决这个问题,但是,特别是对于 Qt,惯用的方法是启动一个后台线程来完成这项工作。

You really need to read a tutorial on threading in Qt.您确实需要阅读有关 Qt 中的线程的教程。 From a quick search, this one looks decent, even though it's for PyQt4.通过快速搜索,这个看起来不错,即使它是针对 PyQt4 的。 But you can probably find a good one for PyQt5.但是你可能会为 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:现在,您的doAction方法更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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