我创建了这个脚本来计算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
答案 0 :(得分: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相同......