Dark theme

Key Ideas


The major element, code wise, this week is the brief introduction to GUIs. This is something to explore in your own time, but in way of essentials, the core of a GUI using TkInter is a "root" window and setting it running.

import tkinter

root = tkinter.Tk() # Main window.

w = tkinter.Canvas(root, width=200, height=200)
w.pack() # Layout
w.create_rectangle(0, 0, 200, 200, fill="blue")

tkinter.mainloop() # Wait for interactions.

GUIs are asynchronous code; they don't run to completion instantly, they wait for the user to initiate new code executions. This is known as "Event Based Programming" as the code waits for events to respond to. In Python, this process is based on callbacks: where you pass a function into another, with the expectation that at some point the function will be run. In the below, for example, the menu has the label "Run Model" added, and the function ("command") run is "bound" to it. When the label is pressed, the run function is run.

import tkinter
def run():
pass # Do nothing, but could, for example, run the model.

root = tkinter.Tk()
menu = tkinter.Menu(root)
root.config(menu=menu)
model_menu = tkinter.Menu(menu)
menu.add_cascade(label="Model", menu=model_menu)
model_menu.add_command(label="Run model", command=run)

tkinter.mainloop()

Here's the full code integrating both: gui.py.

GUI design is hard, and needs careful thought and testing. At every stage when designing the GUI, think "is it obvious what this does?"; make all elements as simple as possible. Users learn by trying stuff - they rarely read manuals, so think carefully about what the default behavior of any function should be; hide complex functionality and the options to change defaults in 'Options' menus.

Most of all consult and test. When you think you have a bit users are interested in up and running, test its 'usability'. Sit your users down with the software and get them to play with it. It's useful to set them common tasks to do. See how long they take to do stuff, and what they do that's unexpected. Some companies use mirrored windows.

Remember, users don't make mistakes - it's your fault if you lead them the wrong way!

Overall, the development process generally includes these steps:

Refining user requirements.
Dividing code (e.g. by important high-risk functions first).
Keeping users informed.
Development.
Usability testing.
Bug hunting.
Refactoring.
Alpha and Beta testing.
Releases.
Deciding on the next set of functions to tackle.