Blog Pages

1/5/18

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( 'tkinter.py')
window.mainloop()

My code looks like this:



4. File - Save as "tkinter1.py" 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 tkinter1.py 
(ignore the "scrot line of code" in image below)



7. Now type in your terminal window:
./tkinter.py



8. RESULTS:
If all goes well you should end up with this:




Common Errors:
(1)
#! /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
./tkinter1.py: line 8: syntax error near unexpected token `('
./tkinter1.py: line 8: `window = Tk()'


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


(3)
OMITTING "*" in from Tkinter import*
from Tkinter import
 File "./tkinter1.py", line 6
    from Tkinter import
                      ^
SyntaxError: invalid syntax


(4)
OMITTING "from" in from Tkinter import*
Tkinter import*
 File "./tkinter1.py", line 6
    Tkinter import*
                 ^
SyntaxError: invalid syntax

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

NameError: name 'windowmainloop' is not defined

Next Up: Pre-sizing the window


Thanks for reading,
Trenton


No comments:

Post a Comment