LOLs
LOLs

Reputation: 13

Python multiprocessing for updating pyqt5 gui

I want to pyqt5 gui using python's multiprocessing. Here is my code

class GUI(QWidget):
    trigger = pyqtSignal()
    i=0
    def __init__(self):
        super().__init__()
        self.setGeometry(0,0,500,200)
        self.b=QLabel(self)
        self.b.setText("hello")
        self.trigger.connect(lambda:self.f(self.b))
        self.show()
    
    def f(self,p):
        print("hello")
        p.setText("h"+str(self.i))
        self.i=self.i+1

    def update(self):
        for i in range(10):
            self.trigger.emit()
            time.sleep(2)

def work(obj):
    obj.update()
app = QApplication(sys.argv)
win=GUI()
if __name__ == '__main__':
    p=Process(target=work,args=(win,))
    p.start()
    app.exec_() 
    p.join()

I get error as

TypeError: cannot pickle 'GUI' object

I have done the solution moving to top module still no use.

Upvotes: 0

Views: 344

Answers (1)

eyllanesc
eyllanesc

Reputation: 244003

TL; DR; You cannot modify the GUI from another process.


The multiplocessing module uses the pickle ability to save and rebuild objects using the pickle module, but this module only You can do this task for objects with simple states, that is, for those objects whose behavior is determined by their properties but in complex objects this is not fulfilled. QObjects (and therefore QWidgets) are complex objects that have many internal states so it is impossible for them to be pickable.

Upvotes: 1

Related Questions