Reference to calling class inside called class?

I have two .py files: a tkinter UI file and a backend file. I want functions to be called from button events in the UI file, handled by the backend, and then reflected in the UI as a response. for example:

Main window file:

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.loadDirectory = tk.Button(leftConsole, text = 'Load sort folder',
                                       command = lambda: backend.loadDirectory())

Aaaaand backend:

class Backend(object):
    def __init__(self, master=None):
        super().__init__()
        self.master = master
    def loadDirectory(self):
        sort_folder_path = askdirectory(title='Select Folder to Sort')
        self.fileNameLabel.config(text = sort_folder_path)

It works right until I go and try to configure the text in the calling class’ widget. Ive messed around with the super() and init() functions, reversing which class calls which, etc., but I can’t get this thing to be a two-way street.

Answer

Typically, you either have a controller class which acts as an intermediary that can return instances of the classes, or one object will pass itself to any other object that needs it.

You didn’t show in your example where or how your classes are constructed, but a simple method is just to pass the instance of app to any function that needs it:

class Application(tk.Frame):
    def __init__(self, master=None):
        ...
        self.loadDirectory = tk.Button(..., command = lambda: backend.loadDirectory(app=self))

class Backend(object):
    def loadDirectory(self, app):
        sort_folder_path = askdirectory(title='Select Folder to Sort')
        app.fileNameLabel.config(text = sort_folder_path)

Using a controller, it might look something like this:

class Controller:
    def __init__(self, root):
        self.app = App(root, controller=self)
        self.backend = Backend(root, controller=self)

class Application(tk.Frame):
    def __init__(self, master=None, controller=None):
        ...
        self.controller = controller
        ...
        self.loadDirectory = tk.Button(..., command = self.controller.backend.loadDirectory)

class Backend(object):
    def __init__(self, root, controller):
        ...
        self.controller = controller
        ...

    def loadDirectory(self):
        sort_folder_path = askdirectory(title='Select Folder to Sort')
        self.controller.app.fileNameLabel.config(text = sort_folder_path)

root = tk.Tk()
controller = Controller(root)