PyQt pass data from class

Michael Torrie torriem at gmail.com
Wed Nov 16 10:01:34 EST 2016


On 11/16/2016 12:47 AM, luca72 via Python-list wrote:
> Thanks for your reply
> 
> Is the latter, can you explain how i can do it.

Just add an argument to __init__() of the one class where you will pass
the instance of the other class to it. Then you can store it inside the
instance, and use it whenever needed.

class foo(object):
    def __init__(self):
	pass

    def some_method(self):
        print ("Hello from foo.some_method")

class bar(object):
    def __init__(self, foo_instance):
        self.foo_instance = foo_instance

    def some_method(self):
        foo.some_method()

a=foo()
b=bar(a)

Of course since you're dealing with PyQt your classes will involve some
other parameters to __init__ but you can still tack your parameter on there.

You may want to re-examine how you're building your classes, though.
There's likely a way you can structure things to eliminate this kind of
coupling, especially in the context of a GUI. Often times making your
own signal that can be externally connected to a method of the other
class is the way to go as this reduces the coupling between the classes.




More information about the Python-list mailing list