Jump to content

Repeat loop function when click button


Recommended Posts

I have issue with below code,

1. when the condition is 'PASS' and i click 'OK' button it will logs all detail into notepad. but if the condition is 'FAIL' it unable to run the notepad. can't think of why.

2. How do i close the GUI and repeat the function 'Dataentry ()' when the condition is 'PASS' or 'FAIL' infinite loop, until i close it with a button like 'Log out'?

thanks

Func Dataentry ()

Local $Input1 = "", $Input2 = "", $Input3 ="", $Input4 =""

$hWnd = GUICreate("Data Entry", 400, 400)

$GuiStyles = GuiGetStyle ($hWnd)

$font="Arial Unicode MS"

$n = GUICtrlCreatePic($Install_pic1, 25, 35, 350, 25)
GUICtrlCreateLabel("Input Measurement!" & @crlf & "Click OK when complete", 65, 300)
GUICtrlCreateLabel ("Welcome " & $empid, 300,10)
;GUICtrlSetColor(-1,0xff0000)
GUICtrlSetFont (3,8, 8000, 2, $font)



$Button_1 = GUICtrlCreateButton("&OK", 175,350,60,35)
GUICtrlCreateLabel("Scan PCBA Serial Number",10,30,140,20)
$Input4 = GUICtrlCreateInput("" ,140,30,80,24)
GUICtrlCreateLabel("Resistance Reading",10,100,140,20)
$Input1 = GUICtrlCreateInput("" ,140,100,80,24)
GUICtrlCreateLabel("DC Voltage Reading",10,150,140,20)
$Input2 = GUICtrlCreateInput("" ,140,150,80,24)
GUICtrlCreateLabel("AC Voltage Reading",10,200,140,20)
$Input3 = GUICtrlCreateInput("" ,140,200,70,24)
GUISetState () ; will display an empty dialog box

; Run the GUI until the dialog is closed
$msg = 0
While $msg <> $GUI_EVENT_CLOSE
$msg = GUIGetMsg()
Select
Case $msg = $Button_1
ExitLoop

EndSelect
WEnd
$res = GUICtrlRead ($Input1, 1)
$VDC = GUICtrlRead ($Input2, 1)
$VAC = GUICtrlRead ($Input3, 1)
$Scanserial = GUICtrlRead ($Input4, 1)
; GUIDelete ($hWnd)
EndFunc

func Condition ()
Local $Result1 = "", $Result2 = "", $Result3 = ""

if $res > 975 Then
$Result1 = "Pass"
Else
$Result1 = "Fail"
EndIf

If $VDC < 0.060 Then
$Result2 = "Pass"
Else
$Result2 = "Fail"
EndIf

If $VAC < 0.060 Then
$Result3 = "Pass"
Else
$Result3 = "Fail"
EndIf


$pf = GUICreate("STATUS - " & $ScanSerial, 250, 180)

$GuiStyles = GuiGetStyle ($pf)

$font="Arial Unicode MS"
If ($Result1 = "Pass") Then
If ($Result2 = "Pass") Then
If ($Result3 = "Pass") Then
GUICtrlCreateLabel("PASS", 35, 5,180,80,-1)
GUICtrlSetColor(-1,0x00ff00)
GUICtrlSetFont (-1,50, 200, 1, $font)
$Button_4 = GUICtrlCreateButton("&OK", 90,120,60,35)
GUISetState () ; will display an empty dialog box

; Run the GUI until the dialog is closed

$EndResult = "PASS"
Else

EndIf
EndIf

Else

EndIf

; While 1
; $msg = GUIGetMsg()
; Select
; Case $msg = $GUI_EVENT_CLOSE then ExitLoop
; Case $msg = $Button_4 then Exit
; EndSelect
; WEnd


;Condition ()

If $EndResult = "PASS" Then

Else
$EndResult = "FAIL"
GUICtrlCreateLabel("FAIL", 35, 5,180,80,-1)
GUICtrlSetColor(-1,0xff0000)
GUICtrlSetFont (-1,50, 200, 1, $font)
EndIf
$Button_2 = GUICtrlCreateButton("&OK", 90,120,60,35)

GUISetState () ; will display an empty dialog box

