2013-05-11 46 views
-1

我創建了這個腳本來計算mAh和mWh。當我輸入數值並按計算時,計算似乎是正確的。計算器沒有重置

但是,我無法弄清楚如何輸入新的值和命中而不必手動重新啓動腳本。有人能幫我弄清楚缺少的是什麼嗎?

; Source(s) 
; http://www.autoitscript.com/forum/topic/125495-guicreate-select-radio-button/ 

#include <ButtonConstants.au3> 
#include <EditConstants.au3> 
#include <GUIConstantsEx.au3> 
#include <StaticConstants.au3> 
#include <WindowsConstants.au3> 

#Region ### START Koda GUI section ### Form=D:\Downloads\koda_2008-10-03\Forms\mWh2mAh.kxf 
$Form1 = GUICreate("mAh mWh Calculator", 336, 210, 682, 127) 

$capacity = GUICtrlCreateInput("", 224, 48, 81, 21) 
GUICtrlSetCursor (-1, 0) 

$mAh = GUICtrlCreateRadio("mAh", 224, 72, 41, 17) 
GUICtrlSetCursor (-1, 0) 
GUICtrlSetState ($mAh,$GUI_CHECKED) 

$mWh = GUICtrlCreateRadio("mWh", 272, 72, 41, 17) 
GUICtrlSetCursor (-1, 0) 

$Volt=GUICtrlCreateInput("", 224, 96, 81, 21) 
GUICtrlSetCursor (-1, 0) 

$Calculate = GUICtrlCreateButton("Calculate", 104, 136, 75, 25) 
$Input2 = GUICtrlCreateInput("", 184, 136, 121, 21) 
GUICtrlSetState ($Input2, $GUI_DISABLE) 
GUICtrlSetCursor (-1, 0) 

$Label3 = GUICtrlCreateLabel("Calculate between milliampere-hour and milliwatt-hour", 16, 16, 304, 17) 
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") 
$Label4 = GUICtrlCreateLabel("Select the appropriate radio button:", 48, 72, 170, 17) 
$Label1 = GUICtrlCreateLabel("Enter the mAh or mWh value for the battery:", 8, 48, 211, 17) 
$Label2 = GUICtrlCreateLabel("Enter the battery voltage:", 96, 96, 123, 17) 
GUISetState(@SW_SHOW) 
#EndRegion ### END Koda GUI section ### 

While 1 
    $nMsg = GUIGetMsg() 
    Switch $nMsg 
     Case $GUI_EVENT_CLOSE 
      Exit 

     Case $Calculate 

      if GUICtrlRead($capacity)=0 then ExitLoop 
      if GUICtrlRead($Volt)=0 then ExitLoop 

      Switch GUICtrlRead($mAh) 
       Case $GUI_Checked ;mAh checked 

        $mAh = Int(GUICtrlRead($capacity)) 
        $Ah = ($mAh/1000) 
        $Wh = ($Ah*(GUICtrlRead($Volt))) 
        $mWh = ($Wh*1000) 
        $myval = Int($mWh)&" mWh" 

       Case Else 
      EndSwitch 

      Switch GUICtrlRead($mWh) 
       Case $GUI_Checked ;mWh checked 

        $mWh = Int(GUICtrlRead($capacity)) 
        $Wh = ($mWh/1000) 
        $Ah = ($Wh/(GUICtrlRead($Volt))) 
        $mAh = ($Ah*1000) 
        $myval = Int($mAh)&" mAh" 

       Case Else 
      EndSwitch 

        GUICtrlSetData(8, string($myval))   

    EndSwitch 
WEnd 

回答

1

而是ExitLoop的使用ContinueLoop ...你退出while循環否則這將導致腳本終止......它不會以任何GUI消息的反應更多,但將繼續下去,直到你停止繼續開放這是困難的方式,因爲GUI仍在顯示,但主循環已經完成,並且沒有其他命令可以執行。 而不是有兩個內部開關塊,你可以只使用一個if-then-else-endif ......你的收音機組只能有兩種狀態。 最後但並非最不重要的是你不應該在GUICtrlSetData函數中使用「8」作爲參考,但是$ Input2 ... 我沒有測試我的建議,它只是從讀你的代碼時看到了什麼錯誤。所以請隨時測試它,並寫下如果它嘗試我們的建議後不工作...祝你好運。

編輯: 好的,我完全檢查出來並運行你的代碼。這是我的工作結果:

#include <ButtonConstants.au3> 
#include <EditConstants.au3> 
#include <GUIConstantsEx.au3> 
#include <StaticConstants.au3> 
#include <WindowsConstants.au3> 

#Region ### START Koda GUI section ### Form=D:\Downloads\koda_2008-10-03\Forms\mWh2mAh.kxf 
$Form1 = GUICreate("mAh mWh Calculator", 336, 210, 682, 127) 

$capacity = GUICtrlCreateInput("", 224, 48, 81, 21) 

$mAhRadio = GUICtrlCreateRadio("mAh", 224, 72, 41, 17) 
GUICtrlSetState ($mAhRadio, $GUI_CHECKED) 

$mWhRadio = GUICtrlCreateRadio("mWh", 272, 72, 41, 17) 

$Volt=GUICtrlCreateInput("", 224, 96, 81, 21) 

$Calculate = GUICtrlCreateButton("Calculate", 104, 136, 75, 25) 
$Input2 = GUICtrlCreateInput("", 184, 136, 121, 21) 
GUICtrlSetState ($Input2, $GUI_DISABLE) 

$Label3 = GUICtrlCreateLabel("Calculate between milliampere-hour and millwatt-hour", 16, 16, 304, 17) 
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") 
$Label4 = GUICtrlCreateLabel("Select the appropriate radio button:", 48, 72, 170, 17) 
$Label1 = GUICtrlCreateLabel("Enter the mAh or mWh value for the battery:", 8, 48, 211, 17) 
$Label2 = GUICtrlCreateLabel("Enter the battery voltage:", 96, 96, 123, 17) 
GUISetState(@SW_SHOW) 
#EndRegion ### END Koda GUI section ### 

While True 
    Switch GUIGetMsg() 
     Case $GUI_EVENT_CLOSE 
      Exit 

     Case $Calculate 
      If GUICtrlRead($capacity) == 0 Then ContinueLoop 
      If GUICtrlRead($Volt) == 0 Then ContinueLoop 

      If GUICtrlRead($mAhRadio) == $GUI_Checked Then 
       ;mAh checked 
       $mAh = Int(GUICtrlRead($capacity)) 
       $Ah = ($mAh*1000) 
       $Wh = ($Ah*(GUICtrlRead($Volt))) 
       $mWh = ($Wh/1000) 
       $myval = Int($mWh)&" mWh" 
      Else 
       ;mWh checked 
       $mWh = Int(GUICtrlRead($capacity)) 
       $Wh = ($mWh*1000) 
       $Ah = ($Wh/(GUICtrlRead($Volt))) 
       $mAh = ($Ah/1000) 
       $myval = Int($mAh)&" mAh" 
      EndIf 
      GUICtrlSetData($Input2, string($myval))   
    EndSwitch 
WEnd 

您的問題是:您覆蓋$ mAh和$ mWh。你分配了兩次。到電臺按鈕和計算中的變量。所以我改變了這一點,並且像上面提到的那樣清理了你的代碼。它現在按預期工作。祝你好運!

順便說一句,我花了一點自由來改變你的算法。從mAh到Ah它乘以1000,對吧?不分......反之亦然。與mWh和Wh相同...