Python _tkinter.TclError: 没有显示名称和 $DISPLAY 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37604289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
_tkinter.TclError: no display name and no $DISPLAY environment variable
提问by user3654307
I am running a simple python script in the server:
我在服务器中运行一个简单的 python 脚本:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(60)
y = np.random.randn(60)
plt.scatter(x, y, s=20)
out_png = 'path/to/store/out_file.png'
plt.savefig(out_png, dpi=150)
I try to use the command python example.py
in this server which has matplotlib 1.5.1 installed it fails with the error:
我尝试python example.py
在安装了 matplotlib 1.5.1 的服务器中使用该命令,但失败并显示错误:
Traceback (most recent call last):
File "example.py", line 7, in <module>
plt.scatter(x, y, s=20)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3241, in scatter
ax = gca()
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 928, in gca
return gcf().gca(**kwargs)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 578, in gcf
return figure()
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 527, in figure
**kwargs)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure
window = Tk.Tk()
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1810, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
What is happening here?
这里发生了什么?
回答by Serenity
Matplotlib chooses Xwindows backend by default. You need to set matplotlib to not use the Xwindows backend.
Matplotlib 默认选择 Xwindows 后端。您需要将 matplotlib 设置为不使用 Xwindows 后端。
Add this code to the start of your script (before importing pyplot) and try again:
将此代码添加到脚本的开头(在导入 pyplot 之前)并重试:
import matplotlib
matplotlib.use('Agg')
Or add to .config/matplotlib/matplotlibrc
line backend: Agg
to use non-interactive backend.
或者添加到.config/matplotlib/matplotlibrc
行backend: Agg
以使用非交互式后端。
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc
Or when connect to server use ssh -X remoteMachine
command to use Xwindows.
或者当连接到服务器时使用ssh -X remoteMachine
命令来使用 Xwindows。
Also you may try to export display: export DISPLAY=mymachine.com:0.0
.
您也可以尝试导出 display: export DISPLAY=mymachine.com:0.0
。
For more info: https://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
更多信息:https: //matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
回答by Qihui
You can solve it by adding these two lines in the VERYbeginning of your .py script.
您可以通过在.py 脚本的非常开头添加这两行来解决它。
import matplotlib
matplotlib.use('Agg')
PS: The error will still exists if these two lines are not added in the very beginning of the source code.
PS:如果在源代码的最开始没有添加这两行,错误仍然存在。
回答by Sylhare
To add up on the answer, I used this at the beginning of the needed script. So it runs smoothly on different environments.
为了增加答案,我在所需脚本的开头使用了它。所以它在不同的环境下都能流畅运行。
import os
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
print('no display found. Using non-interactive Agg backend')
mpl.use('Agg')
import matplotlib.pyplot as plt
Because I didn't want it to be alsways using the 'Agg'
backend, only when it would go through Travis CI for example.
因为我不希望它总是使用'Agg'
后端,例如只有当它通过 Travis CI 时。
回答by ajgriese
I had this same issue trying to run a simple tkinter app remotely on a Raspberry Pi. In my case I did want to display the tkinter GUI on the pi display, but I want to be able to execute it over SSH from my host machine. I was also not using matplotlib, so that wasn't the cause of my issue. I was able to resolve the issue by setting the DISPLAY environment variable as the error suggests with the command:
我在尝试在 Raspberry Pi 上远程运行一个简单的 tkinter 应用程序时遇到了同样的问题。就我而言,我确实想在 pi 显示器上显示 tkinter GUI,但我希望能够从我的主机通过 SSH 执行它。我也没有使用 matplotlib,所以这不是我的问题的原因。我能够通过设置 DISPLAY 环境变量来解决这个问题,因为命令的错误提示:
export DISPLAY=:0.0
A good explanation of what the display environment variable is doing and why the syntax is so odd can be found here: https://askubuntu.com/questions/432255/what-is-display-environment-variable
关于显示环境变量正在做什么以及为什么语法如此奇怪的一个很好的解释可以在这里找到:https: //askubuntu.com/questions/432255/what-is-display-environment-variable
回答by Joseph
Another solution is to install Xvfb, and export your display to it. ie:
另一种解决方案是安装 Xvfb,并将您的显示导出到它。IE:
disp=:8
screen=0
geom=640x480x24
exec Xvfb $disp -screen $screen $geom 2>/tmp/Xvfb.log &
Then
然后
$ export DISPLAY=:8
$出口显示=:8
$ ./example.py
$ ./example.py
回答by tsveti_iko
In order to see images, plots and anything displayed on windows on your remote machine you need to connect to it like this:
为了查看远程机器上窗口上显示的图像、绘图和任何内容,您需要像这样连接到它:
ssh -X user@hostname
That way you enable the access to the X server. The X server is a program in the X Window System that runs on local machines (i.e., the computers used directly by users) and handles all access to the graphics cards, display screens and input devices (typically a keyboard and mouse) on those computers.
这样您就可以访问 X 服务器。X 服务器是 X Window 系统中的一个程序,它运行在本地机器(即用户直接使用的计算机)上,并处理对这些计算机上的图形卡、显示屏和输入设备(通常是键盘和鼠标)的所有访问.
More info here.
更多信息在这里。
回答by Mode
I also met this problem while using Xshell to connect Linux server.
我在使用 Xshell 连接 Linux 服务器时也遇到了这个问题。
After seaching for methods, I find Xming + Xshell to solve image imshow problem with matplotlib.
寻找方法后,我找到了Xming + Xshell 来解决matplotlib 的图像显示问题。
If solutions aboved can't solve your problem, just try to download Xming under the condition you're using Xshell. Then set the attribute in Xshell, SSH->tunnel->X11transfer->choose X DISPLAY localhost:0.0
如果上述解决方案不能解决您的问题,请尝试在您使用Xshell的情况下下载Xming。然后在Xshell中设置属性,SSH->tunnel->X11transfer->选择X DISPLAY localhost:0.0