[Python] I can't seem to get a function in one class to properly call a function in another...

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

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

    Continue reading...

Compartilhe esta Página