Python Forum

Full Version: Help with GUI and MVC pattern
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I need some help understanding the MVC pattern, i am using python. As i understood by reading around, there are two ways to implement this.

First Way
Viewer/controller can talk to the Model and the model can publish some data (that the viewer/controller can eventually read).

Second Way
Controller takes, in the constructor, both Model and Viewer objects so that the system is fully layered. In this way, the controller takes input from the viewer and send them to the model and vice-versa to update the display on the GUI.

I am a bit lost here, the first way of doing it seems a bit easier to implement, however, i don't see a neat separation between controller and
viewer. In second way, on the other hand, the separation is neat. I would structure the code in the following way:

model.py

class model(self):

    def __init__(self):
        some_data = ...

    def writeToDB(self)
        ....
    
    def readFromDB(self)
        ....
viewer.py

class viewer(self):

    def __init__(self):
        some_data = ...
        build_gui

    def onEventButton1(self)
        doSomething()
    
    def UpdateText(self, data)
        updateText(data)
controller.py
class controller(self, model, controller):

    def __init__(self):
        self.model = model
        self.controller = controller
        some_data = ...

    def method1(self)
        data = self.model.readFromDB()
        self.viewer.updateText(data)
    

This is my question. If i do not admit circular dependencies, i cannot instantiate an object of controller into the viewer class.
How can i, then, press a button in the viewer class (GUI) and call the "method1" in controller?

Thanks
Found a solution!! I hope it can help other people!
I used Pypubsub to create publisher-subscriber. This way you can create commnunication channels in the MVC.