MVC Finished AND Login Generator Begin

This commit is contained in:
Yousri 2022-04-20 16:40:44 +02:00
parent f21ccb082d
commit 1246e905d3
1 changed files with 42 additions and 33 deletions

View File

@ -2949,7 +2949,7 @@ class MainWindow(Frame):
elif number < MyNumber: elif number < MyNumber:
MyLabel.config(text="My number is bigger") MyLabel.config(text="My number is bigger")
else: else:
MyLabel.cionfig(text="You WIN!") MyLabel.config(text="You WIN!")
else: else:
MyLabel.config(text="I need numbers!") MyLabel.config(text="I need numbers!")
@ -2980,7 +2980,6 @@ We'll add the GUI view in a bit.
```python ```python
from tkinter import * from tkinter import *
class ConsoleView(object): class ConsoleView(object):
"""A view for console.""" """A view for console."""
@ -3029,14 +3028,16 @@ class Controller(object):
if self.view is ConsoleView: if self.view is ConsoleView:
self.view = self.view() self.view = self.view()
self._run_console_view() self._run_console_view()
elif self.view is WxView: elif self.view is TkinterView:
app = wx.App() root = Tk()
root.title("Task Manager")
root.geometry("500x300")
self.view = self.view() self.view = self.view()
self.view._set_controller(self) self.view._set_controller(self)
app.MainLoop() root.mainloop()
def get_task_from_model(self, idx): def get_task_from_model(self, idx):
"""Needed for the WxView to communicate with the controller.""" """Needed for the TkinterView to communicate with the controller."""
task = self.model.get_task(idx) task = self.model.get_task(idx)
self.view.show_task(task) self.view.show_task(task)
@ -3055,7 +3056,6 @@ class Controller(object):
if __name__ == "__main__": if __name__ == "__main__":
view = ConsoleView view = ConsoleView
# view = WxView
app = Controller(view) app = Controller(view)
app.run() app.run()
``` ```
@ -3063,8 +3063,7 @@ if __name__ == "__main__":
And now with the implemented `TkinterView` class. And now with the implemented `TkinterView` class.
```python ```python
import wx from tkinter import *
class ConsoleView(object): class ConsoleView(object):
"""A view for console.""" """A view for console."""
@ -3084,38 +3083,45 @@ class ConsoleView(object):
print("error: {}".format(msg)) print("error: {}".format(msg))
class WxView(wx.Dialog): class TkinterView(Frame):
"""A view using a wx.Dialog window""" """A view using a wx.Dialog window"""
def __init__(self): def __init__(self):
wx.Dialog.__init__(self, None, title="Task Manager") Frame.__init__(self, master=None)
self.panel = wx.Panel(self) #Panel
self.box = wx.BoxSizer(wx.VERTICAL) self.panel = PanedWindow(self, bg="green")
self.task = wx.StaticText(self.panel, label="your task") self.panel.pack(fill=BOTH, expand=True)
self.idx = wx.SpinCtrl(self.panel)
self.button = wx.Button(self.panel, label="submit") #Task Label
self.button.Bind(wx.EVT_BUTTON, self.select_task) self.task = Label(self.panel, text="your task")
self.box.Add(self.task, 0, wx.EXPAND, 1) self.task.pack(expand=True)
self.box.Add(self.idx, 0, wx.EXPAND, 1)
self.box.Add(self.button, 0, wx.EXPAND, 1) #SpinBox
self.panel.SetSizer(self.box) self.idx = Spinbox(self.panel, from_=0, to=2, wrap=True )
self.Show() self.idx.pack(side= TOP)
#Button
self.button = Button(self.panel, text="submit", command=lambda : self.select_task())
self.button.pack(ipadx=60, ipady=30)
self.pack(fill=BOTH, expand=True)
def _set_controller(self, controller): def _set_controller(self, controller):
"""Set the controller so the view can communicate it's requests to it """Set the controller so the view can communicate it's requests to it
and update it's values too.""" and update it's values too."""
self.controller = controller self.controller = controller
def select_task(self, event): def select_task(self):
"""Gets the index to look up in the model and submits the request to """Gets the index to look up in the model and submits the request to
the controller.""" the controller."""
idx = self.idx.GetValue() idx = self.idx.get()
self.controller.get_task_from_model(idx) self.controller.get_task_from_model(idx)
def show_task(self, task): def show_task(self, task):
"""Updates the visual label in the view with the task. This method is """Updates the visual label in the view with the task. This method is
called from the controller.""" called from the controller."""
self.task.SetLabel(task) self.task.config(text=task)
class Model(object): class Model(object):
"""The model houses add data and should implement all methods related to """The model houses add data and should implement all methods related to
@ -3128,7 +3134,7 @@ class Model(object):
If no task is found, it returns an error message that will be displayed If no task is found, it returns an error message that will be displayed
in the view (via the controller).""" in the view (via the controller)."""
try: try:
task = Model.db[idx] task = Model.db[int(idx)]
except IndexError: except IndexError:
task = "task with {} not found!".format(idx) task = "task with {} not found!".format(idx)
return task return task
@ -3148,14 +3154,16 @@ class Controller(object):
if self.view is ConsoleView: if self.view is ConsoleView:
self.view = self.view() self.view = self.view()
self._run_console_view() self._run_console_view()
elif self.view is WxView: elif self.view is TkinterView:
app = wx.App() root = Tk()
root.title("Task Manager")
root.geometry("500x300")
self.view = self.view() self.view = self.view()
self.view._set_controller(self) self.view._set_controller(self)
app.MainLoop() root.mainloop()
def get_task_from_model(self, idx): def get_task_from_model(self, idx):
"""Needed for the WxView to communicate with the controller.""" """Needed for the TkinterView to communicate with the controller."""
task = self.model.get_task(idx) task = self.model.get_task(idx)
self.view.show_task(task) self.view.show_task(task)
@ -3173,9 +3181,10 @@ class Controller(object):
if __name__ == "__main__": if __name__ == "__main__":
view = WxView view = TkinterView
app = Controller(view) app = Controller(view)
app.run() app.run()
``` ```
For a GUI only login generator an implementation without MVC could look a bit like this. For a GUI only login generator an implementation without MVC could look a bit like this.