Some notes on GUI programming

Baby Steps

This is another little idea I have found while researching CoderDojo.

The next step to take should always be as small as possible.

 – Yes, as small as possible.

In other words, when you are writing some code, test whether or not it does what you expect. If something isn’t right, fix it before moving on.

 

GUI Programming

When you create a GUI, you are basically making a window with a grid in it. You decide where to position the widgets (labels, text boxes, buttons etc.) inside the grid and create them accordingly.

For instance:

# gui1.py : demonstrates the grid in a GUI
# Import the Tk gui-making features
from tkinter import *

# create the “root” window – within which
# everything else will be built.
root = Tk()
root.title(“GUI example”)
root.geometry(“50×100”)
app = Frame(root)
app.grid()

# setting up the GUI
# make some buttons
bttn1 = Button(app, text = “0,0”)
bttn1.grid(row = 0, column = 0)
bttn2 = Button(app, text = “0,1”)
bttn2.grid(row = 0, column = 1)
bttn3 = Button(app, text = “0,2”)
bttn3.grid(row = 0, column = 2)
bttn4 = Button(app, text = “1,0”)
bttn4.grid(row = 1, column = 0)
bttn5 = Button(app, text = “1,1”)
bttn5.grid(row = 1, column = 1)
bttn6 = Button(app, text = “1,2”)
bttn6.grid(row = 1, column = 2)
bttn7 = Button(app, text = “2,0”)
bttn7.grid(row = 2, column = 0)
bttn8 = Button(app, text = “2,1”)
bttn8.grid(row = 2, column = 1)
bttn9 = Button(app, text = “2,2”)
bttn9.grid(row = 2, column = 2)

# Start the main-loop (like in a game).
root.mainloop()

Alternative: You can download a program that does this (and a little bit more) here. Have a look at the code and make some changes – (eg, change what is printed when the button is clicked).

This will set up a little grid of buttons that should look like this:

Hopefully this will help you to visualise the way we are creating the GUI for lovegui.py.

If you want, you can cut and paste this code into a new file in Python IDLE and check out how it works and see what happens if you make some changes.

Moved from top of post!

Pair Programming

I’m keen for you to have a go at pair programming this week.  The basic idea is that two people use one computer. You could compare it to flying a plane or driving a rally car.  One person is “driving”, the other is “co-pilot”. The driver will type the code and operate the computer, the co-pilot will stay focused, read the code, spot errors and offer suggestions. As I have mentioned, this approach is used in “real” software development. If you want to read more, you can look at this webpage: Pair Programming.

Pair Programming is also part of the methodology of the CoderDojo movement. You might be interested in reading more about this also. It was started in Ireland by a Sixth-former who was keen to help young people to learn programming.

Skills

We’ve also been discussing the skills that we use when programming. If you are interested in what sort of skills programming develops, you might want to look at this page (from Learn Python The Hard Way). The author does a good job of explaining why excellent literacy skills are important for coders.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: