LordRaven Posted May 19, 2011 Posted May 19, 2011 (edited) I got a small questions about GUI that I still didnt got even doing research, or even checking the 1-2-3 AutoIT guide. How do I make an GUI interact with a function? For Example I've made 2 simple Functions and a gui form with 2 buttons, my question is, when i click that button, how do i make it call that specific function? Please I know is a stupid question with no use, but to learn i need to start with the stupid things, if you dont want to help dont reply, just ignore my topic. Functions: Func Quit() MsgBox(0,"Quiting", "Have a nice day.") Exit EndFunc Func Start() MsgBox(0,"Testing", "This is a GUI Test Message.") EndFunc GUI Code: #include <GUIConstants.au3> #Region ### START Koda GUI section ### Form= $Form2 = GUICreate("This is a Test!", 136, 108, 195, 125) $Button1 = GUICtrlCreateButton("START", 32, 16, 75, 25, 0) $Button2 = GUICtrlCreateButton("QUIT", 32, 56, 75, 25, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Thanks in advance. Edited May 19, 2011 by LordRaven
Moderators Melba23 Posted May 19, 2011 Moderators Posted May 19, 2011 LordRaven,Clicking controls creates event messages which you look for in your GUIGetMsg loop in the same way you are currently looking for the [X] to be clicked and send the $GUI_EVENT_CLOSE message. You do it like this: #include <GUIConstantsEx.au3> $Form2 = GUICreate("This is a Test!", 136, 108, 195, 125) $Button1 = GUICtrlCreateButton("START", 32, 16, 75, 25, 0) $Button2 = GUICtrlCreateButton("QUIT", 32, 56, 75, 25, 0) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 Start() Case $Button2 Quit() EndSwitch WEnd Func Quit() MsgBox(0,"Quiting", "Have a nice day.") Exit EndFunc Func Start() MsgBox(0,"Testing", "This is a GUI Test Message.") EndFuncAll clear? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
LordRaven Posted May 20, 2011 Author Posted May 20, 2011 (edited) @Melba23 Thanks for replying my topic and answering my question. Your help did make me understand more about GUI's and how they work. Now im facing a new problem. In fact I got 2 questions now, I'll ask both so I avoid spam. 1st question: How can I make a GUI form call another one? For example, the last code i posted here(the following): #include <GUIConstants.au3> #Region ### START Koda GUI section ### Form= $Form2 = GUICreate("This is a Test!", 136, 108, 195, 125) $Button1 = GUICtrlCreateButton("START", 32, 16, 75, 25, 0) $Button2 = GUICtrlCreateButton("QUIT", 32, 56, 75, 25, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd I Turned into this: #include <GUIConstants.au3> #Region ### START Koda GUI section ### Form= $Form2 = GUICreate("This is a Test!", 136, 132, 195, 125) $Button1 = GUICtrlCreateButton("START", 32, 16, 75, 25, 0) $Button2 = GUICtrlCreateButton("QUIT", 32, 56, 75, 25, 0) $Button3 = GUICtrlCreateButton("About", 32, 96, 75, 25, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd As you can see I only add 1 extra option, About. What I want is call another Form that i made in Koda as an example, I'll show you, follows the GUI code of the About form: #include <GUIConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 219, 170, 194, 302) $Label2 = GUICtrlCreateLabel("THIS IS A EXAMPLE", 56, 16, 105, 17) $Label1 = GUICtrlCreateLabel("Credits: Someone", 24, 48, 87, 17) $Label3 = GUICtrlCreateLabel("E-Mail: som-e-one@test.com", 24, 72, 139, 17) $Label4 = GUICtrlCreateLabel("Website: www.someone.com", 24, 96, 142, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd 2nd question: How do I make a inputbox inside the GUI save an attribued value to an variable? Content: - Inputbox - Apply button - Show Value button Goal: I type any number in the gui inputbox, click apply button setting that value from the box to the variable and the I click Show Value so the value will be shown to me in a common MsgBox Follows the new GUI code: #include <GUIConstants.au3> #Region ### START Koda GUI section ### Form= $Form2 = GUICreate("This is a Test!", 212, 132, 195, 125) $Button1 = GUICtrlCreateButton("START", 32, 16, 75, 25, 0) $Button2 = GUICtrlCreateButton("QUIT", 32, 56, 75, 25, 0) $Button3 = GUICtrlCreateButton("About", 32, 96, 75, 25, 0) $Input1 = GUICtrlCreateInput("Input1", 128, 16, 65, 21) $Button4 = GUICtrlCreateButton("Apply", 128, 40, 59, 17, 0) $Button5 = GUICtrlCreateButton("Show Value", 120, 96, 75, 25, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Thanks very much for any help. -- Edited -- @Melba23 Also i want to know how do i replace those 2 buttons(Start and Quit) per checkboxes like |_| Start |_| Quit If i click start the checkbox gets checked and it goes to a function, if i check the Quit for example it "uncheck the Start box" and check the Quit one and after 10 secs quit. My point is, i made 2 checkboxes in Koda but when i check the 1st everything is good, but when i check the 2nd the first stills marked, i just want one choice not both. Edited May 20, 2011 by LordRaven
martin Posted May 20, 2011 Posted May 20, 2011 expandcollapse popup#include <GUIConstants.au3> #region ### START Koda GUI section ### Form= $Form2 = GUICreate("This is a Test!", 222, 172, 195, 125) $Button1 = GUICtrlCreateButton("START", 32, 16, 75, 25, 0) $Button2 = GUICtrlCreateButton("QUIT", 32, 56, 75, 25, 0) $Button3 = GUICtrlCreateButton("About", 32, 96, 75, 25, 0) $ed1 = GUICtrlCreateInput("",32,135,100) $BtnSet = GUICtrlCreateButton("Set",160,135) GUISetState(@SW_SHOW) #endregion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button3 GUISetState(@SW_DISABLE,$Form2) _About() GUISetState(@SW_ENABLE,$Form2) winactivate($Form2) case $BtnSet msgbox(0,"contents = ",GUICtrlRead($ed1),0,$Form2) EndSwitch WEnd Func _About() Local $Form1, $Label2, $label1, $label3, $label4 #region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 219, 170, 194, 302) $Label2 = GUICtrlCreateLabel("THIS IS A EXAMPLE", 56, 16, 105, 17) $label1 = GUICtrlCreateLabel("Credits: Someone", 24, 48, 87, 17) $label3 = GUICtrlCreateLabel("E-Mail: som-e-one@test.com", 24, 72, 139, 17) $label4 = GUICtrlCreateLabel("Website: www.someone.com", 24, 96, 142, 17) GUISetState(@SW_SHOW) #endregion ### END Koda GUI section ### Local $AllDone = False While Not $AllDone $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE $AllDone = True EndSwitch WEnd GUIDelete($Form1) EndFunc ;==>_About For the check boxes you need to look in the help for GuiCtrlSteStae. In fact I think you need to spend a lot more time in the help and trying the examples because all the questions you have asked so far are answered in the help. Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
LordRaven Posted May 20, 2011 Author Posted May 20, 2011 (edited) Thanks for the previous help. And sorry mate, I'll pay more attention to the help file content. Im facing another issue here. Im working on some kind of shouter wich should send the same sentence like 5 times each 20 secs(ill work on the details of it later) but for start im getting a small problem here. My problem is, theres a button specific to trigger the function but when i click again the same button to stop the function nothing happen, the function still on and sending that 'X'... Weird thing is, this code works fine using HotKeySet, but wont work for a reason with a GUI button. Follows the Code: ;GUI's While looping While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $Button1 KeyPress() EndSwitch WEnd ;Function called by the button click event Func KeyPress() Local $KPUnPaused $KPUnPaused = NOT $KPUnPaused While $KPUnPaused ToolTip('KeyPresser is "ON"',500,8,"",0,2) Send("X") WEnd ToolTip('KeyPresser is "OFF"',500,8,"",0,2) Sleep(2000) EndFunc Edited May 20, 2011 by LordRaven
Moderators Melba23 Posted May 20, 2011 Moderators Posted May 20, 2011 LordRaven,Im working on some kind of shouter wich should send the same sentence like 5 times each 20 secsBefore we go any further, would you care to clarify what exactly this "shouter" is and why you need to create it? We do not help create spammers - which is my interpretation of your phrase above. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
LordRaven Posted May 20, 2011 Author Posted May 20, 2011 It is not a kind of spammer it would be more like an Advisor to show time to time a defined message. Its more like a countdown timer with a message. If it was for spamming I would have don already. I could've solve this by making 2 buttons Active and Desactive, but I want a single On/Off button which is the right way to do, saving most lines we can. Thats it, an advisor for some random message time to time by determined times, for now idk really for what I'll use for, so far im still doing for knowledge, not because of futile things like spamming. Thanks Melba23
Moderators Melba23 Posted May 20, 2011 Moderators Posted May 20, 2011 LordRaven, an Advisor to show time to time a defined messageOK, my apologies for having doubted your intentions. One of the problems in your script was that you gave the $KPUnPaused variable Local scope so it was recreated each time you enter the function - you need it in Global scope so that it retained its value. You might like to take a look at the beginning of the Variables - using Global, Local and ByRef tutorial in the Wiki - I wrote it to help people understand the difference. Another problem was the While...WEnd loop within the KeyPress function - there was no way to escape from it as you could never change the value of KPUnPaused in the function - you need to look for the flag outside the function. ;( Here is a much modified version of your scipt which I think does what you want: expandcollapse popup#include <GUIConstantsEx.au3> ; Set flags Global $KPUnPaused = False ; Are we running? Global $iBegin = 0 ; A timer to run after a defined time $hGUI = GUICreate("Test", 500, 500) $Button1 = GUICtrlCreateButton("Start", 10, 10, 80, 30) GUISetState() ;GUI looping While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 KeyPress() EndSwitch ; If the falg is set and the timer is over the required delay If $KPUnPaused And TimerDiff($iBegin) > 2000 Then ; Do it ConsoleWrite("Doing things every 2 secs - " & @SEC & @CRLF) ; Reset the timer $iBegin = TimerInit() EndIf WEnd ;Function called by the button click event Func KeyPress() ; Toggle the flag $KPUnPaused = NOT $KPUnPaused ; Set the tooltip and button text depending on the state If $KPUnPaused Then ToolTip('KeyPresser is "ON"',500,8,"",0,2) GUICtrlSetData($Button1, "Stop") Else ToolTip('KeyPresser is "OFF"',500,8,"",0,2) GUICtrlSetData($Button1, "Start") EndIf EndFunc All clear? Ask if not. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
LordRaven Posted May 20, 2011 Author Posted May 20, 2011 @Melba23 Thanks for replying me, I've tried your changes and they worked just as I expected. Related to the code I didnt understand one process, the following: If $KPUnPaused And TimerDiff($iBegin) > 2000 Then ; Do it ConsoleWrite("Doing things every 2 secs - " & @SEC & @CRLF) ; Reset the timer $iBegin = TimerInit() EndIf What this suppose to do exactly? I know the KPUnPause is the control var to check if the keypressing process is true or false, in other words, on/off, but what about the timerdiff and that var called iBegin, this part I didnt got right;
Moderators Melba23 Posted May 20, 2011 Moderators Posted May 20, 2011 LordRaven,Using TimerInit and TimerDiff gives you a simple, but very useful, way of determining if a period of time has passed. $iBegin = TimerInit sets the variable $iBegin to a timestamp value of the instant it is called in the script. TimerDiff calculates the difference (surprise, surprise!) between the value stored in the variable and the instant TimerDiff is called in the script. To help us mere humans, it converts the difference into milliseconds - this script shows what is going on: HotKeySet("{ESC}", "On_Exit") Func On_Exit() Exit EndFunc $iBegin = TimerInit() ConsoleWrite("Initial Timestamp = " & $iBegin & @CRLF) While 1 ConsoleWrite("Current Timestamp = " & TimerInit() & " --- Difference = " & TimerDiff($iBegin) & @CRLF) Sleep(1000) WEndAs you can see the TimerDiff return always increases by about 1000 - the slight variations are quite normal as the code takes a slightly different time to run each pass through the loop depending on what is going on in your machine. All clear? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
LordRaven Posted May 20, 2011 Author Posted May 20, 2011 So basically when you use TimerDiff($iBegin) the iBegin var value is not 0 anymore, the value changes due the count to 2000 in that example? So when the count reach 2000 milliseconds the ConsoleWrite("Doing things every 2 secs - " & @SEC & @CRLF) or whatever other command, like even a msgbox(0,"Hello!") is executed, and next the TimerInit brings the var $iBegin again to 0 again, so in the next loop of that while the process starts all over again? Is something like that?(sorry if i sound confusing)
Moderators Melba23 Posted May 20, 2011 Moderators Posted May 20, 2011 LordRaven,When I run the test script I posted above I get this as output:Initial Timestamp = 257728357205 Current Timestamp = 257728358244 --- Difference = 0.0828317565500643 Current Timestamp = 257742698897 --- Difference = 1001.66829862455 Current Timestamp = 257757017774 --- Difference = 2001.71704783709As it shows, TimerInit sets the variable to 257728357205 when it is first used - this is the timestamp for that instant.When we go through the loop, TimerInit returns gradually increasing values for the current timestamp - not surprising as the time is increasing. TimerDiff returns the difference between the timestamp at the instant it is called and the stored variable used as a parameter - converted to milliseconds as the code shows.In the original script I reset the value of $iBegin each time we "did" something so that we kept looking for a 2000ms difference to "do" it again:; If the flag is set and the timer is over the required delay If $KPUnPaused And TimerDiff($iBegin) > 2000 Then ; Do it ConsoleWrite("Doing things every 2 secs - " & @SEC & @CRLF) ; Reset the timer $iBegin = TimerInit() EndIfIs that clearer? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
LordRaven Posted May 20, 2011 Author Posted May 20, 2011 Oh now i got it, the difftime is the diference of the initial value, which is the 0 attribued to the iBegin in global declaration and the time its called in that if.. for example we got a 0 value that was setted to our iBegin var, the difftime will look for 2000milliseconds forth this, once it finds the 2000ms point, it execute what we want to do, and then ... as you said the TimerInit() sets a new Timestamp as you show to roll another 2000ms. Now I finally got it. Clearly I need to read more the help files Thank you very much for your patience and help Melba23, your examples were very enlightening. Have a nice day.
Moderators Melba23 Posted May 20, 2011 Moderators Posted May 20, 2011 LordRaven,My pleasure. Clearly I need to read more the help filesAlways a good idea! M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
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