Pyqt GUI 没有响应 [英] Pyqt GUI not responding

查看:65
本文介绍了Pyqt GUI 没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pyqt gui 没有响应

pyqt gui not responding

我正在尝试为我的linkedin 抓取程序制作一个gui.但是一旦主程序开始执行,GUI 就没有响应.它工作正常,直到调用 main 函数.gui代码是

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

还想将该进度条与 main 链接,我该怎么做?main 函数是一个很长的一个 ang 有很多子函数.所以我想把它链接到每个子功能

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

推荐答案

GUI 应用程序围绕事件循环构建:Qt 坐在那里从用户那里获取事件,并调用您的处理程序.您的处理程序必须尽快返回,因为在您返回之前,Qt 无法接受下一个事件.

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.

这就是 GUI不响应"的含义:事件只是在排队,因为您没有让 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.

有几种方法可以解决这个问题,但是,特别是对于 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 中线程的教程.通过快速搜索,这个 看起来不错,即使它适用于 PyQt4.但是你可能会为 PyQt5 找到一个好的.

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.

简短版本是:

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)

现在,您的 doAction 方法更改为:

And now, your doAction method changes to this:

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

这篇关于Pyqt GUI 没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