Thursday 21 June 2012

Making a start

We want to run some code using Tkinter, this is not installed on R-Pi by default, but it is easy to add if your R-Pi is connected to the internet. Login as normal and at the command prompt type

sudo apt-get install python-tk

This will install the extension to python.

To start creating a program that runs on R-Pi in a GUI way, we first have to enter that GUI environment. When you power up the R-Pi you get prompted to login and then get the Linux command prompt. If you type startx at that prompt the GUI environment of X-Windows will start.

We need to create the code of a program and working in the GUI environment means we can edit it and test it easily. There are various editors available and one that is available in Debian is Geany. What we are going to write is a Python program and Geany helps a little by understanding the way Python should look. To get started we need to create an folder to store our program in. If you click the File Manager on the bar near the bottom left. Hover the mouse over the icons for a hint about what they are. It will open up in the home folder for the user you are logged in as. Right-click in the window and create a new folder called python. Double click on that to open it and create another folder called rye. Notice that I'm using lower-case folder names. You can use mixed or upper-case names, but file and folder names in Linux are case sensitive, so folder Python is different from folder python. Open the folder rye, double-click and this time create a blank file called start.py.

If you right-click this blank file you can look at its properties from the pop-up menu. There is something we need to change. On the Permissions tab you need to check the setting 'Make the file executable'. This will allow us to run our typed code as a program.

If you right-click the file and select Geany from the menu, Geany should start with the empty file in it. type the following code and save it.

#!/usr/bin/env python

from Tkinter import *

def RyeSetup():

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

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


if __name__ == "__main__":
    ryewin=Tk()
    RyeSetup()
    ryewin.mainloop()

This is a simple GUI program in Python that doesn't do much, but does give us a start. The indenting is vital with python. Use the tab key to create the indents carefully as shown. The case and spelling of everything here is important too. Every part must be exactly as you see it above.

To run this program, go back to the File Manager window and from the Tools menu select 'Open current folder in terminal'. We are going to run the program from a terminal session, because any errors will display on the terminal. Later we can add debugging text to display on the terminal too. To run the program type ./start.py . You need to put the ./ in front of the filename because Linux only looks in its PATH to find programs and the folder we created will not be on the PATH. You can redo a previous command in the terminal by pressing up arrow. You can edit the line by inserting or deleting characters before pressing return. If you have typed everything as shown you should see a grey window appear with the title bar at the top displaying Raspberry Rye. You should be able to move the window, resize it and close it, which is not bad for a few lines of code.

That is enough for now. In the next post I'll explain what the code does and we will start to display something more useful. If you use this or have any problems please add a comment.

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.