-1

I have a Tkinter application that has multiple class based frames and I would like to call a function which is in tkinter Frame from another frame/class.

For example, my frames are like this:


    
class_B(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
            
        def Update_class_b():
        label = tk.Label(class_B, text=f"Welcome", font=LARGEFONT)
        label.grid(row=1, column=0)

        # wigdgets
        button = tk.Button(self, text="START_TRIP", command=lambda: controller.show_frame(D_file.D_class)
        #i am using this controller to navigate between those pages

        # packing/grid
        button.grid(row=2, column=0, padx=10, pady=10)

Now, I would like to call the function update_class_b from another class base frame. How can I do that right now I am passing class_B in label widget while making it as you can see and directly calling it in class_A, but it is not working. Please if anyone could help me regarding this. Also, I would like to call this update_class_b function from inside another class based frame like while pressing a button in Class_A this function should be triger

class A would be like

class class_A(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        def do_something_in_class_B():
            #this should trigger Update_class_b function in class_B

        button = tk.Button(self, text="DO SOMETHING IN CLASS B", command=do_something_in_class_B).pack()

controller class

class tkinterApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        self.frames = {}
        self.attributes('-fullscreen', True)
        


        for F in (file_A.Class_A,file_B.class_B):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(startPage.StartPage)


    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


if __name__ == "__main__":
    app = tkinterApp()
    app.mainloop()
lexi
  • 57
  • 5

1 Answers1

0

Assuming you have your frames stored in a dictionary in the controller frame:

func = controller.frames[class_B].update_class_b
button = tk.Button(self, text="START_TRIP", command=func)
button.grid(row=2, column=0, padx=10, pady=10)

We'd have to see a MCVE to give a specific answer.

Novel
  • 13,406
  • 2
  • 25
  • 41