How to use functions from one class to another in python tkinter

Viewed 273

How can I use a function from class MainPage in class AddPart? I have some functions in class Mainpage and I want to use all the functions that are in class MainPage in another class AddPart.

class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) #initialise parent class --> TSS()


    def main_heading(self, heading):
        main_label = tk.Label(self, text = heading, font = LARGE_FONT, fg = 'white', bg = 'RoyalBlue1', width = 1000, height = 4)
        main_label.pack(side = 'top')

    def sub_heading(self, title):
        sub_label = tk.Label(self, text = title, font = MED_FONT, fg = 'RoyalBlue2')
        sub_label.pack(side = 'top', padx = 20, pady = 20)

    def paragraph(self, para):
        paragraph_label = tk.Label(self, text = para, font = SMALL_FONT, fg = 'gray26')
        paragraph_label.pack(side = 'top', padx = 20, pady = 20)

    def top_tool(self):
        toolbar = tk.Frame(self, bg = 'RoyalBlue2')
        AddBtn = tk.Button(toolbar, text = 'ADD', width = 10, bg = 'RoyalBlue2', fg = 'white', borderwidth=0)
        AddBtn.pack(side = 'left', padx = 0, pady = 0)
        RemBtn = tk.Button(toolbar, text = 'REMOVE', width = 10, bg = 'RoyalBlue2', fg = 'white', borderwidth=0)
        RemBtn.pack(side = 'left', padx = 0, pady = 0)
        RevBtn = tk.Button(toolbar, text = 'REVIEW', width = 10, bg = 'RoyalBlue2', fg = 'white', borderwidth=0)
        RevBtn.pack(side = 'left', padx = 0, pady = 0)
        AssignBtn = tk.Button(toolbar, text = 'ASSIGN EVENTS', width = 15, bg = 'RoyalBlue2', fg = 'white', borderwidth=0)
        AssignBtn.pack(side = 'left', padx = 0, pady = 0)
        toolbar.pack(side = 'top', fill = 'x')

#-----Second Class------
class AddPart(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.top_tool()
        self.main_heading("Home")

    def main_heading(self, heading):
        main_label = tk.Label(self, text = heading, font = LARGE_FONT, fg = 'white', bg = 'RoyalBlue1', width = 1000, height = 4)
        main_label.pack(side = 'top')
2 Answers

Did you try to inherit the base class? class AddPart(MainPage):

instead of class AddPart(tk.Frame, MainPage):, because MainPage is already a tk.Frame.

Inheritance

I want to use all the functions that are in class MainPage in another class AddPart

This is just what inheritance is used for. As @progmatico and @mondlos pointed out, by inheriting MainPage in AddPart you will have access to all the base classes functions.

It looks like both of your classes have functions that depend on the class (unless I am misunderstanding the tk module you are using), so using something like a static method would not work well.

From your comment, you are getting the TypeError: Cannot create a consistent method resolution order (MRO) for bases error when trying @mondlos suggestion. This is because you are inheriting a type of tk.Frame from the MainPage that you are also including in AddPart. Doing what @progmatico recommends should work. There are several answers suggesting the double inheritance causes this issue at this SO question: TypeError: Cannot create a consistent method resolution order (MRO)

Related