I'm creating a Python (3.4.3) - tkinter program and am wondering whether it is possible to reference a def (self.get_details) from inside another class for a button's command. I haven't been able to find an answer to this problem anywhere, so I figured I'd 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 which belongs to the class Widgets() to call a command that is def get_details(self) which belongs to the class App(), which has the Widgets() class packed inside it.
I hope I was descriptive enough, it is fairly hard to word this problem. I am still kind of new to Python in general. Thanks!
Edit:
As suggested I changed it to self.parent.get_details(), which worked! However when I reference a tkinter widget from the Widgets() class inside def get_details() for example: self.button, I get:
AttributeError: 'App' object has no attribute 'button'
So I tried referencing the button as: self.parent.button, to which I received:
AttributeError: 'tkapp' object has no attribute 'button'
How should I be calling/referencing the button? Thanks!
self.parent.get_details()should work. Have you tried that?Widgetsobject while you're inget_details, useself.widgets.button. Whichever object you're in,selfrefers to an instance of that object.