; Run the GUI until the dialog is closed
; While 1
; $msg = GUIGetMsg()
; If $msg = $Button_1 Then Exit
; If $msg = $Button_4 then Exit
; If $msg = $GUI_EVENT_CLOSE Then ExitLoop
; Wend

$msg = 0
While $msg <> $GUI_EVENT_CLOSE
$msg = GUIGetMsg()
Select
Case $msg = $Button_2
ExitLoop
Case $msg = $Button_4
Exit

EndSelect
WEnd

EndFunc

func savelog ()

 ;  $text = WinGetText("Notepad","")
 ;  $Gettext = stringsplit($text,chr(10))

   Run("notepad.exe", "", @SW_MAXIMIZE)
   WinWaitActive("Untitled - Notepad")  
   sleep(500)
   send ($StartDateTime & "|" & $empid & "|" & $FixtureID & "{ENTER}")
   sleep (100)
   send ("{ENTER}")
   sleep (100)
   send ($Scanserial & "{ENTER}")
   sleep (100)
   send ("{ENTER}")
   sleep (100)
   send ($res & "|" & $VDC & "|" & $VAC & "{ENTER}")
   Send("!f")
   Send("a")
   WinWaitActive("Save As")
   Send($file_path & $StartDateTime & "_" & $Scanserial & ".txt")
   Send("{ENTER}")
   sleep (300)
   Send("!f")
   sleep (50)
   Send("x")


dataentry () 
EndFunc
Edited by stevensys
Link to comment
Share on other sites

Haven't checked the complete code but I see you create a second GUI. Maybe this is the source of your problem. Did you read the wiki article about multiple GUIs?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

stevensys,

Take a look at this - I think it does what you want: ;)

#include <GUIConstantsEx.au3>

Global $res, $VDC, $VAC, $empid
$Install_pic1 = ""
$Scanserial= "fred"

While 1
    ; Get data
    Dataentry()
    ; Check results
    Condition()
    ; Loop
WEnd


Func Dataentry()

    Local $Input1, $Input2, $Input3, $Input4 ; Already set to empty

    $hWnd = GUICreate("Data Entry", 400, 400)

    $font = "Arial Unicode MS"

    $n = GUICtrlCreatePic($Install_pic1, 25, 35, 350, 25)
    GUICtrlCreateLabel("Input Measurement!" & @CRLF & "Click OK when complete", 65, 300)
    GUICtrlCreateLabel("Welcome " & $empid, 300, 10)
    GUICtrlSetFont(3, 8, 8000, 2, $font)

    $Button_1 = GUICtrlCreateButton("&OK", 175, 350, 60, 35)
    GUICtrlCreateLabel("Scan PCBA Serial Number", 10, 30, 140, 20)
    $Input4 = GUICtrlCreateInput("", 140, 30, 80, 24)
    GUICtrlCreateLabel("Resistance Reading", 10, 100, 140, 20)
    $Input1 = GUICtrlCreateInput("", 140, 100, 80, 24)
    GUICtrlCreateLabel("DC Voltage Reading", 10, 150, 140, 20)
    $Input2 = GUICtrlCreateInput("", 140, 150, 80, 24)
    GUICtrlCreateLabel("AC Voltage Reading", 10, 200, 140, 20)
    $Input3 = GUICtrlCreateInput("", 140, 200, 70, 24)
    GUISetState()

    ; Run the GUI until the dialog is closed
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE  ; Exit the script completely
                Exit
            Case $Button_1 ; Exit the loop so we continue
                ExitLoop
            EndSwitch
    WEnd
    $res = GUICtrlRead($Input1, 1)
    $VDC = GUICtrlRead($Input2, 1)
    $VAC = GUICtrlRead($Input3, 1)
    $Scanserial = GUICtrlRead($Input4, 1)
    GUIDelete($hWnd)
EndFunc   ;==>Dataentry

