Following my previous blog Step by Step Build Trick or Trivia Halloween Candy Dispenser #2, I will start with the Python GUI in this blog.


I got the LCD display work and I can SSH to my Pi 2 in the last two blogs, so I decided to try out Python GUI code in the SSH session. However, it didn't work. I thought I could create a widget on the LCD by typing Python code root=TK() in SSH session. Obviously, I was wrong.

pi@candydispenser1 ~ $ 
pi@candydispenser1 ~ $ python
Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Tkinter import *
>>> root=Tk()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/lib-tk/", line 1712, in __init__ = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

It seems I have to use GUI desktop to get the widget shown up. However, I don't have a keyboard to connect my Pi, so I decided to use remote desktop. I used VNC. On the Pi 2, I installed TightVNC server and on my Mac computer, I installed VNC viewer. As described in, I used the following command to install VNC server on Pi 2.

sudo apt-get install tightvncserver

However, I didn't install xtightvncviewer because it isn't free. Instead, I installed VNC Viewer for Mac ( Now I am ready to use Tkinter for Python GUI programming.


Next, I will check out the control of Pi's GPIO pins. I typed the following statements in Python

import RPi.GPIO as GPIO
GPIO.setup(26, GPIO.OUT)

and it got runtime error and we need root access. Then I restarted Python interpreter with root privilege: sudo python and it worked. No runtime error anymore.

Screen Shot 2015-10-24 at 10.00.56 PM.png

Now, I am ready to follow Charles_Gantt's blog Trick or Trivia Halloween Candy Dispenser #002 - Building The Trivia Interface.

I created the file containing the whole program(Charles Gantt's code in Put it all together section in his blog#2 mentioned above), then ran the program as shown below. Unfortunately, it gave me errors no matter I ran Python interpreter in privilege mode or not. I posted my question in the comments of Charles Gantt's blog and hopefully he can answer my questions soon.

I will continue this series of blogs and stay tune for the next oneStep by Step Build Trick or Trivia Halloween Candy Dispenser #4 - LED blink test.