1说明: 1。1Pyface库由大名鼎鼎的enthought出品。 1。2介绍: 1。2。1英文: traitscapablewindowingframework。 ThepyfaceprojectcontainsatoolkitindependentGUIabstractionlayer, whichisusedtosupportthevisualizationfeaturesoftheTraitspackage。 Thus,youcanwritecodeintermsoftheTraitsAPI(views,items,editors,etc。), andletpyfaceandyourselectedtoolkit, andbackendtakecareofthedetailsofdisplayingthem。 1。2。2中文: 具有Traits特质的窗口框架。 pyface项目包含一个独立于工具包的GUI抽象层, 用于支持Traits包的可视化功能。 因此,您可以根据TraitsAPI(视图,项目,编辑器等)编写代码, 并让pyface和您选择的工具包, 和后端负责显示它们的细节。 2准备: 2。1官网:https:github。comenthoughtpyfacehttps:pypi。orgprojectpyface教程https:docs。enthought。compyfacehttps:docs。enthought。compyfaceapipyface。html 2。2依靠: ThefollowingGUIbackendsaresupported:选择一个即可 wxPython本机未安装 PyQt本机安装pyqt5 PySide本机安装pyside2 2。3安装:pipinstallpyface 3Helloworld: 3。1效果图: 3。2代码:注释中有4种方法,基本对建议窗口设置的熟悉和入门了。方法一frompyface。apiimportApplicationWindow,GUI,HeadingTextclassMainWindow(ApplicationWindow):窗口标题名,默认窗口大小设置titleHelloWorld你好世界!默认titlePyface定义defcreatecontents(self,parent):窗口标签显示text文本self。labelHeadingText(parent,textHelloWorld你好世界!)returnself。label。controldefmain():guiGUI()windowMainWindow()window。open()gui。starteventloop()ifnamemain:main()方法二frompyface。apiimportApplicationWindow,GUI,HeadingTextclassMainWindow(ApplicationWindow):窗口标题名titleHelloWorld你好世界!注意没有逗号size(700,700)窗口大小设置,与size与title没有逗号隔开定义defcreatecontents(self,parent):窗口标签显示text文本self。labelHeadingText(parent,textHelloWorld你好世界!)returnself。label。controlguiGUI()windowMainWindow()window。open()gui。starteventloop()方法三frompyface。apiimportApplicationWindow,GUI,HeadingTextclassMainWindow(ApplicationWindow):窗口标题名titleHelloWorld你好世界!注意没有逗号size(700,700)窗口大小设置,与size与title没有逗号隔开定义defcreatecontents(self,parent):窗口标签显示text文本self。labelHeadingText(parent,textHelloWorld你好世界!)returnself。label。controlifnamemain:guiGUI()windowMainWindow()window。open()gui。starteventloop()方法四frompyface。apiimportApplicationWindow,GUI,HeadingTextclassMainWindow(ApplicationWindow):窗口标题名titleHelloWorld你好世界!注意没有逗号size(700,700)窗口大小设置,与size与title没有逗号隔开定义defcreatecontents(self,parent):窗口标签显示text文本self。labelHeadingText(parent,textHelloWorld你好世界!)returnself。label。controlifnamemain:guiGUI()windowMainWindow()windowMainWindow(size(700,700),titleHelloWorld你好世界!)window。open()gui。starteventloop() 4pythonshell: 4。1简易pythonshell: 4。1。1代码:frompyface。apiimportApplicationWindow,GUI,PythonShellclassMainWindow(ApplicationWindow):窗口大小和标题名size(800,800)titlePythonshelldefcreatecontents(self,parent):self。shellPythonShell(parent)调用pythonsheelreturnself。shell。controlifnamemain:guiGUI()windowMainWindow()window。open()gui。starteventloop() 4。1。2效果图: 4。2高级pythonshell: 4。2。1特点:带有功能菜单、图标、网址外联的。 4。2。2文件结构展示: 4。2。3代码省略,看看效果图: 4。2。4注意上面还运行了一个外部脚本:matplotlibtkpie。py 5progress: 5。1带进度条的主窗口。 5。2代码:importtimefrompyface。apiimportGUI,ApplicationWindow,ProgressDialogfrompyface。action。apiimportAction,MenuManager,MenuBarManagerdeftaskfunc(t):展示进度条窗口progressProgressDialog(titleprogress,messagecountingtodt,maxt,showtimeTrue,显示展示时间cancancelTrue,显示取消按钮)progress。open()foriinrange(0,t1):time。sleep(1)print(i)(cont,skip)progress。update(i)ifnotcontorskip:breakprogress。update(t)defmain():注意带下划线的命名,防止与程序的main重复defnummain():这种就不会taskfunc(10)主窗口,暂时注释掉classMainWindow(ApplicationWindow):definit(self,traits):super(MainWindow,self)。init(traits)以上是主窗口的默认设置Addamenubar。self。menubarmanagerMenuBarManager(MenuManager(Action(nameExit,onperformself。close),Action(nameDoIt,onperformmain),这种命名容易混淆,还好带有下划线Action(nameDoIt,onperformnummain),nameFile,))returnifnamemain:guiGUI()windowMainWindow()主窗口window。open()main()注意这种命名nummain()这种就不会,调用进度条窗口gui。starteventloop() 5。3操作和效果图: 6弹出框学习: 6。1代码:一行模块带出,方法一,带括号frompyface。apiimport(ApplicationWindow,GUI,YES,chooseone,confirm,error,information,warning,)一行模块导出,方法二,不带括号frompyface。action。apiimportAction,MenuBarManager,MenuManager定义主窗口classMainWindow(ApplicationWindow):definit(self,traits):super(MainWindow,self)。init(traits)默认主窗口设置,背景颜色为蓝色定义菜单menubarself。menubarmanagerMenuBarManager(MenuManager(Action(nameExit,onperformself。onexit),nameFile))returndefonexit(self):parentself。control调出弹出窗口chooseone(parent,Makeachoice,〔one,two,three〕)选择框print(chooseone(parent,Makeachoice,〔one,two,three〕))information(parent,Going。。。)信息弹出框窗口warning(parent,Going。。。。。。)警告框error(parent,Gone!)错误框ifconfirm(parent,ShouldIexit?)YES:确认框self。close()ifnamemain:guiGUI()windowMainWindow(size(800,800),title弹出框学习)window。open()gui。starteventloop() 6。2效果图: 7本机默认pyface的窗口GUI是pyqt5 自己整理并分享出来 喜欢的人,请点赞、转发、关注、评论和收藏。