MVC Finished AND Login Generator Begin
This commit is contained in:
parent
f21ccb082d
commit
1246e905d3
|
@ -2949,7 +2949,7 @@ class MainWindow(Frame):
|
|||
elif number < MyNumber:
|
||||
MyLabel.config(text="My number is bigger")
|
||||
else:
|
||||
MyLabel.cionfig(text="You WIN!")
|
||||
MyLabel.config(text="You WIN!")
|
||||
else:
|
||||
MyLabel.config(text="I need numbers!")
|
||||
|
||||
|
@ -2980,7 +2980,6 @@ We'll add the GUI view in a bit.
|
|||
```python
|
||||
from tkinter import *
|
||||
|
||||
|
||||
class ConsoleView(object):
|
||||
"""A view for console."""
|
||||
|
||||
|
@ -3029,14 +3028,16 @@ class Controller(object):
|
|||
if self.view is ConsoleView:
|
||||
self.view = self.view()
|
||||
self._run_console_view()
|
||||
elif self.view is WxView:
|
||||
app = wx.App()
|
||||
elif self.view is TkinterView:
|
||||
root = Tk()
|
||||
root.title("Task Manager")
|
||||
root.geometry("500x300")
|
||||
self.view = self.view()
|
||||
self.view._set_controller(self)
|
||||
app.MainLoop()
|
||||
root.mainloop()
|
||||
|
||||
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)
|
||||
self.view.show_task(task)
|
||||
|
||||
|
@ -3055,7 +3056,6 @@ class Controller(object):
|
|||
|
||||
if __name__ == "__main__":
|
||||
view = ConsoleView
|
||||
# view = WxView
|
||||
app = Controller(view)
|
||||
app.run()
|
||||
```
|
||||
|
@ -3063,8 +3063,7 @@ if __name__ == "__main__":
|
|||
And now with the implemented `TkinterView` class.
|
||||
|
||||
```python
|
||||
import wx
|
||||
|
||||
from tkinter import *
|
||||
|
||||
class ConsoleView(object):
|
||||
"""A view for console."""
|
||||
|
@ -3084,38 +3083,45 @@ class ConsoleView(object):
|
|||
print("error: {}".format(msg))
|
||||
|
||||
|
||||
class WxView(wx.Dialog):
|
||||
class TkinterView(Frame):
|
||||
"""A view using a wx.Dialog window"""
|
||||
|
||||
def __init__(self):
|
||||
wx.Dialog.__init__(self, None, title="Task Manager")
|
||||
self.panel = wx.Panel(self)
|
||||
self.box = wx.BoxSizer(wx.VERTICAL)
|
||||
self.task = wx.StaticText(self.panel, label="your task")
|
||||
self.idx = wx.SpinCtrl(self.panel)
|
||||
self.button = wx.Button(self.panel, label="submit")
|
||||
self.button.Bind(wx.EVT_BUTTON, self.select_task)
|
||||
self.box.Add(self.task, 0, wx.EXPAND, 1)
|
||||
self.box.Add(self.idx, 0, wx.EXPAND, 1)
|
||||
self.box.Add(self.button, 0, wx.EXPAND, 1)
|
||||
self.panel.SetSizer(self.box)
|
||||
self.Show()
|
||||
Frame.__init__(self, master=None)
|
||||
#Panel
|
||||
self.panel = PanedWindow(self, bg="green")
|
||||
self.panel.pack(fill=BOTH, expand=True)
|
||||
|
||||
#Task Label
|
||||
self.task = Label(self.panel, text="your task")
|
||||
self.task.pack(expand=True)
|
||||
|
||||
#SpinBox
|
||||
self.idx = Spinbox(self.panel, from_=0, to=2, wrap=True )
|
||||
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):
|
||||
"""Set the controller so the view can communicate it's requests to it
|
||||
and update it's values too."""
|
||||
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
|
||||
the controller."""
|
||||
idx = self.idx.GetValue()
|
||||
idx = self.idx.get()
|
||||
self.controller.get_task_from_model(idx)
|
||||
|
||||
def show_task(self, task):
|
||||
"""Updates the visual label in the view with the task. This method is
|
||||
called from the controller."""
|
||||
self.task.SetLabel(task)
|
||||
self.task.config(text=task)
|
||||
|
||||
|
||||
class Model(object):
|
||||
"""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
|
||||
in the view (via the controller)."""
|
||||
try:
|
||||
task = Model.db[idx]
|
||||
task = Model.db[int(idx)]
|
||||
except IndexError:
|
||||
task = "task with {} not found!".format(idx)
|
||||
return task
|
||||
|
@ -3139,23 +3145,25 @@ class Controller(object):
|
|||
|
||||
def __init__(self, view):
|
||||
self.model = Model()
|
||||
self.view = view
|
||||
self.view = view
|
||||
|
||||
def run(self):
|
||||
"""The controller's main function. Depending on what type of view is
|
||||
"""The controller's main function. Depending on what type of view is
|
||||
selected, a different event loop is setup. Do note that the ConsoleView
|
||||
is not a real event loop, just a basic flow of action."""
|
||||
if self.view is ConsoleView:
|
||||
self.view = self.view()
|
||||
self._run_console_view()
|
||||
elif self.view is WxView:
|
||||
app = wx.App()
|
||||
elif self.view is TkinterView:
|
||||
root = Tk()
|
||||
root.title("Task Manager")
|
||||
root.geometry("500x300")
|
||||
self.view = self.view()
|
||||
self.view._set_controller(self)
|
||||
app.MainLoop()
|
||||
root.mainloop()
|
||||
|
||||
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)
|
||||
self.view.show_task(task)
|
||||
|
||||
|
@ -3173,9 +3181,10 @@ class Controller(object):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
view = WxView
|
||||
view = TkinterView
|
||||
app = Controller(view)
|
||||
app.run()
|
||||
|
||||
```
|
||||
|
||||
For a GUI only login generator an implementation without MVC could look a bit like this.
|
||||
|
|
Loading…
Reference in New Issue