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()

New contributor

Nick Shepherd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật