next up previous contents
Next: 實機練習題 Up: Glade Previous: 簡介   Contents

Calculator Example

  1. 下載計算機範例檔
    calculator.glade
    calculatorglade.py
  2. 下載計算機範例檔(增加開根號)
    calculator1.glade
    calculator1.py
  3. 以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")
    
  4. 執行
    [dywang@deyu glade]$ python calculatorglade.py
    
  5. 相關聯結
    1. GTK+ and Glade3 GUI Programming Tutorial
    2. 用 Libglade 快速開發 GTK+ 視窗程式
    3. Calculator Program using Python and Glade
    4. Python 教學



2017-06-14