Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
963 views
in Technique[技术] by (71.8m points)

python - Putty: _tkinter.TclError: no display name and no display environment variable

Linked Question: Mac OS X: _tkinter.TclError: no display name and no $DISPLAY environment variable

The above has the same issue but the answer to that doesn't apply to me.

a.py

from Tkinter import * 

root = Tk() 
canvas = Canvas(bg='white', width = 200, height = 200) 
canvas.pack() 

canvas.create_line(0, 0, 199, 199, fill="blue", width = 5) 
canvas.create_line(0, 199, 199, 0, fill="blue", width = 5) 

canvas.update() 
canvas.postscript(file = "x.ps") 

root.mainloop() 

a.py is on a remote server

This works fine when I connect to a server via VNC and run it.

But when I connect to a server via Putty on windows and run it, it gives me "no display name and no diplay environment variable"

1) Is it possible to run this through Putty?

2) Can python know if a connection is made through putty and maybe raise my own error instead of TclError?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to run an X server on your windows machine and then enable X11 forwarding in your putty session before you log in (see the Connection / SSH / X11 property page in PuTTY). In the past I've used Xming which is free and eXceed which was not. Once you have an X server on the machine you actually are looking at and forward the X connection over the ssh link, it will setup the DISPLAY environment variable on the remote machine so that X clients can communicate back to the display server. Otherwise, without a DISPLAY setup Tk will raise an error - as you are seeing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...