pythonclasstkintercustomtkinter

I can't seem to get a function in one class to properly call a function in another class in tkinter - Python


I am having trouble getting this text box to print text when the button from Class3 is pressed. If I call the same function from Class2 then everything is fine. But from Class 3 not so much. I can still call the function (It even prints to the console) But it does not change the Text window. Can't figure out what I am missing. I am still learning classes in python so having trouble phrasing my questions online to get the desired results.

import tkinter as tk

def mainGUI():
    app = Class1()
    app.mainloop()
    

class Class1(tk.Tk):
    def __init__(self):
        super().__init__()
        
        self.title("test")
        frame1 = Class2(self)
        frame1.pack()
        frame2 = Class3(self)
        frame2.pack()


class Class2(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        
        button1 = tk.Button(self, text='Class2 Button', command=self.doStuff)
        button1.pack()

        self.text_box = tk.Text(self)
        self.text_box.pack()

    def doStuff(self):
        self.text_box.insert(tk.END, "You Pushed the button!\n")
        print("I can print to console though")


class Class3(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        button2 = tk.Button(self, text='Class3 Button', command=Class2(parent).doStuff)
        button2.pack()

mainGUI()

Solution

  • Class2 and Class3 are both passed the root object. They shouldn't need to know anything about each other -- any connections should happen in the root class.

    Remember, you need to call a method of that OBJECT, not just the class. What you are doing now is creating a brand new Class2 object in the callback -- it's not the Class2 object you created above. If Class3 needs to send to Class2, the proper way is to have Class3 call a callback in the root, and have the root forward that to people who need it. For example:

    import tkinter as tk
    
    def mainGUI():
        app = Class1()
        app.mainloop()
        
    
    class Class1(tk.Tk):
        def __init__(self):
            super().__init__()
            
            self.title("test")
            self.frame1 = Class2(self)
            self.frame1.pack()
            self.frame2 = Class3(self)
            self.frame2.pack()
    
        def class3_button(self):
            self.frame1.doStuff()
    
    
    class Class2(tk.Frame):
        def __init__(self, parent):
            super().__init__(parent)
            
            button1 = tk.Button(self, text='Class2 Button', command=self.doStuff)
            button1.pack()
    
            self.text_box = tk.Text(self)
            self.text_box.pack()
    
        def doStuff(self):
            self.text_box.insert(tk.END, "You Pushed the button!\n")
            print("I can print to console though")
    
    
    class Class3(tk.Frame):
        def __init__(self, parent):
            super().__init__(parent)
    
            button2 = tk.Button(self, text='Class3 Button', command=parent.class3_button)
            button2.pack()
    
    mainGUI()