【问题标题】:Refresh a GUI using AutoIt使用 AutoIt 刷新 GUI
【发布时间】:2014-09-11 05:31:03
【问题描述】:

我正在编写 AutoIt 代码,并且 GUI 上的其中一项需要每隔几秒更新一次,我似乎可以做到这一点。为了简单起见,我编写了一些代码来显示问题:

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

如果代码有效,那么数字会改变,但不会。如何让它刷新?

【问题讨论】:

    标签: user-interface autoit


    【解决方案1】:

    号码正在更新,但您的数据没有。另外,您正在将整数与字符串混合。

    幸运的是 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
    

    P.S.:避免在这些情况下使用 sleep,因为它们会暂停您的脚本。

    【讨论】:

      【解决方案2】:

      这很容易通过 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
      

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

        • 仅供参考,在这种情况下,autoit 会自动将字符串转换为整数。所以基本上 $num += "1" 会起作用 ;) 这不是一个好的脚本实践
        • 有趣的笔记,谢谢!因为我认为有一些好的做法是有原因的,所以我会坚持在添加字符串之前将它们转换为整数! ;)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        • 2014-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-09
        • 2019-11-30
        相关资源
        最近更新 更多