Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

10,630 views
Skip to first unread message

Michael Matveev

unread,
Dec 29, 2013, 3:20:00 PM12/29/13
to
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

Chris Angelico

unread,
Dec 29, 2013, 4:40:56 PM12/29/13
to pytho...@python.org
On Mon, Dec 30, 2013 at 7:20 AM, Michael Matveev
<misc...@googlemail.com> wrote:
> 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"

Worked for me on an installed Debian, inside Xfce with xfce4-terminal.

1) What version of Python are you running?
2) Are you running inside some kind of graphical environment?
3) Do you have any sort of permissions/environment change happening? I
get an error like that if I try "sudo python" without any sort of
guard.

ChrisA

Steven D'Aprano

unread,
Dec 29, 2013, 6:22:56 PM12/29/13
to
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.



--
Steven

Chris Angelico

unread,
Dec 29, 2013, 6:30:11 PM12/29/13
to pytho...@python.org
On Mon, Dec 30, 2013 at 10:22 AM, Steven D'Aprano
<steve+comp....@pearwood.info> wrote:
> 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.

With SSH, that's usually just "ssh -X target", and it'll mostly work.
But there are potential issues with .Xauthority, which is why the sudo
example fails.

ChrisA

Steven D'Aprano

unread,
Dec 29, 2013, 10:29:28 PM12/29/13
to
On Mon, 30 Dec 2013 10:30:11 +1100, Chris Angelico wrote:

> On Mon, Dec 30, 2013 at 10:22 AM, Steven D'Aprano
> <steve+comp....@pearwood.info> wrote:
>> 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.
>
> 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
st...@ando.pearwood.info's password:
Last login: Mon Dec 30 10:10:13 2013 from orac
[steve@ando ~]$ python2.7 test.py
[steve@ando ~]$


--
Steven

Chris Angelico

unread,
Dec 29, 2013, 11:11:10 PM12/29/13
to pytho...@python.org
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

Jason Swails

unread,
Dec 30, 2013, 12:59:31 AM12/30/13
to Steven D'Aprano, python list
On Sun, Dec 29, 2013 at 10:29 PM, Steven D'Aprano <st...@pearwood.info> wrote:
On Mon, 30 Dec 2013 10:30:11 +1100, Chris Angelico wrote:

> On Mon, Dec 30, 2013 at 10:22 AM, Steven D'Aprano
> <steve+comp....@pearwood.info> wrote:
>> 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.
>
> With SSH, that's usually just "ssh -X target", and it'll mostly work.

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)

gemja...@gmail.com

unread,
Feb 4, 2016, 9:47:39 AM2/4/16
to
This fixed my problem with thkinter..... sudo cp ~/.Xauthority ~root/

Dave Farrance

unread,
Feb 5, 2016, 6:21:05 AM2/5/16
to
gemja...@gmail.com wrote:

>This fixed my problem with thkinter..... sudo cp ~/.Xauthority ~root/

Which means that you were creating a GUI window with Python as root,
which is to be avoided if you can. If you can't avoid it and you're
running it with sudo in a bash console, rather than a root console, then
I'd suggest adding the line...

XAUTHORITY=$HOME/.Xauthority

...to the root's .bashrc which avoids putting a specific user's
xauthority file in the root directory.
0 new messages