barrano247 Posted July 23, 2018 Share Posted July 23, 2018 (edited) Hello all, I'm trying to create a script that first takes an input for number of specimen, which is then used as a limit for the next task where a GUI window opens, user fills in each input, and once the ok button is clicked the user input info is used to create a .mat file via autoit. The limit I mentioned is essentially the number of times this GUI window opens/autoit makes the mat file. I tried using a for loop at first, and found that after the OKbutton function runs the For loop does not continue, meaning no more GUI windows open and the process stops early. Now I'm doing everything via functions, and for this code in particular the first window runs properly and a second window opens, but it is completely unresponsive and will not close unless I restart my computer. It seems that the second time matwin() is run the GUI doesnt respond to events anymore. Here's my (simplified) code to show exactly how I've tried to do this so far: Note: $mcount keeps track of the number of times a matfile has been created. $scount is the number of specimen, and the goal is when $mcount equals $scount the GUI window creation/mat file generation process will cease. Also, i'm new to AutoIT so keep that in mind expandcollapse popup#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.5 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <EditConstants.au3> Global $scount = InputBox ("value", "Enter # of Specimen (total)") ;stores # of specimen being imaged Run("cmd.exe") ;code to activate python in cmd/load libraries, this code works fine excluded for readability $mcount = 1 matwin() ;after python is ready, matwin() is called to start the GUI window/mat file creation process Func matwin() ;window code Opt("GUIOnEventMode", 1) ; Change to OnEvent mode Local $matwin = GUICreate('Specimen '&$mcount, 320, 520) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton") GUICtrlCreateLabel("Enter mat name without .mat extension", 10, 5) Global $matname = GUICtrlCreateInput("", 10, 25, 300, 20) GUICtrlCreateLabel("Z First", 10, 55) Global $lzF = GUICtrlCreateInput("", 10, 75, 300, 20) Global $btn = GUICtrlCreateButton("Ok", 40, 460, 60, 20) GUICtrlSetOnEvent($btn, "OKButton") GUISetState(@SW_SHOW, $matwin) While 1 Sleep(1000) WEnd EndFunc Func OKButton() If $mcount <= $scount Then $mat = GUICtrlRead($matname) $tzf = GUICtrlRead($lzF) WinActivate("py36 - python") ; this is command prompt, autoit creates a mat file in the following lines WinWaitActive("py36 - python") Send("zlist ={") Send("{ENTER}") Send("'zF' : "&$tzf&",") Send("{ENTER}") Send("}") Send("{ENTER}") Send("savemat('"&$mat&".mat', zlist)") Send("{ENTER}") MsgBox(0, "Mat File Created", "Mat file "&$mcount&" created, press OK to make next mat file") WinWaitClose("Mat File Created") $mcount = 1 + $mcount If $mcount <= $scount Then matwin() ;gui generating function is run again Else GUIDelete() Exit EndIf EndFunc Func CLOSEButton() GUIDelete() Exit EndFunc Edited July 23, 2018 by JLogan3o13 Link to comment Share on other sites More sharing options...
ternal Posted July 23, 2018 Share Posted July 23, 2018 (edited) you should not use global variables in functions as a thumbrule. I am quite new myself but I would dare to guess when it runs a second time through there there is conflict with the variables that adress your buttons since they are global. edit: you can always pass variables from function to function by using filling the variables in between parentheses (see helpfile and also checkout byref). return is also an option Kind regards, Ternal Edited July 23, 2018 by ternal barrano247 1 Link to comment Share on other sites More sharing options...
barrano247 Posted July 23, 2018 Author Share Posted July 23, 2018 Thank you JLogan, just wanted to update you all, I compiled the matwin/okbutton/closebutton functions into a seperate exe, then made a For loop in the main script like so For $mcount = 1 To $scount Run("matwin.exe") Next Once again, the first time the loop runs everything works fine, however the second GUI window is unresponsive again. Heres the code for matwin expandcollapse popupOpt("GUIOnEventMode", 1) ; Change to OnEvent mode $mcount = 1 Local $matwin = GUICreate('Enter Specimen '&$mcount&' info', 320, 520) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton") GUICtrlCreateLabel("Enter mat name without .mat extension", 10, 5) Global $matname = GUICtrlCreateInput("", 10, 25, 300, 20) GUICtrlCreateLabel("Z First", 10, 55) Global $lzF = GUICtrlCreateInput("", 10, 75, 300, 20) Global $btn = GUICtrlCreateButton("Ok", 40, 460, 60, 20) GUICtrlSetOnEvent($btn, "OKButton") GUISetState(@SW_SHOW, $matwin) While 1 Sleep(1000) WEnd Func OKButton() $mat = GUICtrlRead($matname) $tzf = GUICtrlRead($lzF) WinActivate("py36 - python") WinWaitActive("py36 - python") Send("zlist ={") Send("{ENTER}") Send("'zF' : "&$tzf&",") Send("{ENTER}") Send("}") Send("{ENTER}") Send("savemat('"&$mat&".mat', zlist)") Send("{ENTER}") $mcount = $mcount + 1 EndFunc Func CLOSEButton() GUIDelete() Exit EndFunc Link to comment Share on other sites More sharing options...
ternal Posted July 23, 2018 Share Posted July 23, 2018 are you sure it is non responsive or does it hang at winwaitactive? Link to comment Share on other sites More sharing options...
barrano247 Posted July 23, 2018 Author Share Posted July 23, 2018 8 minutes ago, ternal said: you should not use global variables in functions as a thumbrule. I am quite new myself but I would dare to guess when it runs a second time through there there is conflict with the variables that adress your buttons since they are global. edit: you can always pass variables from function to function by using filling the variables in between parentheses (see helpfile and also checkout byref). return is also an option Kind regards, Ternal Thanks so much for your quick input! I removed the global/local declarations from all of the variables to begin debugging and unfortunately I'm getting the same error. I'd think that the global declaration wouldnt have a big effect on the inputs or the ok button since the second time the function runs it should just overwrite the previous values with the new inputs, and the ok button being global wouldn't have any effect really since it should have the same functionality as before. Link to comment Share on other sites More sharing options...
barrano247 Posted July 23, 2018 Author Share Posted July 23, 2018 3 minutes ago, ternal said: are you sure it is non responsive or does it hang at winwaitactive? When the second window opens nothing happens when I click the ok button, or the close window button. Even end task from taskmanager doesnt close the window. I literally have to restart my computer to close them and each time the script runs to the second window i'm left with another invincible window lingering around. I haven't seen many examples attempting to do what I'm trying to do, perhaps there's some deeper AutoIT quirk at play here? Link to comment Share on other sites More sharing options...
ternal Posted July 23, 2018 Share Posted July 23, 2018 (edited) That is quite possible. I gave it my best shot but I am not smart enough, my apologies I would have suggested to add a msgbox just before winwaitactive but it seems as the problem is really deeper. I am clueless edit: I am not sure but I have seen people state that the forum is more readable if you dont quote people unnessecary. kind regards, and good luck, Ternal Edited July 23, 2018 by ternal barrano247 1 Link to comment Share on other sites More sharing options...
barrano247 Posted July 24, 2018 Author Share Posted July 24, 2018 Well, I was about to give up and try using python for this job, but it turns out all I needed to do is use message loop mode instead of onevent mode. Everything's working for now! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now