Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – How to pass extra data to an inherited class

Being newer to Python, I am not sure how to pass my configuration data to a class, which inherits a Frame.

This read/sets configuration data

class clsConfig:

    # init method/constructor
    def __init__(self):
        path = str(sys.path[0]) 
        self.srcPath = path + "\\"
        self.imagePath = self.srcPath + "Images" + "\\"
        ................

This class has configuration data, creates a notebook/tab calling my inherited class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class maingui:
    def __init__(self):

        # Set Config Values
        config = clsConfig()
        config.getConfiguration()
    
        print("Config-Path: "+ config.srcPath)

        # root
        self.root = Tk() #Makes the window
        self.root.wm_title("My tab") 

        # tab control - Main Control
        self.tabMain = ttk.Notebook(self.root)

        # *** I WANT TO PASS config DATA HERE ***
        tabWeather = clsWeatherTab(self.tabMain)
        self.tabMain.add(tabWeather,text="Weather")

This class is where I want to have available the config class data

class clsWeatherTab(Frame):
   def __init__(self,name,*args,**kwargs):
       Frame.__init__(self,*args,**kwargs)
       self.label = Label(self, text="Hi This is Tab1-seperated")
       self.label.grid(row=1,column=0,padx=10,pady=10)
       self.name = name
       
       # *** HERE I WANT TO USE PASSED config DATA ***
       print("Config-Path: "+ config.srcPath)

Any guidance would be great.

thank you,
Dean

>Solution :

All you have to do is pass the config structure to the constructor for the tag frame:

class maingui:
    def __init__(self):

        # Set Config Values
        config = clsConfig()
        config.getConfiguration()
...
        tabWeather = clsWeatherTab(self.tabMain, config)
        self.tabMain.add(tabWeather,text="Weather")
...
class clsWeatherTab(Frame):
   def __init__(self,name,config,*args,**kwargs):
       Frame.__init__(self,*args,**kwargs)
       self.config = config
       self.label = Label(self, text="Hi This is Tab1-seperated")
       self.label.grid(row=1,column=0,padx=10,pady=10)
       self.name = name
       
       print("Config-Path: "+ config.srcPath)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading