docker _tkinter.TclError: couldn't connect to display
Asked Answered
B

2

25

ubuntu 16.04 , nvidia-docker installed, a tensorflow container running, python 2.7

i want to run a simple python code inside the container. shown as below

from tkinter import *
master = Tk()

canvas_width = 80
canvas_height = 40
w = Canvas(master, 
           width=canvas_width,
           height=canvas_height)
w.pack()

y = int(canvas_height / 2)
w.create_line(0, y, canvas_width, y, fill="#476042")


mainloop()

but when i run this, this error comes

_tkinter.TclError: no display name and no $DISPLAY environment variable

and according to this site https://github.com/moby/moby/issues/8838 i committed my container to an image, and run it again with the -e flag:

-e DISPLAY=:0.0

but here comes the error:

_tkinter.TclError: couldn't connect to display :0.0

i'm not using ssh to login the container. could anyone can give me advice about it?

Beichner answered 8/3, 2018 at 8:55 Comment(0)
B
30

This is because the container couldn't access the x11 socket of the host. so when doing the docker run, need to include these two flag.

-v /tmp/.X11-unix:/tmp/.X11-unix
-e DISPLAY=unix$DISPLAY

and after this, we need to do another operation. because the default settings of X11 only allows local users to print. so we need to change this to all users.

$ sudo apt-get install x11-xserver-utils
$ xhost +

then the problem solved.

Beichner answered 12/3, 2018 at 6:56 Comment(3)
It works on Ubuntu! How to make this work on Windows ?Reilly
Im following this and I still get "couldn't connect to display "127.0.0.1:0.0""Accentor
Is xhost + really safe since this allows ALL users to connect? Shouldn't this be xhost +local:docker instead (if using docker in local)?Bostick
I
6

_tkinter.TclError: couldn't connect to display "localhost:0.0"

i have same error message, solved. (at least the error message gone!)

export MPLBACKEND='Agg'
Inexpiable answered 19/8, 2020 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.