I have read from here(Tkinter! Understanding how to switch frames) about self.controller = controller. I'm only new to python and also to tkinter, I just want to ask if the 'controller' does always refer to the main program? If not, Can you please give an example. Thanks
Asked
Active
Viewed 1,057 times
1 Answers
0
No, the controller does not always refer to the main program. It can refer to any object you want. In the example you cite, using the application was the simplest, shortest solution.
For example:
class Model(...): ...
class Controller(...): ...
class View(tk.Tk):
def __init__(self, controller):
self.controller = controller
tk.Tk.__init__(self)
...
...
class App(...):
def __init__(...):
...
model = Model(...)
controller = Controller(..., model=self.model)
view = View(..., controller=self.controller)
controller.add_view(view)
...
,,,
if __name__ == "__main__":
app = App()

Bryan Oakley
- 370,779
- 53
- 539
- 685
-
Im still confused.If not always the main program, how does python know what "self.controller = controller" is referring to? How does python know that its another object,not the main program? – marco alexis Mar 18 '16 at 22:56
-
@marcoalexis: how does python know? You tell it. If you pass in some other object, `self.controller` is set to that object. You can pass whatever you want into the parameter for `controller` – Bryan Oakley Mar 18 '16 at 23:08
-
about the code that I have cited, I think thats originally from you?The self.controller = controller directly refers to the main program even though It is not defined in the main program because one else can be referred to right? and to your other examples in your answer a while ago, It can refer to any other objects because there are many classes declared and you specifically set it to a particular object, right? – marco alexis Mar 19 '16 at 01:51
-
@marcoalexis: stackoverflow isn't designed for extended discussions. I can't answer those questions in a comment field. – Bryan Oakley Mar 19 '16 at 03:29