Page 1 of 1

help to start with tkinter

Posted: Fri Nov 04, 2016 7:52 pm
by Canedje
I try to start to make a simpel GUI with tinker.
I try to do the most simpel start:

Code: Select all

from tkinter import *
# if you are working under Python 3, comment the previous line and comment out the following line
#from tkinter import *

root = Tk()

w = Label(root, text="Hello Tkinter!")
w.pack()

root.mainloop()
After running the programm: sudo python3 gui.py
I do receive te next error:

Code: Select all

Traceback (most recent call last):
  File "gui.py", line 5, in <module>
    root = Tk()
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1854, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
What am I doing wrong?

Re: help to start with tkinter

Posted: Fri Nov 04, 2016 9:11 pm
by B.Goode
At a guess, I'd say you are trying to run the script that will provide your gui from a command line (shell) environment that does not have access to an X-windows display.

Invoking the script via sudo may be a contributing factor.

Re: help to start with tkinter

Posted: Fri Nov 04, 2016 9:25 pm
by Canedje
Thanks for the answer.
I do run it by "sudo python3 gui.py".
This will generate this error

Do I need to install something for an X-window display surrounding?

Re: help to start with tkinter

Posted: Fri Nov 04, 2016 10:21 pm
by scruss
don't use sudo. It's what's causing the DISPLAY problem.

Note that Python 3 calls it tkinter, but Python calls it Tkinter.

Re: help to start with tkinter

Posted: Fri Nov 04, 2016 10:27 pm
by Canedje
Thanks for the answer.

I tried all:
running:
sudo python3 gui.py
python3 gui.py
sudo python gui.py
python gui.py

Changing in the code Tkinter in tkinter depending onde python version.
All the same result:

Code: Select all

Traceback (most recent call last):
  File "gui.py", line 5, in <module>
    root = Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1813, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

I do read on the internet that the RPI cannot find the Display device.

It can be fixed bj making a ssh -X connection/
I do use putty for my ssh connection. How do i make a ssh -X connection with Putty?

Re: help to start with tkinter

Posted: Fri Nov 04, 2016 10:48 pm
by DougieLawson
There was a "Get started with TKinter" article in MagPi issue #29 page 44.
https://www.raspberrypi.org/magpi-issues/MagPi29.pdf

Code can be downloaded from https://github.com/zleap/magpi-resources

Re: help to start with tkinter

Posted: Fri Nov 04, 2016 10:57 pm
by Canedje
Thanks.

I do more or less what is mentioned over there:

I did read something on internet:

that the error is because that the RPI cannot find the Display device.

It can be fixed by making a ssh -X connection.
I do use putty for my ssh connection. How do i make a ssh -X connection with Putty?

Re: help to start with tkinter

Posted: Fri Nov 04, 2016 11:24 pm
by B.Goode
I am not in front of a system with PuTTy right now, but in one of the dialogue boxes prior to opening the connection there is a tick box option for something like 'enable x-forwarding'

Of course, the system running PuTTy also has to be running an X-windows server.

Added later:

From the PuTTy configuration screen navigate to

Connection / SSH / X11

Then tick Enable X11 forwarding

Re: help to start with tkinter

Posted: Sat Nov 05, 2016 8:40 pm
by Canedje
I did found the solution.

First you need to start up in GUI mode after reboot
You need to enter the GUI mode under the same login as the ssh connection (not the standard pi or you are logged in as pi at the ssh also)
then you need to execute at the ssh connection the command: export DISPLAY=:0.0
This forces display at hdmi

Than running the GUI programm from ssh the tkinter wil be displayed at the hdmi monitor

Re: help to start with tkinter

Posted: Sun Nov 06, 2016 12:42 pm
by Canedje
Strange.
The other day I did try to use the HDMI connection again from ssh mode
It will not work anymore and also not get it operational again?

Help again?

Re: help to start with tkinter

Posted: Sun Nov 06, 2016 12:49 pm
by Canedje
Solution found again.

use gksudo command instead of sudo

Re: help to start with tkinter

Posted: Sun Nov 06, 2016 1:03 pm
by DougieLawson
Why do you think you need to run your Tkinter program with elevated privileges? Does it really need root auth?