Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

help to start with tkinter

Fri Nov 04, 2016 7:52 pm

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?

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: help to start with tkinter

Fri Nov 04, 2016 9:11 pm

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.

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: help to start with tkinter

Fri Nov 04, 2016 9:25 pm

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?

User avatar
scruss
Posts: 3212
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: help to start with tkinter

Fri Nov 04, 2016 10:21 pm

don't use sudo. It's what's causing the DISPLAY problem.

Note that Python 3 calls it tkinter, but Python calls it Tkinter.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: help to start with tkinter

Fri Nov 04, 2016 10:27 pm

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?
Last edited by Canedje on Fri Nov 04, 2016 10:52 pm, edited 1 time in total.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: help to start with tkinter

Fri Nov 04, 2016 10:48 pm

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
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: help to start with tkinter

Fri Nov 04, 2016 10:57 pm

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?

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: help to start with tkinter

Fri Nov 04, 2016 11:24 pm

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

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: help to start with tkinter

Sat Nov 05, 2016 8:40 pm

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

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: help to start with tkinter

Sun Nov 06, 2016 12:42 pm

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?

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: help to start with tkinter

Sun Nov 06, 2016 12:49 pm

Solution found again.

use gksudo command instead of sudo

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: help to start with tkinter

Sun Nov 06, 2016 1:03 pm

Why do you think you need to run your Tkinter program with elevated privileges? Does it really need root auth?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Python”