user2536262
user2536262

Reputation: 279

GUI won't close when supposed to (AutoIT)

I'm learning AutoIT and am trying to write a script for a GUI that let's the user enter a few values, hit an OK button, and then see the same values displayed again before hitting another OK button, quitting the script.

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

Example()

Func Example()
    Local $file, $btn, $msg
    Local $width1, $width2, $iOldOpt
    local $startangle, $endangle, $nomeas, $time

    GUICreate("My GUI",370,160) ; will create a dialog box that when displayed is centered
    $iOldOpt = Opt("GUICoordMode", 2)
    $width1 = 300
    $width2 = 50
    GUICtrlCreateLabel("Startvinkel i förhållande till referensriktningen (grader):", 10, 30, $width1)
    $startangle = GUICtrlCreateInput("", 0, -1, $width2)
    GUICtrlCreateLabel("Slutvinkel i förhållande till referensriktningen (grader):", -($width1+$width2), 0, $width1)
    $endangle = GUICtrlCreateInput("", 0, -1, $width2)
    GUICtrlCreateLabel("Antal mätriktningar:", -($width1+$width2), 0, $width1)
    $nomeas = GUICtrlCreateInput("", 0, -1, $width2)
    GUICtrlCreateLabel("Mättid i varje riktning (sekunder):", -($width1+$width2), 0, $width1)
    $time = GUICtrlCreateInput("", 0, -1, $width2)

    $iOldOpt = Opt("GUICoordMode", 1)
    $btn = GUICtrlCreateButton("Ok", 155, 130, 60, 20)



    GUISetState(@SW_SHOW)

    $msg = 0

    ; Loop until the user exits.
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $btn
                ExitLoop
        EndSelect
    WEnd

    GUICreate("My GUI",370,160) ; will create a dialog box that when displayed is centered
    $iOldOpt = Opt("GUICoordMode", 2)
    GUICtrlCreateLabel("Startvinkel i förhållande till referensriktningen (grader):", 10, 30, $width1)
    GUICtrlCreateLabel(GUICtrlRead($startangle), 0, -1, $width2)
    GUICtrlCreateLabel("Slutvinkel i förhållande till referensriktningen (grader):", -($width1+$width2), 0, $width1)
    GUICtrlCreateLabel(GUICtrlRead($endangle), 0, -1, $width2)
    GUICtrlCreateLabel("Antal mätriktningar:", -($width1+$width2), 0, $width1)
    GUICtrlCreateLabel(GUICtrlRead($nomeas), 0, -1, $width2)
    GUICtrlCreateLabel("Mättid i varje riktning (sekunder):", -($width1+$width2), 0, $width1)
    GUICtrlCreateLabel(GUICtrlRead($time), 0, -1, $width2)
    $iOldOpt = Opt("GUICoordMode", 1)
    $btn = GUICtrlCreateButton("Ok", 155, 130, 60, 20)


    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $btn
                ExitLoop
        EndSelect
    WEnd

    MsgBox($MB_SYSTEMMODAL, "Finished", GUICtrlRead($nomeas))
EndFunc

The script starts, displays the first GUI and let's me enter my values, but when I click the OK button nothing happens. Why is this?

This script is to some extent based on a coding exemple where clicking the OK button led you to a 'message box', as is seen in the second half of the script. What I've done is essentialy removing the MsgBox()-line and replcing it with GUICreate(). Nothing else has changed.

Upvotes: 0

Views: 1633

Answers (2)

Xenobiologist
Xenobiologist

Reputation: 2151

Something like this?

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

Example()

