Saturday, 23 June 2012

Making a start (2)

The last post created a small piece of code, which you can download from here. It displays a simple window, but it needs explaining. Here's the code again, but with some more explanation:

#!/usr/bin/env python 1

from Tkinter import * 2

def RyeSetup(): 3

    # GUI initialisation 4
    ryewin.title('Raspberry Rye') 5
    ryewin.grid() 6

    # create the canvas for the game 7
    cnvs = Canvas(ryewin, height=300, width=300) 8
    cnvs.grid(column=0, row=0) 9

if __name__ == "__main__": 10
    ryewin=Tk() 11
    RyeSetup() 12
    ryewin.mainloop() 13

The first line 1 is something that often appears in Linux script. It's know as a shebang. Linux uses it to find the interpreter that runs the script, in this case Python. All of your Python scripts should begin with this.

Line 2 is how Python knows about extensions to the scripting language. The import statement looks for external modules of code and makes them available to the program. An external module (Tkinter in this case) can have many functions in it and you may choose to only import some of them by name or, as in this case, import them all with '*'. We'll use some this later.

Line 3 defines the beginning of a function called RyeSetup. The function has no parameters but we still need the parentheses () to show this. The function is marked with a colon to show where the function starts. Everything in the function must be indented by the same amount to group the statements together. Lines 4 to 8 are in the function called RyeSetup(). Nothing in the function will get run yet, it is just being defined.

Line 4 is a comment. Everything after a # on a line is ignored as a comment. Line 7 is a comment too.

Line 5 sets the title of the window and line 6 initialises a grid in the window, but what window? Well it is created elsewhere, remember this code is being defined, not run yet. The grid is a very important part of the way the things on the window look and behave, but more of that later.

Line 8 creates a Canvas. A canvas is something the program can draw on, which is where we will draw the elements of the game in later stages. It has a single parameter we must pass  and two optional parameters. The required parameter is the window that it belongs to - your program could display multiple windows. We pass ryewin - this mysterious window we haven't yet defines. We also pass height and width, both are in pixels and define the size of the canvas on the screen. We call the Canvas cnvs. Canvas is one of the things that we can use because with imported the Tkinter module.

Next, in line 9, we add the canvas to the grid for the window in row 0, column 0. Later when we define a some more things on the window we will position them using this grid.

That was the end of the ryewin() function. Now we get to the program that gets run when the script is started. Line 10 is a horrible looking line that is fairly common in Python programs. It is used to find out if a piece of code is being run or loaded as a module. Basically it is an if statement and if it is True (and it will be) the indented code that follows the colon will be run. I could have missed this line out - the program would generally work, but you will often see it so I left it in. The indents are just like the ones needed for the function - they

Line 11 creates the window we are going to use. It looks simple, and indeed it is simple to use, but it does a lot of work behind the scenes. It is another function that is included in the Tkinter module. We create an object called ryewin which is returned from the function Tk(). This we will use as the reference to our window. We have created it outside of any functions so it is a global variable and therefore accessible from anywhere in the program. Creating and using global variables is often frowned on, but a small number of key ones works well.

Now we have created our window we need to set up its looks and the things in it, so we call the function we defined earlier, Ryesetup(), in line 12 which we now know will add a title and a grid and add a canvas to the grid.

Lastly in line 13 we call a function mainloop(). Strictly this is a method of the ryewin object, but what does it do? Well it processes events for the window. The way GUI programs work is called event-driven. The mainloop() method displays the window and loops around waiting for any events to happen. We haven't defined any yet, so only a few built-in ones will work. We can move the window around the screen, minimise it, change its size and finally close it. All of these create events that have default responses. We can add our own events to respond to mouse events and keyboard events and other things too. We'll make use of some of these another time.

Getting going with GUI programming is a little slow, but the bulk of the work is done for you with Tkinter and now progress can be a bit more visible, as we'll see next time.

No comments:

Post a Comment

Thank you for taking the time to add a comment.

I have decided, regrettably, to review comments before they are published to ensure that offensive or inappropriate comments do not get shown.

Genuine comments are always welcome and will be published as quickly as possible.