Python Tkinter in Docker .TclError: couldn't connect to display

雨燕双飞 提交于 2019-12-06 05:16:50

问题


I am new to python and am trying to build a small app. It needs to be a GUI app and I was wanting to containerise it with docker. I am getting the following error and can not find a solution

No protocol specified
No protocol specified
Traceback (most recent call last):
  File "tkinker.py", line 7, in <module>
    tinker = Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1818, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0.0"

It starts locally but wont start in docker. My OS is Xubuntu.

I have created a sample app (below), see run-test.sh https://github.com/jeremysells/test/tree/master/docker-tkinter


回答1:


As described here, you would need an X11 graphic layer.
But since you are already on an '(X)Ubuntu, setting the DISPLAY should be enough:

export DISPLAY=127.0.0.1:0.0

docker run -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix yourImage

Check also XAuthority.




回答2:


You'll have to set DISPLAY in the container. You can add it as an argument to the docker run command like this:

docker run -ti -e DISPLAY=$DISPLAY blah-image blah-command

DISPLAY should be set in the Xubuntu shell you are running the command from.




回答3:


Type xhost + in your terminal. Then

docker run --rm \
    --network=host --privileged \
    -v /dev:/dev \
    -e DISPLAY=$DISPLAY \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -v $HOME/.Xauthority:/root/.Xauthority \
    -it yourimage

worked for me. xhost + disables access control. After which a client can connect from any host.



来源:https://stackoverflow.com/questions/39445047/python-tkinter-in-docker-tclerror-couldnt-connect-to-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!