1
\$\begingroup\$

Just starting to get the idea of MVC with tkinter. I wrote this simple program and I'm looking for feedback. There are no errors, but I don't know if I set it up right or not.

import tkinter as tk


class Model():

    def __init__(self):
      self.list = ["email", "sms", 'voice']

class View(tk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self, parent, bg="yellow", bd=2, 
           relief=tk.RIDGE)
        self.parent = parent
        self.pack()

        self.labelVariable=tk.StringVar()
        self.output=tk.Label(self, textvariable=self.labelVariable, 
             bg="orange", fg="white")
        self.output.pack()

        self.hello = tk.Button(self, text="Show", command= 
             self.controller.hello_Button_Pressed,  bd=2,            
             relief=tk.RIDGE)
        self.hello.pack(side="left")


class Controller():
    def __init__(self):
        self.root = tk.Tk()
        self.model = Model()
        self.view = View(self.root, self)

        self.root.title("MVC example")
        self.root.geometry("250x350")
        self.root.config(background="LightBlue4")
        self.root.mainloop()

    def hello_Button_Pressed(self):
        self.view.labelVariable.set(self.model.list)

if __name__ == '__main__':
    c = Controller()
\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Review. Can you tell us more about what the code is supposed to do? Does the code have a purpose? I understand you want to learn about MVC, that's your goal. What's the goal of the code? \$\endgroup\$ – Mast Dec 26 '19 at 8:57
  • \$\begingroup\$ Hi @Mast, thanks for responding. This code is just to print the list onto the label. Nothing else. I could do this code without MVC, but to elevate my learning, i wanted to organize it with MVC. If this is correct, without much feedback, I will begin to rewrite most of my other programs into MVC, so any thoughts are welcomed. thanks again. \$\endgroup\$ – fishtang Dec 26 '19 at 9:19
  • \$\begingroup\$ Please edit the question to add the purpose of the code there. In the comment it is not visible enough. \$\endgroup\$ – Roland Illig Feb 5 '20 at 1:09
1
\$\begingroup\$

The code looks like it is set up fine for MVC. I'm not an expert but I have enogh experience to tell that it's fine.

\$\endgroup\$
2
  • \$\begingroup\$ thanks @Cruise Lickerman. it took me a while to understand the set up of MVC, and now I can start use it. \$\endgroup\$ – fishtang Jan 6 '20 at 20:04
  • \$\begingroup\$ Yeah took me a while also \$\endgroup\$ – Cruise Lickerman Jan 6 '20 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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