简体   繁体   中英

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. If Information is found the GUI should update itself.

I am new in Threading so i googled a lot and found some good HowTo's although it does not work as it should.

when i run the program it just ends itself after 3 s.

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.

In your particular case QThread needs it. 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_())

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