Question: There is a sample Python program that uses Python's tkinter and maplotlib packages to illustrate the workings of the model - view - controller pattern.

There is a sample Python program that uses Python's tkinter and maplotlib packages to illustrate the workings of the model-view-controller pattern.See if you can get this program running on your computer.Provide a short paragraph (in comments in the source code) explaining the relationship between each of the three pattern components.
import tkinter as Tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
class Model():
def __init__(self):
self.xpoint =200
self.ypoint =200
self.res = None
def calculate(self):
x, y = np.meshgrid(np.linspace(-5,5, self.xpoint),
np.linspace(-5,5, self.ypoint))
z = np.cos(x**2*y**3)
self.res ={"x": x,"y": y,"z": z}
class View():
def __init__(self, master):
self.frame = Tk.Frame(master)
self.fig = Figure(figsize=(7.5,4), dpi=80)
self.ax0= self.fig.add_axes(
(0.05,.05,.90,.90), facecolor=(.75,.75,.75), frameon=False)
self.frame.pack(side=Tk.LEFT, fill=Tk.BOTH, expand=1)
self.sidepanel = SidePanel(master)
self.canvas = FigureCanvasTkAgg(self.fig, master=self.frame)
self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.canvas.draw()
class SidePanel():
def __init__(self, root):
self.frame2= Tk.Frame(root)
self.frame2.pack(side=Tk.LEFT, fill=Tk.BOTH, expand=1)
self.plotBut = Tk.Button(self.frame2, text="Plot ")
self.plotBut.pack(side="top", fill=Tk.BOTH)
self.clearButton = Tk.Button(self.frame2, text="Clear")
self.clearButton.pack(side="top", fill=Tk.BOTH)
class Controller():
def __init__(self):
self.root = Tk.Tk()
self.model = Model()
self.view = View(self.root)
self.view.sidepanel.plotBut.bind("", self.my_plot)
self.view.sidepanel.clearButton.bind("", self.clear)
def run(self):
self.root.title("Tkinter MVC example")
self.root.deiconify()
self.root.mainloop()
def clear(self, event):
self.view.ax0.clear()
self.view.fig.canvas.draw()
def my_plot(self, event):
self.model.calculate()
self.view.ax0.clear()
self.view.ax0.contourf(
self.model.res["x"], self.model.res["y"], self.model.res["z"])
self.view.fig.canvas.draw()
if __name__=='__main__':
c = Controller()
c.run()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!