AutoIt GUI 幼幼班第二課 - GUI data set & color
本帖最後由 rictirse 於 2012-10-24 13:25 編輯直接用 BMI 計算機這個例題 來做 GUI 連結
會用到的指令有
GUICtrlCreateLabel ( "文字", 左方, 上方 [, 寬度 [, 高度 [, 類型(style) [, 進階類型(exStyle)]]]] )
在GUI上建立一個 Label 控制項。
GUISetFont (大小 [, 高度 [, 屬性 [, 字型名稱 [, 視窗控制碼]]]] )
設定視窗的預設字型。
GUICtrlSetColor ( 控制項ID, 文字顏色)
設定指定控制項的文字顏色。
GUICtrlRead ( 控制項ID [, 進階參數] )
讀取指定控制項的狀態或資料。
GUICtrlSetData ( 控制項ID, 資料 [, 預設] )
修改指定控制項的相關資料。
本帖最後由 rictirse 於 2013-2-10 13:56 編輯
第一種作法 按鈕式,這種做法是比較常見的做法,輸入完 身高體重後 按按鈕 才進行計算
#include <GUIConstantsEx.au3>
Opt ( "GUIOnEventMode", 1)
Dim $BMI, $w, $h, $Comment
GUICreate ( "", 390, 115)
GUISetOnEvent ( $GUI_EVENT_CLOSE, "_Exit") ;;OnEvent Mod call func _Exit
GUISetFont ( 10, 800);; 宣告預設的字體 與 大小
GUICtrlCreateLabel ( "Height (cm)", 20, 20, 120) ;; 建立 Label
GUICtrlSetColor ( -1, 0x1260FF)
GUICtrlCreateLabel ( "Width (kg)", 110, 20)
GUICtrlSetColor ( -1, 0x1260FF)
GUICtrlCreateLabel ( "BMI", 315, 20)
GUICtrlSetColor ( -1, 0x1260FF)
$iw = GUICtrlCreateInput ( "", 20, 50, 80, 25);; 建立 Input
GUICtrlSetColor ( -1, 0xEE7600)
$ih = GUICtrlCreateInput ( "", 110, 50, 80, 25)
GUICtrlSetColor ( -1, 0xEE7600)
$btn = GUICtrlCreateButton ( "Sol" , 200, 50, 80 ,25)
GUICtrlSetOnEvent ( -1, "_Sol")
$sol = GUICtrlCreateInput ( "", 290, 50, 80, 25)
GUICtrlSetColor ( -1, 0xEE7600)
$review = GUICtrlCreateLabel ( "評語:", 25, 85, 250, 25)
GUICtrlSetColor ( -1, 0xEE7600)
GUISetState ()
While True
Sleep (10)
WEnd
Func _Exit()
Exit
EndFunc
Func _Sol()
If GUICtrlRead ($iw) <> "" And GUICtrlRead ($ih) <> "" Then;; input (iw)、(ih) 內容不等於 空白時執行
$w = GUICtrlRead ($iw) ;; 變數 w= input (iw) 內的 data
$h = GUICtrlRead ($ih) ;; 變數 h = input (ih) 內的 data
$w /= 100;; 單位換算 cm 轉 m
$BMI = $h/($w^2) ;; 公式 BMI = 身高(kg) / 體重平方(m)
If $BMI <18.5 Then
$Comment = "體重過輕"
GUICtrlSetColor ( $review, 0x00CD00);; 體重過輕 綠色
ElseIf $BMI <= 24 Then
$Comment = "正常體重"
GUICtrlSetColor ( $review, 0x87CEFF);; 正常體重 天藍色
ElseIf $BMI <= 27 Then
$Comment = "過重"
GUICtrlSetColor ( $review, 0xEE9A00);;過重 橙色
ElseIf $BMI <= 30 Then
$Comment = "輕度過胖"
GUICtrlSetColor ( $review, 0xEE4000);;輕度過重 橘紅色
ElseIf $BMI <= 35 Then
$Comment = "中度過胖"
GUICtrlSetColor ( $review, 0xFF0000);;中度過重 鮮紅色
Else
$Comment = "重度過胖"
GUICtrlSetColor ( $review, 0xBF3EFF);;重度過重 紫色
EndIf
GUICtrlSetData ( $sol, StringFormat ( "%f", $BMI)) ;;修改 Input (sol)內的資料 使用 BMI 取代
GUICtrlSetData ( $review, StringFormat ( "評語:%s", $Comment)) ;;修改 Input (review)內的資料 填入評語 %f = 小數點下 六位數
EndIf
EndFunc
本帖最後由 rictirse 於 2013-2-10 13:57 編輯
第二種方法 互動式 - 第一版 CPU無時無刻都在計算BMI,所以比較耗費資源
#include <GUIConstantsEx.au3>
Opt ( "GUIOnEventMode", 1)
Dim $BMI, $w, $h
GUICreate ( "", 300, 115)
GUISetOnEvent ( $GUI_EVENT_CLOSE, "_Exit") ;;OnEvent Mod call func _Exit
GUISetFont ( 10, 800);; 宣告預設的字體 與 大小
GUICtrlCreateLabel ( "Height (cm)", 20, 20, 120) ;; 建立 Label
GUICtrlSetColor ( -1, 0x1260FF)
GUICtrlCreateLabel ( "Width (kg)", 110, 20)
GUICtrlSetColor ( -1, 0x1260FF)
GUICtrlCreateLabel ( "BMI", 225, 20)
GUICtrlSetColor ( -1, 0x1260FF)
$iw = GUICtrlCreateInput ( "", 20, 50, 80, 25);; 建立 Input
GUICtrlSetColor ( -1, 0xEE7600)
$ih = GUICtrlCreateInput ( "", 110, 50, 80, 25)
GUICtrlSetColor ( -1, 0xEE7600)
$sol = GUICtrlCreateInput ( "", 200, 50, 80, 25)
GUICtrlSetColor ( -1, 0xEE7600)
$review = GUICtrlCreateLabel ( "評語:", 25, 85, 250, 25)
GUICtrlSetColor ( -1, 0xEE7600)
GUISetState ()
While True
If GUICtrlRead ($iw) <> "" And GUICtrlRead ($ih) <> "" Then;; input (iw)、(ih) 內容不等於 空白時執行
$w = GUICtrlRead ($iw) ;; 變數 w= input (iw) 內的 data
$h = GUICtrlRead ($ih) ;; 變數 h = input (ih) 內的 data
$w /= 100;; 單位換算 cm 轉 m
$BMI = $h/($w^2) ;; 公式 BMI = 身高(kg) / 體重平方(m)
If $BMI <18.5 Then
$Comment = "體重過輕"
GUICtrlSetColor ( $review, 0x00CD00);; 體重過輕 綠色
ElseIf $BMI <= 24 Then
$Comment = "正常體重"
GUICtrlSetColor ( $review, 0x87CEFF);; 正常體重 天藍色
ElseIf $BMI <= 27 Then
$Comment = "過重"
GUICtrlSetColor ( $review, 0xEE9A00);;過重 橙色
ElseIf $BMI <= 30 Then
$Comment = "輕度過胖"
GUICtrlSetColor ( $review, 0xEE4000);;輕度過重 橘紅色
ElseIf $BMI <= 35 Then
$Comment = "中度過胖"
GUICtrlSetColor ( $review, 0xFF0000);;中度過重 鮮紅色
Else
$Comment = "重度過胖"
GUICtrlSetColor ( $review, 0xBF3EFF);;重度過重 紫色
EndIf
GUICtrlSetData ( $sol, StringFormat ( "%f", $BMI)) ;;修改 Input (sol)內的資料 使用 BMI 取代
GUICtrlSetData ( $review, StringFormat ( "評語:%s", $Comment)) ;;修改 Input (review)內的資料 填入評語 %f = 小數點下 六位數
EndIf
WEnd
Func _Exit()
Exit
EndFunc
第二種方法 互動式 - 第二版 多一組暫存判斷區,讓GUI 輸入視窗內 資料有變動過才會執行 計算,減少CPU 使用
#include <GUIConstantsEx.au3>
Opt ( "GUIOnEventMode", 1)
Dim $BMI, $w, $h
Dim $tmpW, $tmpH;;; 宣告 h 、 w 的暫存變數
GUICreate ( "", 300, 115)
GUISetOnEvent ( $GUI_EVENT_CLOSE, "_Exit") ;;OnEvent Mod call func _Exit
GUISetFont ( 10, 800);; 宣告預設的字體 與 大小
GUICtrlCreateLabel ( "Height (cm)", 20, 20, 120) ;; 建立 Label
GUICtrlSetColor ( -1, 0x1260FF)
GUICtrlCreateLabel ( "Width (kg)", 110, 20)
GUICtrlSetColor ( -1, 0x1260FF)
GUICtrlCreateLabel ( "BMI", 225, 20)
GUICtrlSetColor ( -1, 0x1260FF)
$iw = GUICtrlCreateInput ( "", 20, 50, 80, 25);; 建立 Input
GUICtrlSetColor ( -1, 0xEE7600)
$ih = GUICtrlCreateInput ( "", 110, 50, 80, 25)
GUICtrlSetColor ( -1, 0xEE7600)
$sol = GUICtrlCreateInput ( "", 200, 50, 80, 25)
GUICtrlSetColor ( -1, 0xEE7600)
$review = GUICtrlCreateLabel ( "評語:", 25, 85, 250, 25)
GUICtrlSetColor ( -1, 0xEE7600)
GUISetState ()
While True
;;; 將讀取 資料拉到 if 外
$w = GUICtrlRead ($iw) ;; 變數 w= input (iw) 內的 data
$h = GUICtrlRead ($ih) ;; 變數 h = input (ih) 內的 data
;;; 多加入一組判斷
If $w <> $tmpW Or $h <> $tmpH Then;; 判斷 GUI 視窗內的 W 「或」 H 是否跟 暫存區的 tmpW tmpH 資料是否相同 如果不相同 才執行 下面動作
$tmpW = $w ;; 將 GUI w 內的資料放到暫存內
$tmpH = $h ;; 將 GUI h 內的資料放到暫存內
If GUICtrlRead ($iw) <> "" And GUICtrlRead ($ih) <> "" Then;; input (iw)、(ih) 內容不等於 空白時執行
$w /= 100;; 單位換算 cm 轉 m
$BMI = $h/($w^2) ;; 公式 BMI = 身高(kg) / 體重平方(m)
If $BMI <18.5 Then
$Comment = "體重過輕"
GUICtrlSetColor ( $review, 0x00CD00);; 體重過輕 綠色
ElseIf $BMI <= 24 Then
$Comment = "正常體重"
GUICtrlSetColor ( $review, 0x87CEFF);; 正常體重 天藍色
ElseIf $BMI <= 27 Then
$Comment = "過重"
GUICtrlSetColor ( $review, 0xEE9A00);;過重 橙色
ElseIf $BMI <= 30 Then
$Comment = "輕度過胖"
GUICtrlSetColor ( $review, 0xEE4000);;輕度過重 橘紅色
ElseIf $BMI <= 35 Then
$Comment = "中度過胖"
GUICtrlSetColor ( $review, 0xFF0000);;中度過重 鮮紅色
Else
$Comment = "重度過胖"
GUICtrlSetColor ( $review, 0xBF3EFF);;重度過重 紫色
EndIf
GUICtrlSetData ( $sol, StringFormat ( "%f", $BMI)) ;;修改 Input (sol)內的資料 使用 BMI 取代
GUICtrlSetData ( $review, StringFormat ( "評語:%s", $Comment)) ;;修改 Input (review)內的資料 填入評語 %f = 小數點下 六位數
EndIf
EndIf
WEnd
Func _Exit()
Exit
EndFunc
頁:
[1]