I am trying to split my code into different layers: GUI, reading the GUI, creating input, calculating the model, interpreting data, creating view, sending the view to the GUI. Somebody told me about MVC and I would like to try to build my program that way. But I'm a little confused about how to exchange information between those modules. I'm running into problems with undefined global names and so forth. My model is written in matlab and consists of a simulation. The GUI is supposed to present the data in plots.
My actual problem is: how to connect the different modules/classes/files to each other if they are supposed to be as independent as possible?
For example: I'm creating a GUI in one file, like this:
# userinterface.py class Userinterface(object): def __init__(self, controller): self.controller = controller self.buildGUI() return def buildGUI(): # build lots of widgets and stuff widget = tk.Thewidget(options=values, command=self.controller.onclick)
And call it my interface. In order to keep this file nice and clean, I would like to define all functions to handle button clicks and read fields in another file and I guess this is what you would call the controller part:
# main.py import userinterface as gui class Controls(object): def onclick(self): # read values from widgets and do stuff var1 = GUI.widget_entry.get() # Call some other function with var1 etc. if __name__ == '__main__': GUI_controls = Controls() GUI = gui.UserInterface(GUI_controls)
But how do I do to connect these two? In my example above, I'm handing over an instance of my Controls object. But then I read about Events too or Inheritance or subclasses.
I'm rather new to programming and started all this 2 months ago, so I'm struggling sorting all these theories.
If somebody could point out to me what the way of communication between my modules/files should be and what such a meeting-point looks like. Or just give me a link that I can study (like Javier did, although I'll have to read this thing more than once)