Class Inheritance Issue.... #1058
              
                
                  
                  
                    Answered
                  
                  by
                    thetrotfreak
                  
              
          
                  
                    
                      AaronCatolico
                    
                  
                
                  asked this question in
                Q&A
              
            -
| I have two classes in this very simple example: Control1 In Control1, I have a red container box that I want to turn to blue when I click the button in the Control2 class. I know that it requires inheritance or a subclass, but not sure how to implement it. Here's the code that I currently have: How do I properly inherit the Control1 class into Control2 class to access the Control1 self.con1 to change it's color to blue after clicking the self.btn1 in Control2 class? | 
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            thetrotfreak
          
      
      
        Feb 20, 2023 
      
    
    Replies: 1 comment 6 replies
-
| @AaronCatolico , maybe the following will help. import flet as ft
class Control1(ft.UserControl):
    def __init__(self):
        super().__init__()
        self.con1 = ft.Ref[ft.Container]
    def build(self):
        return ft.Container(
            ref=self.con1,
            width=200,
            height=200,
            bgcolor='red',
            border_radius=6
        )
class Control2(Control1):
    def __init__(self):
        super().__init__()
    def change_color(self, e):
        # The function that will change 'self.con1' container to blue, located in the
        # 'Control1' class.
        self.con1.current.bgcolor = ft.colors.BLUE
        self.con1.current.update()
        # pass
    def build(self):
        self.btn1 = ft.TextButton(
            text='CHANGE COLOR',
            style=ft.ButtonStyle(bgcolor='#5d5d5d', color='white'),
            on_click=self.change_color
        )
        return self.btn1
def main(page: ft.Page):
    page.window_width = 600
    page.window_height = 400
    page.bgcolor = '#333333'
    page.window_center()
    page.theme_mode = 'light'
    page.padding = 10
    page.update()
    page.add(Control1(), Control2())
if __name__ == '__main__':
    ft.app(target=main, assets_dir='assets') | 
Beta Was this translation helpful? Give feedback.
                  
                    6 replies
                  
                
            
      Answer selected by
        AaronCatolico
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
@AaronCatolico , maybe the following will help.