Func Example()
    Local $file, $btn, $msg
    Local $width1, $width2, $iOldOpt
    Local $startangle, $endangle, $nomeas, $time

    GUICreate("My GUI", 570, 260) ; will create a dialog box that when displayed is centered
    $iOldOpt = Opt("GUICoordMode", 2)
    $width1 = 300
    $width2 = 50
    GUICtrlCreateLabel("Startvinkel i förhållande till referensriktningen (grader):", 10, 30, $width1)
    $startangle = GUICtrlCreateInput("", 0, -1, $width2)
    GUICtrlCreateLabel("Slutvinkel i förhållande till referensriktningen (grader):", -($width1 + $width2), 0, $width1)
    $endangle = GUICtrlCreateInput("", 0, -1, $width2)
    GUICtrlCreateLabel("Antal mätriktningar:", -($width1 + $width2), 0, $width1)
    $nomeas = GUICtrlCreateInput("", 0, -1, $width2)
    GUICtrlCreateLabel("Mättid i varje riktning (sekunder):", -($width1 + $width2), 0, $width1)
    $time = GUICtrlCreateInput("", 0, -1, $width2)

    $iOldOpt = Opt("GUICoordMode", 1)
    $btn = GUICtrlCreateButton("Ok", 155, 130, 60, 20)

    $1 = GUICtrlCreateLabel("Startvinkel i förhållande till referensriktningen (grader):", 10, 160, 500, 20)
    $2 = GUICtrlCreateLabel("Slutvinkel i förhållande till referensriktningen (grader):", 10, 180, 500, 20)
    $3 = GUICtrlCreateLabel("Antal mätriktningar:", 10, 200, 500, 20)
    $4 = GUICtrlCreateLabel("Mättid i varje riktning (sekunder):", 10, 220, 500, 20)

    GUISetState(@SW_SHOW)

    $msg = 0

    ; Loop until the user exits.
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $btn
;~              MsgBox($MB_SYSTEMMODAL, "Finished", 'Antal mätriktningar: ' & GUICtrlRead($nomeas))

                GUICtrlSetData($1, GUICtrlRead($1) & ', ' & GUICtrlRead($nomeas))
                GUICtrlSetData($2, GUICtrlRead($2) & ', ' & GUICtrlRead($endangle))
                GUICtrlSetData($3, GUICtrlRead($3) & ', ' & GUICtrlRead($time))
                GUICtrlSetData($4, GUICtrlRead($4) & ', ' & GUICtrlRead($startangle))

                GUICtrlSetData($nomeas, '')
                GUICtrlSetData($endangle, '')
                GUICtrlSetData($time, '')
                GUICtrlSetData($startangle, '')

        EndSelect
    WEnd
EndFunc   ;==>Example

Upvotes: 1

Xenobiologist
Xenobiologist

Reputation: 2151

I guess you want this

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

Example()

Func Example()
    Local $file, $btn, $msg
    Local $width1, $width2, $iOldOpt
    Local $startangle, $endangle, $nomeas, $time

    GUICreate("My GUI", 370, 160) ; will create a dialog box that when displayed is centered
    $iOldOpt = Opt("GUICoordMode", 2)
    $width1 = 300
    $width2 = 50
    GUICtrlCreateLabel("Startvinkel i förhållande till referensriktningen (grader):", 10, 30, $width1)
    $startangle = GUICtrlCreateInput("", 0, -1, $width2)
    GUICtrlCreateLabel("Slutvinkel i förhållande till referensriktningen (grader):", -($width1 + $width2), 0, $width1)
    $endangle = GUICtrlCreateInput("", 0, -1, $width2)
    GUICtrlCreateLabel("Antal mätriktningar:", -($width1 + $width2), 0, $width1)
    $nomeas = GUICtrlCreateInput("", 0, -1, $width2)
    GUICtrlCreateLabel("Mättid i varje riktning (sekunder):", -($width1 + $width2), 0, $width1)
    $time = GUICtrlCreateInput("", 0, -1, $width2)

    $iOldOpt = Opt("GUICoordMode", 1)
    $btn = GUICtrlCreateButton("Ok", 155, 130, 60, 20)

    GUISetState(@SW_SHOW)

    $msg = 0

    ; Loop until the user exits.
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $btn
                MsgBox($MB_SYSTEMMODAL, "Finished", 'Antal mätriktningar: ' & GUICtrlRead($nomeas))
                GUICtrlSetData($nomeas, '')
                GUICtrlSetData($endangle, '')
                GUICtrlSetData($time, '')
                GUICtrlSetData($startangle, '')
        EndSelect
    WEnd
EndFunc   ;==>Example

Upvotes: 0

Related Questions