Python Forum
Ho can I get the first parent of a class? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Ho can I get the first parent of a class? (/thread-1513.html)



Ho can I get the first parent of a class? - panoss - Jan-09-2017

I have a main class.
This creates another class, achild.
achild creates another class, bchild.
bchild can get it's parent (achild) by self.parent(), but how can it get it's grandparent (main class)?

(ok, maybe I could use self.parent().parent(), but what about bchild 's children? how would they access to main class?)

Would it be a good practice if all children imported main class?
It doesn't seem very good to me.


RE: Ho can I get the first parent of a class? - Larz60+ - Jan-09-2017

You can create a method to do this by walking the tree. There may be a simpler way.
see http://stackoverflow.com/questions/13431139/python-treestructure-parent-and-children-class-children-gets-appended-to-its
You can probably take what you need from this tree example
(you don't need all the fluff).


RE: Ho can I get the first parent of a class? - panoss - Jan-10-2017

Thanks, that's gonna help me a lot!

Now, how about another case:
In QT Designer I make a form.
This is the container of all and will be: a QDialog or a QWidget or a QMainWindow.

I put 2 QGroupBox-es in it, QG1, QG2.
QG1 & QG2 can easily access the form by self.parent().

I put another groupbox, QG3, in QG2.
How can QG3 access the form?

(not with self.parent().parent())