使用AutoIt刷新GUI



我正在制作AutoIt代码,GUI上的一个项目需要每隔几秒钟更新一次,我似乎可以让它做到这一点。为了简单起见,我写了一些代码来显示这个问题:

$num = 0
GUICreate("Example")
$Pic1 = GUICtrlCreateLabel($num, 10, 10)
GUISetState()
While 1
   sleep(1000)
   $num = $num + "1"
WEnd

如果代码正常工作,那么数字会改变,但它没有。我怎么让它刷新?

号码在更新,但你的数据没有更新。另外,你把整数和字符串混在一起了。

幸运的是AutoIt会自动转换它们。但是要小心,因为你在使用其他编程语言时会遇到问题。

给你:

Local $num = 0
Local $hGUI = GUICreate("Example")
Local $Pic1 = GUICtrlCreateLabel($num, 10, 10)
GUISetState()
Local $hTimer = TimerInit()
While 1
    ;sleep(1000)
    If TimerDiff($hTimer) > 1000 Then
        $num += 1
        GUICtrlSetData($Pic1, $num)
        $hTimer = TimerInit()
    EndIf
    If GUIGetMsg() = -3 Then ExitLoop
WEnd

注::在这些情况下避免使用睡眠,因为他们会暂停你的脚本。

这很容易做到,AdLibRegister传递函数名,然后1000毫秒(1秒)。

Local $num = 0
Local $hGUI = GUICreate("Example")
Local $Pic1 = GUICtrlCreateLabel($num, 10, 10)
Local $msg
GUISetState()
AdLibRegister("addOne", 1000)
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
Func addOne ()
    $num += 1
    GUICtrlSetData($Pic1, $num)
EndFunc

您需要使用GUICtrlSetData在GUI中设置新数据。只需像这样修改循环:

While 1
   sleep(1000)
   $num += 1
   GUICtrlSetData($Pic1, $num)
WEnd

请注意,我删除了双引号,以便AutoIt将值作为整数处理。

最新更新