Dark theme

Build your own


In this practical, we'll finish off our model by giving it a Graphical User Interface (GUI), and load some data into it from the web.


First up, let's sort out a GUI. We'll build our GUI such that the model displays in a window with a menu to run the model.

The first thing to say is that matplotlib has different (as it calls them) "backends". These allow it to render its graphics in different ways depending on the output: for example, as a PDF. We need to change the backend to render as associated with TkInter. To do this, add the following to the top of your file. Note that both lines need to come before any other matplotlib imports.

import matplotlib
matplotlib.use('TkAgg')

Note that if you are running this in Spyder, you may also need to tell that to use TkInter. Make sure you're using an IPython prompt (prompt looks like "[1]") rather than a standard Python prompt (prompt looks like ">>>"), then go to the Tools menu, the Preferences menuitem, and pick IPython console from the list on the left, the Graphics tab; and adjust the Backend drop down list to Tkinter, then save your files and restart Spyder.

You can read more about backends in the matplotlib usage FAQ; indeed, it's a good starting point for learning how to properly use matplotlib more generally.


Next, let's add a function that will run our model. We'll connect this to our menu such that when the menu is clicked, this function will run, in line with the event based programming model. Note that what you put in the line to run the model will depend what you did with the animation; you may need to adjust the line to match whatever you have:

def run():
    animation = matplotlib.animation.FuncAnimation(fig, update, frames=gen_function, repeat=False)
    canvas.draw()

Note also that whereas we did have matplotlib.plot.show() we've replaced this and put in canvas.draw(). We'll come to canvas in a second, but for now make sure matplotlib.plot.show() has been deleted from you code (or you'll get two windows).


Next, add in the following code. This builds the main window ("root"); sets its title, then creates and lays out a matplotlib canvas embedded within our window and associated with fig, our matplotlib figure.

root = tkinter.Tk()
root.wm_title("Model")
canvas = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(fig, master=root)
canvas._tkcanvas.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

Immediately below, add the code from the lecture to make a menu, and associate the menu with the function run, above. Which bits of the code do you need to replicate, give we've already added in some code to make the window?


Lastly, at the very bottom of the file, make sure you have the line:

tkinter.mainloop()

This line sets the GUI waiting for events. Once you've added this in, debug and run your code. Once you have that working, go on to part two, where we'll add some data from the web.


  1. This page
  2. Web scraping <-- next
  3. Initializing the model