询问Autoit中的Radio

Ask about Radio in Autoit

如何将 $Day 放入 $Radio1,$Week 放入 $Radio2,$Month 放入 $Radio3

并且 - $Radio1,$Radio2,$Radio3 进入 $RadioCheck ?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
GUICreate("Example", 143, 103, 192, 124)
GUISetFont(12, 400, 0, "Open Sans")
$Radio1 = GUICtrlCreateRadio($Day, 24, 16, 113, 17)
$Radio2 = GUICtrlCreateRadio($Week, 24, 40, 113, 17)
$Radio3 = GUICtrlCreateRadio($Month, 24, 64, 113, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $RadioCheck = $Radio1,$Radio2,$Radio3 ; The problem is here
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
Func Example1()
    Local $Day, $Week, $Month
    $LimitTime =  _DateDiff('s', "1970/01/01 00:00:00", _NowCalc()) ;Get the current time (convert to seconds)
    $Day = 86400 ; total seconds a Day
    $Week = 604800 ; total seconds a Week
    $Month = 2592000 ; total seconds a Month
   _Example2($Example3, $Example4, $Example5-$RadioCheck)
EndFunc

您的 $Day$Week$Monthlocal 变量(无法从全局范围访问)。更重要的是:您试图在声明或初始化这些变量之前设置这些变量。

那你是怎么解决的呢?你有(至少)三种选择:

选项 1

将局部变量从函数 Example1() 更改为全局范围。

  • 所以改变:Local $Day, $Week, $MonthGlobal $Day, $Week, $Month
  • 将其放在脚本的顶部,然后
  • 在创建 GUI 之前调用函数Example1()

这可能是最简单但最肮脏的方法,因为任何函数都可能随时更改变量的数据。一般尽量不要使用全局变量。

选项 2

将您的无线电控件更改为全局变量,然后在您的 Example1() 函数中更改它们的文本。像这样:

$Radio1 = GUICtrlCreateRadio($Day, 24, 16, 113, 17)
$Radio2 = ...

Global $Radio1 = GUICtrlCreateRadio("", 24, 16, 113, 17)
Global $Radio2 = GUICtrlCreateRadio("", 24, 40, 113, 17)
Global $Radio3 = GUICtrlCreateRadio("", 24, 64, 113, 17)

请注意,您必须删除未声明的变量!然后使用以下方法更改 Example1() 函数中控件的文本:

GUICtrlSetData($Radio1, $Day)
GUICtrlSetData($Radio2, $Month)
GUICtrlSetData($Radio3, $Week)

选项 3a

这是最安全的方式。让 Example1() return 变量,或者 ByRef,或者创建和 return 一个数组。作为数组(未经测试):

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; We are now going to receive the return values (in this case an array) from our function:
Local $aDate = Example1() ; it is essential to call this function before we want to make use of $Date[0] to $Date[2].
; What happens is that the function (code block further down) named Example1() is being executed.
; This function will then RETURN us the array.
; Since Example1() returns an array, $aData will automatically become an array filled with the data of Example1()

#Region ### START Koda GUI section ### Form=
GUICreate("Example", 143, 103, 192, 124)
GUISetFont(12, 400, 0, "Open Sans")
$Radio1 = GUICtrlCreateRadio($aDate[0], 24, 16, 113, 17) ; we are now setting the data for the three controls returned by Example1()
$Radio2 = GUICtrlCreateRadio($aDate[1], 24, 40, 113, 17)
$Radio3 = GUICtrlCreateRadio($aDate[2], 24, 64, 113, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
; Local $RadioCheck = $Radio1,$Radio2,$Radio3 ; The problem is here ---- You do not need that and PLEASE READ THE ABOUT LOCAL AND GLOBAL VARIABLES!

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func Example1()
    Local $aReturn[3] ;create a (Local) Array
    $LimitTime =_DateDiff('s', "1970/01/01 00:00:00", _NowCalc()) ; Get the current time (convert to seconds)
    $aReturn[0] = 86400
    $aReturn[1] = 604800
    $aReturn[2] = 2592000
    Return $aReturn ; return the Array
EndFunc

选项 3b

您还可以 return 值 ByRef :

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $Day, $Week, $Month ; we have to declare the variables for our Example1() function BEFORE we use them
Example1($Day, $Week, $Month) ; we here call our function with the previously declared variables as parameter.
; The function will then fill in the data into our variables before we use them to set the radio text

GUICreate("Example", 143, 103, 192, 124)
GUISetFont(12, 400, 0, "Open Sans")
$Radio1 = GUICtrlCreateRadio($Day, 24, 16, 113, 17) ; set the data for the three controls
$Radio2 = GUICtrlCreateRadio($Week, 24, 40, 113, 17)
$Radio3 = GUICtrlCreateRadio($Month, 24, 64, 113, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func Example1(ByRef $Day, ByRef $Week, ByRef $Month) ; NOTE: ByRef means that the given variables will be OVERWRITTEN, that also means that the variables MUST EXIST before the function is called
    $LimitTime =  _DateDiff('s', "1970/01/01 00:00:00", _NowCalc()) ; Get the current time (convert to seconds)
    $Day = 86400
    $Week = 604800
    $Month = 2592000
EndFunc

来源