how to separate view and controller in python tkinter? how to separate view and controller in python tkinter? tkinter tkinter

how to separate view and controller in python tkinter?


the lambda approach problem was exactly as above which is now resolved by using the pack in a new line. it seems more beautiful, here is the sample using lambda which is working fine:

1.view.py

from tkinter import *from controller import *class View():    def __init__(self,parent):        button=Button(parent,text='click me')        button.config(command=lambda : callback(button))        button.pack()root=Tk()app=View(root)root.mainloop()

2.controller.py

def callback(button):    button.config(text='you clicked me!')    print('Hello World!')

using this approach we can move all functionality away from UI and make it clean and readable.


In view.py you are calling:

self.button=Button(parent,text='click me').pack()

The pack function doesn't return the Button object that you want to assign to self.button, which causes the AttributeError later on. You should do:

self.button = Button(parent, text='click me')self.button.pack()