Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh a GUI using AutoIt

I am making AutoIt code and one of the items on the GUI needs to be updated every few seconds, and I can seem to get it to do it. To make it simple I have written some 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 was working then the number would change, but it does not. How do I make it refresh?

like image 589
09stephenb Avatar asked Jan 10 '23 04:01

09stephenb


2 Answers

The number is updating, but your data is not. Plus, you are mixing integers with strings.

Luckily AutoIt converts them automatically. But take care, because you will have problems in 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

P.S.: Avoid using sleep in these situations while they will pause your script.

like image 150
user2530266 Avatar answered Feb 04 '23 22:02

user2530266


This is easily done with AdLibRegister passing the function name and then 1000 milliseconds (1 second).

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
like image 42
Mr. Hargrove Avatar answered Feb 04 '23 23:02

Mr. Hargrove