首先, 默認安裝的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 &