1

I am creating a pretty big application using tkitner and since the app is growing, maintaining it is getting difficult and hence I decided to restructure using MVC design pattern. My understanding of MVC is that:

  • View does not know anything about the Model or Controller
  • Model does not know anything about the View or Controller
  • Controller links the Model and View and also might contain some other code for the application.

Question 1

If I have push buttons or some other widgets which may manipulate some data in my Model, how do I set the command/bind for them? What I am thinking is: create the widgets without the command/bind (just the looks, no functionality) and then call this View class in the Controller and use config() for those widgets in the Controller to bind functions. This would mean that the methods must be defined in the controller class. I want to know if there is a better way to achieve this(most of the times there is). Example:

def View(tk.Tk):
    def __init__(self):
        super().__init__()
        self.btn = ttk.Button(self, text='Hi')
        self.btn.pack()
def Controller:
    def __init__(self):
        self.view = View()
        self.view.btn.config(command=doSomething)
    def doSomething(self):
        # rest of the code
if __name__ == '__main__':
    app = Controller()
    app.view.mainloop()

Question 2

How do I set my feedback? If I use the above method, then I can change the Model data by using the push button (after I define something under doSomething which will work with my model). But now if I have a label that displays that data, then how do I update the label when the data is manipulated. In my case, the data is manipulated not only from the GUI but also from other sources.

Question 3

I have 3 different pages to display depending on the scenario. I am doing it like how Bryan has mentioned in this post. Switch between two frames in tkinter? Do I use the MVC Controller class as the controller to switch frames? In this case, the Controller would have to be inherited from tk.Tk !!! It would work I guess but is it good practice? To my understanding, the controller is where the main application code goes so this should be okay I guess???

I know this is a BIIIIGGG question, but I am a newbie and I am learning this by myself. I would appreciate any help! Thanks

KeyShoe
  • 84
  • 7

0 Answers0