Created
May 7, 2018 22:11
-
-
Save ReddyKilowatt/5d0bfedbe9a92a8f50cd948ab51683ee to your computer and use it in GitHub Desktop.
Model View Controller using Tkinter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
import Tkinter as Tk # python 2 | |
except ModuleNotFoundError: | |
import tkinter as Tk # python 3 | |
from model import Model | |
from view import View | |
class Controller: | |
def __init__(self): | |
self.root = Tk.Tk() | |
self.model = Model() | |
self.view = View(self.root, self.model) | |
def run(self): | |
self.root.title("Tkinter MVC example") | |
self.root.deiconify() | |
self.root.mainloop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on May 7, 2018 | |
This is an improvment on the original program posted here: | |
https://sukhbinder.wordpress.com/2014/12/25/an-example-of-model-view-controller-design-pattern-with-tkinter-python/ | |
The original program tightly coupled GUI dependencies into the Controller class, which defeats the whole MVC paradgim. | |
The example below is an improvement on the original program. | |
Each of the Model, View, Controller and SidePanel objects are in their own modules. | |
The program now runs on Python 2 & Python3, without any modifications. | |
""" | |
from controller import Controller | |
if __name__ == '__main__': | |
c = Controller() | |
c.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
import Tkinter as Tk # python 2 | |
except ModuleNotFoundError: | |
import tkinter as Tk # python 3 | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
import Tkinter as Tk # python 2 | |
except ModuleNotFoundError: | |
import tkinter as Tk # python 3 | |
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg | |
from matplotlib.figure import Figure | |
from side_panel import SidePanel | |
class View: | |
def __init__(self, root, model): | |
self.frame = Tk.Frame(root) | |
self.model = model | |
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(root) | |
self.sidepanel.plotBut.bind("<Button>", self.plot) | |
self.sidepanel.clearButton.bind("<Button>", self.clear) | |
self.canvas = FigureCanvasTkAgg(self.fig, master=self.frame) | |
self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) | |
self.canvas.show() | |
def clear(self, event): | |
self.ax0.clear() | |
self.fig.canvas.draw() | |
def plot(self, event): | |
self.model.calculate() | |
self.ax0.clear() | |
self.ax0.contourf(self.model.res["x"], self.model.res["y"], self.model.res["z"]) | |
self.fig.canvas.draw() |
Nice and simple example, but line#27 of view.py needs to be changed to self.canvas.draw()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ligne 27 self.canvas.draw()