Python 3 tkinter button command inside another class

I am creating a Python program (3.4.3) - tkinter and wondering if it's possible to reference def(self.get_details) from inside another classfor a button command. I could not find the answer to this problem anywhere, so I decided that I would just ask.

Example:

import tkinter as tk
...

class Widgets(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init___(self, parent)
        self.parent = parent

        self.initUI()

    def initUI():

        # Lots of other different tkinter widgets go here

        self.button = tk.Button(command=App(get_details))
        self.button.pack()


class PopUp(tk.TopLevel): ....

class App(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.initUI()

    def get_details(self):

        # Complete a function

    def initUI(self):

        self.parent.title("My Application")
        self.style = Style()
        self.style.theme_use("default")

        self.pack()

        self.widgets = Widgets(self)
        self.widgets.pack(side="top", anchor="center", fill="both", expand=True)

if __name__ == "__main__":

    root = tk.Tk()
    App(root).pack(side="top", fill="both", expand=True)
    root.resizable(0,0)
    root.mainloop()

So I would like a button that belongs to a class Widgets()to invoke a command def get_details(self)that belongs to a class App()that has a class Widgets()packed inside it.

I hope that I am quite descriptive, it is rather difficult to formulate this problem. I'm still new to Python in general. Thank!

Edit:

, self.parent.get_details(), ! , tkinter Widgets() def get_details(), : self.button, :

AttributeError: 'App' object has no attribute 'button'

, : self.parent.button, :

AttributeError: 'tkapp' object has no attribute 'button'

/ ? !

+4
1

App Widgets self.widgets. , App Widgets. App Widgets self.parent. , self : App, self App. Widgets, self Widgets. , , .

App:

self             this App instance
.widgets         the Widgets instance it contains
.button          the button in that Widget instance

Widgets:

self             this Widgets instance
.parent          the App object that created this Widgets object
.get_details     the get_details function in the parent App object

(..., , Style ..) , . "", "", .

import tkinter as tk

class Widgets(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.initUI()

    def initUI(self):

        # Lots of other different tkinter widgets go here

        self.button = tk.Button(text='hello', command=self.parent.get_details)
        self.button.pack()

class App(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.initUI()

    def get_details(self):

        self.widgets.button.config(text='goodbye')

    def initUI(self):

        self.parent.title("My Application")

        self.pack()

        self.widgets = Widgets(self)
        self.widgets.pack(side="top", anchor="center", fill="both", expand=True)

if __name__ == "__main__":

    root = tk.Tk()
    App(root).pack(side="top", fill="both", expand=True)
    root.resizable(0,0)
    root.mainloop()
+1

All Articles