1 说明:
=====
1.1 Pyface库由大名鼎鼎的enthought出品。
1.2 介绍:
1.2.1 英文:
traits-capable windowing framework.
The pyface project contains a toolkit-independent GUI abstraction layer,
which is used to support the "visualization" features of the Traits package.
Thus, you can write code in terms of the Traits API (views, items, editors, etc.),
and let pyface and your selected toolkit,
and back-end take care of the details of displaying them.
1.2.2 中文:
具有Traits特质的窗口框架。
pyface项目包含一个独立于工具包的GUI抽象层,
用于支持Traits包的“可视化”功能。
因此,您可以根据Traits API(视图,项目,编辑器等)编写代码,
并让pyface和您选择的工具包,
和后端负责显示它们的细节。
2 准备:
=====
2.1 官网:
https://github.com/enthought/pyfacehttps://pypi.org/project/pyface/#教程https://docs.enthought.com/pyface/https://docs.enthought.com/pyface/api/pyface.html
2.2 依靠:
The following GUI backends are supported:选择一个即可
wxPython #本机未安装
PyQt #本机安装pyqt5
PySide #本机安装pyside2
2.3 安装:
pip install pyface
3 Hello world:
===========
3.1 效果图:
3.2 代码:注释中有4种方法,基本对建议窗口设置的熟悉和入门了。
'''#方法一from pyface.api import ApplicationWindow, GUI, HeadingTextclass MainWindow(ApplicationWindow): #窗口标题名,默认窗口大小设置 title = "Hello World==你好世界!" #默认 #定义 def _create_contents(self, parent): #窗口标签显示text文本 self._label = HeadingText(parent, text="Hello World==你好世界!") return self._label.controldef main(): gui = GUI() window = MainWindow() window.open() gui.start_event_loop()if __name__ == "__main__": main()''''''#方法二from pyface.api import ApplicationWindow, GUI, HeadingTextclass MainWindow(ApplicationWindow): #窗口标题名 title = "Hello World==你好世界!" #注意没有逗号 size = (700, 700) #窗口大小设置,与size与title没有逗号隔开 #定义 def _create_contents(self, parent): #窗口标签显示text文本 self._label = HeadingText(parent, text="Hello World==你好世界!") return self._label.controlgui = GUI()window = MainWindow()window.open()gui.start_event_loop()''''''#方法三from pyface.api import ApplicationWindow, GUI, HeadingTextclass MainWindow(ApplicationWindow): #窗口标题名 title = "Hello World==你好世界!" #注意没有逗号 size = (700, 700) #窗口大小设置,与size与title没有逗号隔开 #定义 def _create_contents(self, parent): #窗口标签显示text文本 self._label = HeadingText(parent, text="Hello World==你好世界!") return self._label.controlif __name__ == "__main__": gui = GUI() window = MainWindow() window.open() gui.start_event_loop()'''#方法四from pyface.api import ApplicationWindow, GUI, HeadingTextclass MainWindow(ApplicationWindow): #窗口标题名 #title = "Hello World==你好世界!" #注意没有逗号 #size = (700, 700) #窗口大小设置,与size与title没有逗号隔开 #定义 def _create_contents(self, parent): #窗口标签显示text文本 self._label = HeadingText(parent, text="Hello World==你好世界!") return self._label.controlif __name__ == "__main__": gui = GUI() #window = MainWindow() window = MainWindow(size = (700, 700) ,title = "Hello World==你好世界!" ) window.open() gui.start_event_loop()
4 pythonshell:
==========
4.1 简易pythonshell:
4.1.1 代码:
from pyface.api import ApplicationWindow, GUI, PythonShellclass MainWindow(ApplicationWindow): #窗口大小和标题名 size = (800, 800) title = "Pythonshell" def _create_contents(self, parent): self._shell = PythonShell(parent) #调用pythonsheel return self._shell.controlif __name__ == "__main__": gui = GUI() window = MainWindow() window.open() gui.start_event_loop()
4.1.2 效果图:
4.2 高级pythonshell:
4.2.1 特点:带有功能菜单、图标、网址外联的。
4.2.2 文件结构展示:
4.2.3 代码省略,看看效果图:
4.2.4 注意上面还运行了一个外部脚本:matplotlib-tk-pie.py
5 progress:
========
5.1 带进度条的主窗口。
5.2 代码:
import timefrom pyface.api import GUI, ApplicationWindow, ProgressDialogfrom pyface.action.api import Action, MenuManager, MenuBarManagerdef task_func(t): #展示进度条窗口 progress = ProgressDialog( , message="counting to %d" % t, max=t, show_time=True, #显示展示时间 can_cancel=True, #显示取消按钮 ) progress.open() for i in range(0, t + 1): time.sleep(1) #print(i) (cont, skip) = progress.update(i) if not cont or skip: break progress.update(t)#def _main(): #注意带下划线的命名,防止与程序的main重复def num_main(): #这种就不会 task_func(10)'''#主窗口,暂时注释掉class MainWindow(ApplicationWindow): def __init__(self, **traits): super(MainWindow, self).__init__(**traits) #以上是主窗口的默认设置 # Add a menu bar. self.menu_bar_manager = MenuBarManager( MenuManager( Action(name="E&xit", on_perform=self.close), #Action(name="DoIt", on_perform=_main), #这种命名容易混淆,还好带有下划线 Action(name="DoIt", on_perform=num_main), name="&File", ) ) return'''if __name__ == "__main__": gui = GUI() #window = MainWindow() #主窗口 #window.open() #_main() #注意这种命名 num_main() #这种就不会,调用进度条窗口 gui.start_event_loop()
5.3 操作和效果图:
6 弹出框学习:
==========
6.1 代码:
#一行模块带出,方法一,带括号from pyface.api import ( ApplicationWindow, GUI, YES, choose_one, confirm, error, information, warning,)#一行模块导出,方法二,不带括号from pyface.action.api import Action, MenuBarManager, MenuManager#定义主窗口class MainWindow(ApplicationWindow): def __init__(self, **traits): super(MainWindow, self).__init__(**traits) #默认主窗口设置,背景颜色为蓝色 # 定义菜单menu_bar self.menu_bar_manager = MenuBarManager( MenuManager( Action(name="E&xit", on_perform=self._on_exit), name="&File" ) ) return def _on_exit(self): parent = self.control #调出弹出窗口 choose_one(parent, "Make a choice", ["one", "two", "three"]) #选择框 #print(choose_one(parent, "Make a choice", ["one", "two", "three"])) information(parent, "Going...") #信息弹出框/窗口 warning(parent, "Going......") #警告框 error(parent, "Gone!") #错误框 if confirm(parent, "Should I exit?") == YES: #确认框 self.close()if __name__ == "__main__": gui = GUI() window = MainWindow(size=(800,800),) window.open() gui.start_event_loop()
6.2 效果图:
7 本机默认pyface的窗口GUI是pyqt5
===自己整理并分享出来===
喜欢的人,请点赞、转发、关注、评论和收藏。