element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Step by Step Build Trick or Trivia Halloween Candy Dispenser #3- VNC, Problem of Tkinter & RPi.GPIO not working simultaneously
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: cmelement14
  • Date Created: 25 Oct 2015 3:59 AM Date Created
  • Views 793 views
  • Likes 2 likes
  • Comments 4 comments
  • raspberry_pi_projects
  • pumpkinpi2015
Related
Recommended

Step by Step Build Trick or Trivia Halloween Candy Dispenser #3- VNC, Problem of Tkinter & RPi.GPIO not working simultaneously

cmelement14
cmelement14
25 Oct 2015

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/Tkinter.py", line 1712, in __init__
    self.tk = _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 https://www.raspberrypi.org/documentation/remote-access/vnc/, 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 (https://www.realvnc.com/download/viewer/). Now I am ready to use Tkinter for Python GUI programming.

image

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.setwarnings(False)
GPIO.setmode(GPIO.BCM)
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.


image

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.


  • Sign in to reply

Top Comments

  • CharlesGantt
    CharlesGantt over 10 years ago +1
    Correct, you can not remotely start a TKinter session. You must do it from the device that is executing the code and from within a GUI. Are you running Python 2? I tested the code in PyCharm and on another…
  • CharlesGantt
    CharlesGantt over 10 years ago in reply to cmelement14 +1
    Tkinter can most definitely be accessed with root privileges. I do it all the time, and in this project as well. The issue is that Tkinter must be ran from within the GUI on the machine that is executing…
  • cmelement14
    cmelement14 over 10 years ago in reply to CharlesGantt

    Thanks Charles. I think you are right. The problem must come from the remote access.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • CharlesGantt
    CharlesGantt over 10 years ago in reply to cmelement14

    Tkinter can most definitely be accessed with root privileges. I do it all the time, and in this project as well. The issue is that Tkinter must be ran from within the GUI on the machine that is executing the code. It can not be viewed from a remote machine.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • cmelement14
    cmelement14 over 10 years ago in reply to CharlesGantt

    Thanks for your reply. Yes, I am running Python 2.7. I use remote desktop software VNC viewer to interact with Pi2 since I don't have a keyboard to attach.

     

    The GPIO control requires root privilege while Tkinter cannot be access with root privilege. That's why I cannot run the full program as one entity.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • CharlesGantt
    CharlesGantt over 10 years ago

    Correct, you can not remotely start a TKinter session. You must do it from the device that is executing the code and from within a GUI. Are you running Python 2? I tested the code in PyCharm and on another pi connected to a display and running the GUI and it worked just fine in both instances.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube