Update GUI with AutoIt

I am making AutoIt code, and one of the elements in the GUI needs to be updated every few seconds, and I can get it to do it. To make this simple, I wrote code that shows the problem:

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

If the code worked, the number would change, but it is not. How to update it?

+4
source share
3 answers

The number is updated, but your data is missing. In addition, you mix integers with strings.

Fortunately, AutoIt automatically converts them. But be careful, because you will have problems with other programming languages.

Here you go:

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

PS: Avoid using sleep in these situations while they pause your script.

+4

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

You need to set new data in the GUI using GUICtrlSetData . Just change your loop like this:

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

Note that I removed the double quotes so that AutoIt treats the values ​​as integers.

+1
source

All Articles