{{{ #!html
PyGTK+ 研究
glade3.6 + Python 2.x + pygtk2
}}} [[PageOutline]] = 環境 = * ubuntu 9.10 * glade3.6.1 + Python 2.6.4 + pygtk2 皆用 apt-get 安裝 = 心得 = * pygtk+ 的程式架構為 程式邏輯 與 圖形介面 分成兩個不同的檔 * 程式邏輯 : python檔 (xx.py) * 圖形介面 : 可藉由 glade3輔助來產生圖形layout 的xml格式檔,共可分兩種 * xx.glade = 使用 libglade 函式庫 * xx.xml = 使用 gtkbuilder 函式庫 == glade3 畫面 == [[Image(http://www.micahcarrick.com/mc_images/gtk-glade-tutorial/glade-3-tutorial-1.jpg,width=600)]] * inspector 展現 editor 端完整的階層架構 * properties 所展現的'''XXX'''元件屬性可以到 http://www.pygtk.org/docs/pygtk/class-'''XXX'''.html 看更詳細的說明 * 如 button 按鈕元件的properties所列出可以設定的值,可到 [http://www.pygtk.org/docs/pygtk/class-gtkbutton.html class-gtkbutton.html] 來看 == gtk 手冊導讀 == http://www.pygtk.org/docs/pygtk/ 中 class-'''XXX'''.html 說明 * Synopsis & Methods : Synopsis 列出此 XXX 物件可用的 Methods,而這些function 則是透過 py 檔,用來改變屬性值或驅動動作的方法 * Ancestry :類別的繼承關係 * Properties & Style Properties :glade 中 properties 部份的 前三個tag 所定義的屬性 * Signals & Signal Prototypes: 驅動訊號,此部份的說明等同於glade 中 "properties -> 訊號 " 的地方。 = Exam 1 = == 此為 libglade 函式庫範例 == * 此範例原文為 [http://linux.cuit.edu.cn/?p=331 簡單計算器:pygtk連接glade設計的界面] * glade 畫面 [[Image(2010-05-10-181229_964x725_scrot.png)]] * 對照到 http://www.pygtk.org/docs/pygtk/class-gtkbutton.html 網頁的畫面 [[Image(2010-05-10-180916_551x344_scrot.png)]] * python 的 部份程式碼 {{{ #!python #!/usr/bin/env python import gtk import gtk.glade class calc: def __init__(self): # 在 py 檔中 導入glade self.glade_file = "calc.glade" self.main_window = gtk.glade.XML(self.glade_file) self.window = self.main_window.get_widget ("calc_window") # 建立處理訊號常數的字典 events_dic = { "haha":self.handle_haha_show, "yaya":self.handle_yaya_show, "on_button4_clicked":self.handle_4_clicked, } # 連接圖形介面與訊號常數 self.main_window.signal_autoconnect(events_dic) # 取得 widget 視窗的元件 self.entry1 = self.main_window.get_widget("entry1") # entry1 為計算機鍵盤下方的元件名稱 self.entry_res = self.main_window.get_widget("entry_result") # entry_res 為計算機鍵盤上方的元件名稱 # 顯示 self.window.show() # 顯示圖形 # 定義按鈕驅動函數 def handle_haha_show(self, widget): self.entry1.set_text('yaya') self.entry1.set_text('yaya') def handle_yaya_show(self, widget): self.entry1.set_text('haha') self.entry1.set_text('haha') def on_button4_clicked(self, widget): self.entry_res.set_text('4') if __name__ == '__main__': calc() gtk.main() }}} = 好用連結 = * [http://www.pygtk.org/ PyGTK: GTK+ for Python] :官方網站 * [http://www.pygtk.org/docs/pygtk/ PyGTK 2.0 Reference Manual (For PyGTK version 2.14.0 2009-03-05 )] :pygtk手冊,重要! * [http://www.micahcarrick.com/12-24-2007/gtk-glade-tutorial-part-1.html GTK+ and Glade3 GUI Programming Tutorial - Part 1]:詳細介紹的gtk + glade3 (英文) * [http://www.micahcarrick.com/05-30-2008/gtk-builder-libglade-faq.html Libglade to GtkBuilder F.A.Q.]:libglade 與 gtkbuilder 的不同(英文) * [http://www.lerosua.org/2009/08/libglade_to_gtkbuilder libglade轉換至gtkbuilder] :當gtkbuilder 取代libglade 的重要步驟(中文)