Python 新手之旅 V1.10 - 單元 7 - Python 有各式各類的開發應用



標準模組庫已經提供相當多的輪子,可是新技術一直冒出來,還有不同的偏好跟需求也很多,就有團隊依他們的情況做出他們需要的模組庫 (library) ,這些統稱為第三方模組庫 (third-party library) ,也就是非官方發展的模組庫。


像是網路應用的管理系統或維基引擎等等


名稱簡介
ERP5基於 Python 及 Zope 的開放原始碼企業資源計畫 (Enterprise Resource Planning, ERP) 。
GNU Mailman管理電子郵件列表。
MoinMoin建置 Wiki 網站的引擎。
Odoo開放原始碼的商業應用管理系統。
Planet用作聚合器 (feed aggregator) 。
Plone開放原始碼的內容管理系統 (content management system) 。

更多的是架站的框架 (framework) ,每種框架都有其獨到之處


名稱簡介
CherryPy物件導向式的架站框架。
Bottle輕量級架站框架。
DjangoMVC 模式的架站框架。
Flask輕量級架站框架。
Pylons輕量級架站框架。
Quixote架站框架。
Tornado輕量級架站框架。
TurboGearMVC 模式的架站框架。
web2pyMVC 模式的架站框架。
Zope伺服器架站軟體。

遊戲引擎或圖形處理相關


名稱簡介
Pygame連結到 SDL 的模組庫。
Panda3D3D 遊戲引擎。
Python Imaging Library圖像處理模組庫。
Python-Ogre連結到 OGRE 3D 的模組庫。
Soya3D3D 遊戲引擎。

使用者介面 (user interface) 也不少


名稱簡介
Kivy跨平台的自然使用者介面 (natural user interface, NUI) 。
PyGTK連結到 GTK 的模組庫。
PyQt連結到 Qt 的模組庫。
PySide連結到 Qt 的模組庫。
wxPython連結到 wxWidgets 的模組庫。

此外,各種科學應用也很多


名稱簡介
Astropy提供給天文學相關應用的模組。
Biopython提供給分子生物學相關應用的模組。
graph-tool提供給統計分析圖形的模組。
Pathomx提供給生物化學分析數據的模組。
NetworkX提供給研究複雜網路的模組。
SciPy提供做科學計算的模組。
scikit-learn提供給機器學習的模組。
scikit-image提供給圖形處理的模組。
SymPy提供給符號計算的模組。
TomoPy提供給斷層掃描顯影技術的模組。
Veusz提供給科學繪圖的模組。
VisTrails提供給工作流管理系統的模組。

以上僅整理出部分受歡迎的第三方模組庫,幾乎各方面的應用都有相對應的模組庫可供選用。


我們以 Kivy 為例,寫一個示範程式,先來複習一下怎麼做 GUI


  1. 決定版面 (layout) 編排模式。
  2. 決定放入哪些小工具 (widget) 。
  3. 設定小工具相關的事件 (event) 。

雖說 Kivy 強調他們是 NUI ,但其實先把 NUI 當 GUI 理解就行了。以下範例全部放在 Python 程式檔案 main.py 中


001from kivy.app import App
002from kivy.uix.boxlayout import BoxLayout
003from kivy.uix.button import Button
004from kivy.uix.label import Label
005
006class Demo(BoxLayout):
007    def __init__(self, **kwargs):
008        super(Demo, self).__init__(**kwargs)
009        self.count=0
010        self.orientation='vertical'
011
012        s1='Click Me!'
013        self.click=Button(text=s1)
014        self.click.on_press=self.do_something
015        self.add_widget(self.click)
016
017        s2='Something happened...'
018        self.display=Label(text=s2)
019        self.add_widget(self.display)
020
021    def do_something(self):
022        s1='You have clicked the button.'
023        s2='Click Me Again!'
024        s3='Something happened...'
025        s4='Click Me!'
026        if self.count % 2 == 0:
027            self.display.text=s1
028            self.click.text=s2
029        else:
030            self.display.text=s3
031            self.click.text=s4
032
033        self.count+=1
034
035class DemoApp(App):
036
037    def build(self):
038        return Demo()
039
040if __name__ == '__main__':
041    DemoApp().run()

