cygwin 运行窗口程序

首先, 默认安装的cygwin是不能运行窗口程序的

比如,一段python窗口程序:
import * from tkinter
Tk()
mainloop()
如果使用命令行:
python3 py.py #py.py内容是上段代码
会提示如下错误:
Traceback (most recent call last):
  File "py.py", line 3, in <module>
    Tk()
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1851, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
网上搜索,很多人说通过命令行:
export DISPLAY=:0.0
#或者
export DISPLAY=0:0
#抑或
export DISPLAY=localhost:12.0
然而,这个错误确实是变了,变成了这样:
Traceback (most recent call last):
  File "py.py", line 3, in <module>
    Tk()
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1851, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0.0"
然后百度 cygwin tkinter display,出来的结果我只能用呵呵表示,最后还是用360搜索结合bing,慢慢理清的头绪
发现,如果想要运行窗口程序, 需要安装 x server.
可以选择xming x server,也就是cygwin/x
最新的cygwin/x 是集成在cygwin安装里的,在安装的时候选择安装程序的时候,添加xinit
我把xorg也选上了
完成安装后,我是用命令:
export DISPLAY=:0.0
startx
结果异常退出,提示:
$/usr/bin/xterm: Xt error: Can't open display: :0.0
Some products that have been reported to cause problems:
  Aventail Connect
  Zonealarm PC Firewall from Zonelab
Note: These products may not cause problems in all configurations. However, the Cygwin/X project has neither the time, ability, nor resources to help you correctly configure your third-party software.
吓得我赶紧把360那俩货关了,结果还是不行,看来是冤枉他们了,让后又360so,找到这么一句:
 Cygwin doesn't automatically start an X server. You need to install the packages xorg-server and xinit, and run startxwin.
阿西吧,原来是用命令:
export DISPLAY=:0.0
startxwin & #后面加上&就可以了继续在cygwin里面输入命令了,不出意外的话应该可以了,而且托盘会有xming的图标,可以从那退出
效果是这样的:
有x图标的那个就是x server
这是py.py运行的结果
startxwin &> x-server-log &
export DISPLAY=:0.0
就可以把标准输出和标准错误输出重定向的文件s-server-log中,在cygwin控制台中,就看不到x server的输出信息
如果每次启动都需要启动x server
可以在~/.bashrc 中在最后添加两行代码:
export DISPLAY=:0.0
startxwin &> x-server-log &

 

 
原文地址:https://www.cnblogs.com/bovenson/p/5118360.html