Func Condition()
    Local $Result1, $Result2, $Result3, $EndResult  ; Already set to empty

    If $res > 975 Then
        $Result1 = "Pass"
    Else
        $Result1 = "Fail"
    EndIf

    If $VDC < 0.060 Then
        $Result2 = "Pass"
    Else
        $Result2 = "Fail"
    EndIf

    If $VAC < 0.060 Then
        $Result3 = "Pass"
    Else
        $Result3 = "Fail"
    EndIf

    $pf = GUICreate("STATUS - " & $Scanserial, 250, 180)

    $font = "Arial Unicode MS"

    If ($Result1 = "Pass") And ($Result2 = "Pass") And ($Result3 = "Pass") Then
        GUICtrlCreateLabel("PASS", 35, 5, 180, 80, -1)
        GUICtrlSetColor(-1, 0x00ff00)
        GUICtrlSetFont(-1, 50, 200, 1, $font)
        $EndResult = "PASS"
    Else
        GUICtrlCreateLabel("FAIL", 35, 5, 180, 80, -1)
        GUICtrlSetColor(-1, 0xff0000)
        GUICtrlSetFont(-1, 50, 200, 1, $font)
        $EndResult = "FAIL"
    EndIf

    $Button_2 = GUICtrlCreateButton("&OK", 90, 120, 60, 35)
    $Button_4 = GUICtrlCreateButton("&Exit", 90, 150, 60, 35) ; Add exit button

    GUISetState() ; will display an empty dialog box

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $Button_4 ; Exit the script completely 
                Exit
            Case $Button_2 ; Run the log
                GUIDelete($pf)
                savelog()
                Return
        EndSwitch
    WEnd

EndFunc   ;==>Condition

Func savelog()

    ; Simulate the log write
    MsgBox(0, "Saving", "NotePad opens here")
    Return

    ;  $text = WinGetText("Notepad","")
    ;  $Gettext = stringsplit($text,chr(10))

#cs
    Run("notepad.exe", "", @SW_MAXIMIZE)
    WinWaitActive("Untitled - Notepad")
    Sleep(500)
    Send($StartDateTime & "|" & $empid & "|" & $FixtureID & "{ENTER}")
    Sleep(100)
    Send("{ENTER}")
    Sleep(100)
    Send($Scanserial & "{ENTER}")
    Sleep(100)
    Send("{ENTER}")
    Sleep(100)
    Send($res & "|" & $VDC & "|" & $VAC & "{ENTER}")
    Send("!f")
    Send("a")
    WinWaitActive("Save As")
    Send($file_path & $StartDateTime & "_" & $Scanserial & ".txt")
    Send("{ENTER}")
    Sleep(300)
    Send("!f")
    Sleep(50)
    Send("x")

#ce

EndFunc   ;==>savelog

Please ask if you have any questions, but as I am just off to play a round of golf you will not get any answers (from me) until I return this evening. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

stevensys,

Take a look at this - I think it does what you want: ;)

#include <GUIConstantsEx.au3>

Global $res, $VDC, $VAC, $empid
$Install_pic1 = ""
$Scanserial= "fred"

While 1
; Get data
Dataentry()
; Check results
Condition()
; Loop
WEnd


Func Dataentry()

Local $Input1, $Input2, $Input3, $Input4 ; Already set to empty

$hWnd = GUICreate("Data Entry", 400, 400)

$font = "Arial Unicode MS"

$n = GUICtrlCreatePic($Install_pic1, 25, 35, 350, 25)
GUICtrlCreateLabel("Input Measurement!" & @CRLF & "Click OK when complete", 65, 300)
GUICtrlCreateLabel("Welcome " & $empid, 300, 10)
GUICtrlSetFont(3, 8, 8000, 2, $font)

$Button_1 = GUICtrlCreateButton("&OK", 175, 350, 60, 35)
GUICtrlCreateLabel("Scan PCBA Serial Number", 10, 30, 140, 20)
$Input4 = GUICtrlCreateInput("", 140, 30, 80, 24)
GUICtrlCreateLabel("Resistance Reading", 10, 100, 140, 20)
$Input1 = GUICtrlCreateInput("", 140, 100, 80, 24)
GUICtrlCreateLabel("DC Voltage Reading", 10, 150, 140, 20)
$Input2 = GUICtrlCreateInput("", 140, 150, 80, 24)
GUICtrlCreateLabel("AC Voltage Reading", 10, 200, 140, 20)
$Input3 = GUICtrlCreateInput("", 140, 200, 70, 24)
GUISetState()

