-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
4

1 回答 1

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 相同...

于 2013-05-12T19:34:54.893 回答