简体   繁体   English

在GUI PyQT4旁边执行任务

[英]Threading a task beside a GUI PyQT4

So i am trying to run a PyQT GUI while another functions is gathering information in the background. 因此,我正在尝试运行PyQT GUI,而另一项功能是在后台收集信息。 If Information is found the GUI should update itself. 如果找到信息,则GUI应自行更新。

I am new in Threading so i googled a lot and found some good HowTo's although it does not work as it should. 我是线程技术的新手,因此我在Google上进行了很多搜索,并找到了一些不错的HowTo,尽管它不能正常工作。

when i run the program it just ends itself after 3 s. 当我运行程序时,它仅在3秒后结束。

Maybe you see some major mistake ive done. 也许您看到一些重大错误。

Here is the basic code i am trying to get to run 这是我试图运行的基本代码

class scan_Thread(QThread):
    def __init__(self, samp_rate, band, speed, ppm, gain, args, prn):
        QThread.__init__(self)
            self.samp_rate=samp_rate
            self.band=band
            self.speed=speed
            self.ppm=ppm
            self.gain=gain
            self.args=args
            self.prn=prn

        def __del__(self):
            self.wait()

        def run(self):
            do_scan(self.samp_rate, self.band, self.speed,
                    self.ppm, self.gain, self.args, self.prn)


def start_gui():
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
    #app.exec_()
    #sys.exit()

def main(options = None):

    def printfunc(found_list):
        for info in sorted(found_list):
            print info

    get_thread = scan_Thread(options.samp_rate, options.band, options.speed,
                options.ppm, options.gain, options.args, printfunc)
    get_thread.start()
    start_gui()

Thanks! 谢谢!

Many of the objects of the Qt classes and therefore of PyQt need to start some object of type Application (QCoreApplication, QtGuiApplication or QApplication), but only one of these objects must exist. Qt类的许多对象以及PyQt的许多对象都需要启动一些Application类型的对象(QCoreApplication,QtGuiApplication或QApplication),但是这些对象中只有一个存在。

In your particular case QThread needs it. 在您的特殊情况下,QThread需要它。 The previous classes are responsible for generating the necessary loops. 前面的类负责生成必要的循环。

So you should modify your code to the following: 因此,您应该将代码修改为以下内容:

def main(options = None):
    app = QtGui.QApplication(sys.argv) // before creating any PyQt object.

    def printfunc(found_list):
        for info in sorted(found_list):
            print info

    get_thread = scan_Thread(options.samp_rate, options.band, options.speed,
                options.ppm, options.gain, options.args, printfunc)
    get_thread.start()
    window = Window()
    window.show()
    sys.exit(app.exec_())

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

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