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

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Dec 30 00:22:56 CET 2013


Michael Matveev wrote:

> 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 at ando ~]$ cat test.py
import Tkinter
root = Tkinter.Tk()
root.title("Fenster 1")
root.geometry("100x100")
root.mainloop()
[steve at ando ~]$ python2.7 test.py
[steve at ando ~]$


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

steve at orac:~$ ssh ando
steve at ando's password:
Last login: Thu Dec 12 19:27:04 2013 from 203.7.155.68
[steve at 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 at ando ~]$ export DISPLAY=":0"
[steve at ando ~]$ python2.7 test.py
[steve at 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 at orac:~$ export DISPLAY=":0"
steve at 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.



-- 
Steven




More information about the Python-list mailing list