Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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)

share|improve this question
    
welcome to programmers. In order to get a good answer, it is common practice here to ask a question :-) –  Simon Jul 1 '13 at 12:35
    
Please consider editing your question and being more specific about the problem you are having with setting up MVC. –  GlenH7 Jul 1 '13 at 12:59
    
GUI MVC (the original MVC) is not a layered architecture, it's a modular design. See Fowler's description –  Javier Jul 1 '13 at 20:41

1 Answer 1

up vote 2 down vote accepted

You're on the right track. The controller is what binds the app together, so passing it around to the various view and model classes is fine.

However, don't think that just because you have a controller, all widgets must use that controller for everything. For instance, I wouldn't put the onclick method in the controller. The controller should be for routing business logic, but what you're doing is mostly specific to the GUI.

I think a good general rule of thumb is that GUI events and callbacks should be within the GUI module. They then call the controller when interfacing with business logic. Fetching a string from the GUI itself is not business logic.

So, instead of:

# GUI module
button = Button(..., command=controller.on_click)

# Controller module
def on_click():
    var1 = GUI.widget_entry.get()
    self.do_something(var1)

I would do this:

# GUI module
button = Button(..., command=on_click)
def on_click():
    var1 = widget_entry.get()
    controller.do_something(var1)

With the above, the controller doesn't need to know what widgets are part of the GUI. All it does is provide an interface through which the GUI can request services.

If you prefer to keep the on_click in the controller, that's possible too, and not completely unreasonable. However, instead of building in knowledge of the GUI widgets into the controller, give the GUI an interface that the controller can use.

For example, instead of doing GUI.widget_entry.get(), the controller would call an accessor such as GUI.get_entry_widget_value(). That allows you to refactor the GUI without having to change the controller. If you move the entry to the_widget_entry instead of widget_entry, the controller can be completely unaware and still function properly.

share|improve this answer
    
It turned out that I wanted the input reading back in the GUI file. Just as you recommended, I now get my input-fields by calling data = GUI.getinputs() which is defined inside the GUI file and simply returns EVERYTHING. I need to prepare that input quite a bit before starting my simulation anyway, but that will again just be done like this: formated = translator.format(data) Everything is very clean and it's easy to follow the controllers workflow, that was my goal! –  lyvic Jul 2 '13 at 14:36
    
If you like the answer and it was useful, consider voting it up and possibly accepting it. Though, it's always good to wait a couple of days to see if there are better answers before accepting the first one. For help on voting and accepting see What should I do when someone answers my question? –  Bryan Oakley Jul 2 '13 at 14:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.