2020년 9월 8일 화요일

PyQt5 : Access parent from child widget

To access parent widget from child widget,

one have to explicitly set parent when define child. 

Then, one can access parent widget by using widget.parent()

Following is a short example.  

class test_parent_child(QWidget,):
    def __init__(self):
        super().__init__()
        self.a = 1
        self.b = 2
        self.child = QLabel('1',parent=self)        
parent_widg = test_parent_child()
print(parent_widg.child.parent().a)

>> 1 


댓글 없음:

댓글 쓰기