python 且_Pyface库:一个基于pyqt、pyside、wx且简化的python的GUI
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特質(zhì)的窗口框架。
pyface項目包含一個獨(dú)立于工具包的GUI抽象層,
用于支持Traits包的“可視化”功能。
因此,您可以根據(jù)Traits API(視圖,項目,編輯器等)編寫代碼,
并讓pyface和您選擇的工具包,
和后端負(fù)責(zé)顯示它們的細(xì)節(jié)。
2 準(zhǔn)備:
=====
2.1 官網(wǎng):
https://github.com/enthought/pyfacehttps://pypi.org/project/pyface/#教程https://docs.enthought.com/pyface/https://docs.enthought.com/pyface/api/pyface.html2.2 依靠:
The following GUI backends are supported:選擇一個即可
wxPython #本機(jī)未安裝
PyQt #本機(jī)安裝pyqt5
PySide #本機(jī)安裝pyside2
2.3 安裝:
pip install pyface3 Hello world:
===========
3.1 效果圖:
3.2 代碼:注釋中有4種方法,基本對建議窗口設(shè)置的熟悉和入門了。
'''#方法一from pyface.api import ApplicationWindow, GUI, HeadingTextclass MainWindow(ApplicationWindow): #窗口標(biāo)題名,默認(rèn)窗口大小設(shè)置 title = "Hello World==你好世界!" #默認(rèn) #定義 def _create_contents(self, parent): #窗口標(biāo)簽顯示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): #窗口標(biāo)題名 title = "Hello World==你好世界!" #注意沒有逗號 size = (700, 700) #窗口大小設(shè)置,與size與title沒有逗號隔開 #定義 def _create_contents(self, parent): #窗口標(biāo)簽顯示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): #窗口標(biāo)題名 title = "Hello World==你好世界!" #注意沒有逗號 size = (700, 700) #窗口大小設(shè)置,與size與title沒有逗號隔開 #定義 def _create_contents(self, parent): #窗口標(biāo)簽顯示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): #窗口標(biāo)題名 #title = "Hello World==你好世界!" #注意沒有逗號 #size = (700, 700) #窗口大小設(shè)置,與size與title沒有逗號隔開 #定義 def _create_contents(self, parent): #窗口標(biāo)簽顯示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): #窗口大小和標(biāo)題名 size = (800, 800) title = "Pythonshell" def _create_contents(self, parent): self._shell = PythonShell(parent) #調(diào)用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 特點(diǎn):帶有功能菜單、圖標(biāo)、網(wǎng)址外聯(lián)的。
4.2.2 文件結(jié)構(gòu)展示:
4.2.3 代碼省略,看看效果圖:
4.2.4 注意上面還運(yùn)行了一個外部腳本:matplotlib-tk-pie.py
5 progress:
========
5.1 帶進(jìn)度條的主窗口。
5.2 代碼:
import timefrom pyface.api import GUI, ApplicationWindow, ProgressDialogfrom pyface.action.api import Action, MenuManager, MenuBarManagerdef task_func(t): #展示進(jìn)度條窗口 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重復(fù)def num_main(): #這種就不會 task_func(10)'''#主窗口,暫時注釋掉class MainWindow(ApplicationWindow): def __init__(self, **traits): super(MainWindow, self).__init__(**traits) #以上是主窗口的默認(rèn)設(shè)置 # 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() #這種就不會,調(diào)用進(jìn)度條窗口 gui.start_event_loop()5.3 操作和效果圖:
6 彈出框?qū)W習(xí):
==========
6.1 代碼:
#一行模塊帶出,方法一,帶括號from pyface.api import ( ApplicationWindow, GUI, YES, choose_one, confirm, error, information, warning,)#一行模塊導(dǎo)出,方法二,不帶括號from pyface.action.api import Action, MenuBarManager, MenuManager#定義主窗口class MainWindow(ApplicationWindow): def __init__(self, **traits): super(MainWindow, self).__init__(**traits) #默認(rèn)主窗口設(shè)置,背景顏色為藍(lán)色 # 定義菜單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 #調(diào)出彈出窗口 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: #確認(rèn)框 self.close()if __name__ == "__main__": gui = GUI() window = MainWindow(size=(800,800),) window.open() gui.start_event_loop()6.2 效果圖:
7 本機(jī)默認(rèn)pyface的窗口GUI是pyqt5
===自己整理并分享出來===
喜歡的人,請點(diǎn)贊、轉(zhuǎn)發(fā)、關(guān)注、評論和收藏。
總結(jié)
以上是生活随笔為你收集整理的python 且_Pyface库:一个基于pyqt、pyside、wx且简化的python的GUI的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hashmap remove 没释放内存
- 下一篇: assertpythonraise_使用