您的位置:首页 > 其它

cygwin 运行窗口程序

2016-01-10 14:00 417 查看
首先,默认安装的cygwin是不能运行窗口程序的

比如,一段python窗口程序:

import*fromtkinter
Tk()
mainloop()


如果使用命令行:

python3py.py#py.py内容是上段代码


会提示如下错误:

Traceback(mostrecentcalllast):
File"py.py",line3,in<module>
Tk()
File"/usr/lib/python3.4/tkinter/__init__.py",line1851,in__init__
self.tk=_tkinter.create(screenName,baseName,className,interactive,wantobjects,useTk,sync,use)
_tkinter.TclError:nodisplaynameandno$DISPLAYenvironmentvariable


网上搜索,很多人说通过命令行:

exportDISPLAY=:0.0
#或者
exportDISPLAY=0:0
#抑或
exportDISPLAY=localhost:12.0


然而,这个错误确实是变了,变成了这样:

Traceback(mostrecentcalllast):
File"py.py",line3,in<module>
Tk()
File"/usr/lib/python3.4/tkinter/__init__.py",line1851,in__init__
self.tk=_tkinter.create(screenName,baseName,className,interactive,wantobjects,useTk,sync,use)
_tkinter.TclError:couldn'tconnecttodisplay":0.0"


然后百度cygwintkinterdisplay,出来的结果我只能用呵呵表示,最后还是用360搜索结合bing,慢慢理清的头绪

发现,如果想要运行窗口程序,需要安装xserver.
可以选择xmingxserver,也就是cygwin/x
最新的cygwin/x是集成在cygwin安装里的,在安装的时候选择安装程序的时候,添加xinit



我把xorg也选上了



完成安装后,我是用命令:

exportDISPLAY=:0.0

startx


结果异常退出,提示:

$/usr/bin/xterm:Xterror:Can'topendisplay::0.0


很奇怪,后来从http://x.cygwin.com/docs/faq/cygwin-x-faq.html#q-cannot-open-display看到这么一句:

Someproductsthathavebeenreportedtocauseproblems:
  AventailConnect
  ZonealarmPCFirewallfromZonelab
Note:Theseproductsmaynotcauseproblemsinallconfigurations.However,theCygwin/Xprojecthasneitherthetime,ability,norresourcestohelpyoucorrectlyconfigureyourthird-partysoftware.


吓得我赶紧把360那俩货关了,结果还是不行,看来是冤枉他们了,让后又360so,找到这么一句:

Cygwindoesn'tautomaticallystartanXserver.Youneedtoinstallthepackages
xorg-server
and
xinit
,andrun
startxwin
.
阿西吧,原来是用命令:

exportDISPLAY=:0.0

startxwin&#后面加上&就可以了继续在cygwin里面输入命令了,不出意外的话应该可以了,而且托盘会有xming的图标,可以从那退出


效果是这样的:



有x图标的那个就是xserver



这是py.py运行的结果

startxwin&>x-server-log&

exportDISPLAY=:0.0


就可以把标准输出和标准错误输出重定向的文件s-server-log中,在cygwin控制台中,就看不到xserver的输出信息

如果每次启动都需要启动xserver
可以在~/.bashrc中在最后添加两行代码:

exportDISPLAY=:0.0
startxwin&>x-server-log&



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: