next up previous contents
Next: Signals 和 Slots Up: *使用 QT 設計 KDE Previous: Qt 開發環境建立   Contents

gtk+ 開發環境建立

  1. 安裝 Glade 套件:
    [root@dywHome2 ~]# [root@deyu ~]# yum install glade3
    
  2. 執行glade designer
    圖形界面
    Applications->Programming->Glade Interface Designer
    文字界面
    [dywang@deyu glade]$ glade-3
    
  3. 相關聯結
    1. GTK+ and Glade3 GUI Programming Tutorial
    2. 用 Libglade 快速開發 GTK+ 視窗程式
    3. Calculator Program using Python and Glade
    4. Python 教學
  4. 下載範例檔 Calculator Program Download
  5. 以glade-3開啟calculator.glade
    1. table1 Rows: 5 -> 6
    2. insert ok button in table1 (0,5)
    3. rename ok button Label and Name to Sqrt
    4. set Sqrt button clicked Signals to on_Sqrt_clicked
    5. vim calculatorglade.py
    
    # coding: utf-8
    import sys
    import math # 匯入數學模組
    try:
    	import pygtk
    	pygtk.require('2.0')
    except:
    	pass
    .... omission ....
    
    class Calculator:
    .... omission ....
    			"on_Add_clicked" : self.displayAdd,
    			"on_Sqrt_clicked" : self.displaySqrt, # 定義sqrt按鈕接受函數
    .... omission ....
    		if operator == 'Sqrt':
    			self.firstOperand = self.wTree.get_widget("displayText").get_text()
    			result = math.sqrt(float(self.firstOperand))
    			self.wTree.get_widget("displayText").set_text(str(result))
    .... omission ....
    	
    	def displaySqrt(self,widget): #定義displaySqrt函數
    		self.compute("Sqrt")
    
  6. 執行
    [dywang@deyu glade]$ python calculatorglade.py
    



2017-06-14