Blog Pages


Raspberry Pi 3 GUI Part 1 - Basic Window using TKinter

My son wanted to learn Python game programming so I bought him a raspberry pi 3. I've been working on getting the pi 3 to communicate to the GPIO pins on the PCB with plans to control audio hardware.

In order to communicate graphically with the outside world we need to create a basic window. Here's how using Python 3 and Tkinter:

First - Here's the current version I am working with on the pi3.

1. Goto Raspberry icon - Programming - Python 3 (IDLE)
and a new Python window opens up and looks like this

2. Goto FILE - NEW to open a clean page that looks like this:

3. Type the following code that is in BOLD only:

#! /usr/bin/env python 
from Tkinter import*
window.title( '')

My code looks like this:

4. File - Save as "" on your Desktop

5. Goto your Terminal Window - Note: (ignore the "scrot code")
SCROT is the screen capture application I am using

5. Change Directory to your Desktop using "cd Desktop"
Again: (ignore the "scrot line of code" in image below)

6. Type the following code in BOLD only into your terminal editor:
chmod 755 
(ignore the "scrot line of code" in image below)

7. Now type in your terminal window:

If all goes well you should end up with this:

Common Errors:
#! /usr/bin/env python

This line of code must be the very first line in the program code otherwise you will get this error message in your terminal window:
from: can't read /var/mail/Tkinter
./ line 8: syntax error near unexpected token `('
./ line 8: `window = Tk()'

LOWER CASE "T" in Tk()
window = tk()
Traceback (most recent call last):
  File "./", line 8, in
    window = tk()
NameError: name 'tk' is not defined

OMITTING "*" in from Tkinter import*
from Tkinter import
 File "./", line 6
    from Tkinter import
SyntaxError: invalid syntax

OMITTING "from" in from Tkinter import*
Tkinter import*
 File "./", line 6
    Tkinter import*
SyntaxError: invalid syntax

OMITTING "." in window.mainloop()
Traceback (most recent call last):
  File "./", line 11, in

NameError: name 'windowmainloop' is not defined

Next Up: Pre-sizing the window

Thanks for reading,

No comments:

Post a Comment