類別 Demo 繼承自 BoxLayout , BoxLayout 就是盒狀式的版面編排,簡言之,版面是依平面盒子分布的,小工具就是放在盒子裡


006class Demo(BoxLayout):

建構子 (constructor) 的部分先看到屬性 orientation 設定成 'vertical' ,這意思是說 Demo 盒狀版面是縱向的


007def __init__(self, **kwargs):
008    super(Demo, self).__init__(**kwargs)
009    self.count=0
010    self.orientation='vertical'
011
012    s1='Click Me!'
013    self.click=Button(text=s1)
014    self.click.on_press=self.do_something
015    self.add_widget(self.click)
016
017    s2='Something happened...'
018    self.display=Label(text=s2)
019    self.add_widget(self.display)

接下來設定兩個小工具, Button 及 Label ,由於版面已經設定為縱向盒狀,因此 Button 會是從上而下第一個小工具, Label 則是第二個。此外 Button 的 on_press 屬性設定為 do_something() 方法,因此按下按鈕就會執行 do_something() 。


do_something() 做的事情很簡單,就是替換 Button 及 Label 的文字。執行 Kivy 程式要在命令列打指令 kivy ,如下


$ kivy main.py

就會出現如下的視窗



點擊 Click Me! 後,如下



以上的方式跟使用 tkinter 一樣,都是完全用程式碼刻 GUI 出來。其實 Kivy 提供了設計 GUI 外觀的彈性,像是設計 GUI 外觀的部分可以用 Kv 設計語言來完成,這也是說把 MVC 模式中的 V 交給 Kv 檔案,至於 Kv 檔案的副檔名則是 .kv 。


我們將以上 main.py 中的 GUI ,改以 Kv 語言寫在以下的 demo.kv 檔案中


001#:kivy 1.9.0
002
003<Demo>:
004    display: display
005    click: click_me
006
007    BoxLayout:
008        size: root.size
009        pos: root.pos
010        orientation: 'vertical'
011        padding: 20
012
013        Button:
014            id: click_me
015            text: 'Click Me!'
016            on_press: root.do_something()
017
018        Label:
019            id: display
020            text: 'Something happened...'

Kv 語言是用標記的方式對應到 Demo 類別,先用 <Demo> 指定類別為 Demo ,然後用跟 Python 檔案一樣的縮排, display 跟 click 對應到 Python 程式中的屬性,接下來是 BoxLayout ,除了增加設定 padding 外,就是一個 Button 接著一個 Label 。


接著將 main.py 修改如下


001from kivy.app import App
002from kivy.uix.widget import Widget
003from kivy.properties import ObjectProperty
004
005class Demo(Widget):
006    display = ObjectProperty()
007    click = ObjectProperty()
008
009    def __init__(self, **kwargs):
010        super(Demo, self).__init__(**kwargs)
011        self.count = 0
012
013    def do_something(self):
014        s1 = 'You have clicked the button.'
015        s2 = 'Click Me Again!'
016        s3 = 'Something happened...'
017        s4 = 'Click Me!'
018        if self.count % 2 == 0:
019            self.display.text = s1
020            self.click.text = s2
021        else:
022            self.display.text = s3
023            self.click.text = s4
024
025        self.count += 1
026
027class DemoApp(App):
028
029    def build(self):
030        return Demo()
031
032if __name__ == '__main__':
033    DemoApp().run()

注意到 Demo 改成繼承 Widget


005class Demo(Widget):

display 與 click 兩個屬性改由 ObjectProperty() 取得


006display = ObjectProperty()
007click = ObjectProperty()

基本上 Kv 檔案的檔名要跟 Python 程式的類別名稱相同,如此例的類別名稱為 Demo , Kv 檔案的檔名為 demo.kv ,這樣 Kivy 就會自動尋找相同資料夾是否有跟類別名稱相同的 Kv 檔案,如果有,就會自動載入當作 MVC 模式中的 V ,至於 Python 檔案就是 C 。


這個範例很簡單,沒有用到 M ,如果需要用到資料處理的 M ,那會是另一個 Python 程式檔案,用 import 載入到 main.py 內就行了。

重新執行,結果如下



相較 tkinter , Kivy 的優勢是可以輸出成 Android 跟 iOS 的 App ,這點 tkinter 就沒辦法了說。


the end


Python 新手之旅 V1.10 (Google Play)

沒有留言: