Tkinter problem: TclError> couldn't connect to display ":0

M

Michael Matveev

Hi,
I use live Debian on VM and trying to compile this code.


import Tkinter

root = Tkinter.Tk()

root.title("Fenster 1")
root.geometry("100x100")

root.mainloop()


The shell gives out that kind of message:

File "test.py", line 5, in <module>
root = Tkinter.Tk()
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: couldn't connect to display ":0"



thanks for helping out.

greets.
Mike
 
S

Steven D'Aprano

Michael said:
Hi,
I use live Debian on VM and trying to compile this code.


import Tkinter

root = Tkinter.Tk()

root.title("Fenster 1")
root.geometry("100x100")

root.mainloop()


The shell gives out that kind of message:

File "test.py", line 5, in <module>
root = Tkinter.Tk()
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: couldn't connect to
display ":0"


Are you using ssh to connect to the system? If I create a file and run it
directly from the machine I am physically sitting at, it works fine and the
window is displayed as expected:

[steve@ando ~]$ cat test.py
import Tkinter
root = Tkinter.Tk()
root.title("Fenster 1")
root.geometry("100x100")
root.mainloop()
[steve@ando ~]$ python2.7 test.py
[steve@ando ~]$


But if I ssh to the machine, I get an error (although a different error from
you):

steve@orac:~$ ssh ando
steve@ando's password:
Last login: Thu Dec 12 19:27:04 2013 from 203.7.155.68
[steve@ando ~]$ python2.7 test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
root = Tkinter.Tk()
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1685, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive,
wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable


If I set the $DISPLAY environment variable, it works for me:

[steve@ando ~]$ export DISPLAY=":0"
[steve@ando ~]$ python2.7 test.py
[steve@ando ~]$ logout
Connection to ando closed.

But ando is the machine I am physically seated at, so it's not surprising
that I can see the window on the X display. If I go the other way, and try
to run the code on orac (the remote machine), I get the same error as you:

steve@orac:~$ export DISPLAY=":0"
steve@orac:~$ python2.6 test.py
No protocol specified
Traceback (most recent call last):
File "test.py", line 2, in <module>
root = Tkinter.Tk()
File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1646, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive,
wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0"


So you need to X-forward from the remote machine to the machine you are
physically on, or perhaps it's the other way (X is really weird). I have no
idea how to do that, but would love to know.
 
S

Steven D'Aprano

With SSH, that's usually just "ssh -X target", and it'll mostly work.

Holy cow, it works! Sloooooooowly, but works.


steve@runes:~$ ssh -X ando.pearwood.info
(e-mail address removed)'s password:
Last login: Mon Dec 30 10:10:13 2013 from orac
[steve@ando ~]$ python2.7 test.py
[steve@ando ~]$
 
C

Chris Angelico

With SSH, that's usually just "ssh -X target", and it'll mostly work.

Holy cow, it works! Sloooooooowly, but works.


steve@runes:~$ ssh -X ando.pearwood.info
(e-mail address removed)'s password:
Last login: Mon Dec 30 10:10:13 2013 from orac
[steve@ando ~]$ python2.7 test.py
[steve@ando ~]$

On a LAN, it's not even slow! I've actually run VLC through ssh -X and
watched a DVD that was in a different computer's drive. That was fun.

You can even get a Windows X server and run Linux GUI programs on a
Windows client. *Very* useful if you're working with both types of
computer.

ChrisA
 
J

Jason Swails

Holy cow, it works! Sloooooooowly, but works.

I usually use "ssh -Y". The -Y argument toggles trusted forwarding. From
the ssh man-page:

-Y Enables trusted X11 forwarding. Trusted X11 forwardings are
not subjected to the X11
SECURITY extension controls.

I've found -Y is a bit faster than -X in my experience (I've never really
had many problems with X-forwarding on LANs in my experience -- even with
OpenGL windows)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top