; Run the GUI until the dialog is closed
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE ; Exit the script completely
Exit
Case $Button_1 ; Exit the loop so we continue
ExitLoop
EndSwitch
WEnd
$res = GUICtrlRead($Input1, 1)
$VDC = GUICtrlRead($Input2, 1)
$VAC = GUICtrlRead($Input3, 1)
$Scanserial = GUICtrlRead($Input4, 1)
GUIDelete($hWnd)
EndFunc ;==>Dataentry

Func Condition()
Local $Result1, $Result2, $Result3, $EndResult ; Already set to empty

If $res > 975 Then
$Result1 = "Pass"
Else
$Result1 = "Fail"
EndIf

If $VDC < 0.060 Then
$Result2 = "Pass"
Else
$Result2 = "Fail"
EndIf

If $VAC < 0.060 Then
$Result3 = "Pass"
Else
$Result3 = "Fail"
EndIf

$pf = GUICreate("STATUS - " & $Scanserial, 250, 180)

$font = "Arial Unicode MS"

If ($Result1 = "Pass") And ($Result2 = "Pass") And ($Result3 = "Pass") Then
GUICtrlCreateLabel("PASS", 35, 5, 180, 80, -1)
GUICtrlSetColor(-1, 0x00ff00)
GUICtrlSetFont(-1, 50, 200, 1, $font)
$EndResult = "PASS"
Else
GUICtrlCreateLabel("FAIL", 35, 5, 180, 80, -1)
GUICtrlSetColor(-1, 0xff0000)
GUICtrlSetFont(-1, 50, 200, 1, $font)
$EndResult = "FAIL"
EndIf

$Button_2 = GUICtrlCreateButton("&OK", 90, 120, 60, 35)
$Button_4 = GUICtrlCreateButton("&Exit", 90, 150, 60, 35) ; Add exit button

GUISetState() ; will display an empty dialog box

While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE, $Button_4 ; Exit the script completely
Exit
Case $Button_2 ; Run the log
GUIDelete($pf)
savelog()
Return
EndSwitch
WEnd

EndFunc ;==>Condition

Func savelog()

; Simulate the log write
MsgBox(0, "Saving", "NotePad opens here")
Return

; $text = WinGetText("Notepad","")
; $Gettext = stringsplit($text,chr(10))

#cs
Run("notepad.exe", "", @SW_MAXIMIZE)
WinWaitActive("Untitled - Notepad")
Sleep(500)
Send($StartDateTime & "|" & $empid & "|" & $FixtureID & "{ENTER}")
Sleep(100)
Send("{ENTER}")
Sleep(100)
Send($Scanserial & "{ENTER}")
Sleep(100)
Send("{ENTER}")
Sleep(100)
Send($res & "|" & $VDC & "|" & $VAC & "{ENTER}")
Send("!f")
Send("a")
WinWaitActive("Save As")
Send($file_path & $StartDateTime & "_" & $Scanserial & ".txt")
Send("{ENTER}")
Sleep(300)
Send("!f")
Sleep(50)
Send("x")

#ce

EndFunc ;==>savelog

Please ask if you have any questions, but as I am just off to play a round of golf you will not get any answers (from me) until I return this evening. :D

M23

hmm.... i think you're awesome.
Link to comment
Share on other sites

i've done some reading, the 'return' keyword and can't understand.

from help file

Use the Return keyword to exit the function. Unlike built-in functions, user-defined functions return 0 unless another return value is specified.

can help me to understand the return keyword in while loop?

Link to comment
Share on other sites

Based on your questions I think you need to do some more reading for a - at least basic - understanding of how AutoIt works.

I would suggest you start with the tutorials in the wiki.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

stevensys,

I presume you mean this section:

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $Button_4 ; Exit the script completely
            Exit
        Case $Button_2 ; Run the log
            ; Delete the GUI
            GUIDelete($pf)
            ; Run the function
            savelog()
            ; Return from the function to the place where it was called
            Return
    EndSwitch
WEnd

As you can see the Return command returns immediately from the function to the place where it was called - the initial While...WEnd loop in this case. In this script you could also use ExitLoop here - then the function would just end normally and return to the same point - but that is not always true as you might wish to miss out some later code. ;)

Does that help? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...