Damein Posted October 6, 2010 Share Posted October 6, 2010 (Didn't notice there was a specific section for GUI, sorry) Alrighty, so I have a script with a basic GUI that when I press a button I want something to happen. I can get it to work in a different script.. but in this one it doesn't work. This one works fine for me: #include <StaticConstants.au3> #include <GUIConstantsEx.au3> HotKeySet("`", "ShowGUI") Local $SearchFunction, $GoogleSearchButton GUICreate("Quick Search", 200, 130) GUICtrlCreateLabel("You are current searching: Google", 0, 0) $SearchFunction = GUICtrlCreateEdit("What would you like to search for?", -1, 18, 200, 20) $GoogleSearchButton = GUICTRLCreateButton("Submit", 60, 46, 60, 20) GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg() If $msg = $GoogleSearchButton Then MsgBox(0, "Test", "GOOGLE!") WEnd That was a test one to see if I could get it to work, and it works that whenever I press the submit button I get the MsgBox Now this one, as soon as I choose a website from the menu I start getting the MsgBox's for the buttons, as if I was pressing them. expandcollapse popup#include <StaticConstants.au3> #include <GUIConstantsEx.au3> HotKeySet("`", "ShowGUI") Local $WebSite, $GoogleSearchButton, $YahooSearchButton, $BingSearchButton, $YouTubeSearchButton, $LyricsSearchButton, $msg GUICreate("Quick Search", 200, 100) $WebsiteMenu = GUICtrlCreateMenu("Websites") $WebsiteItemGoogle = GUICtrlCreateMenuItem("Google", $WebsiteMenu) $WebsiteItemYahoo = GUICtrlCreateMenuItem("Yahoo!", $WebsiteMenu) $WebsiteItemBing = GUICtrlCreateMenuItem("Bing", $WebsiteMenu) $WebsiteItemYouTube = GUICtrlCreateMenuItem("YouTube", $WebsiteMenu) $WebsiteItemLyrics = GUICtrlCreateMenuItem("Lyrics", $WebsiteMenu) GUICtrlCreateLabel("You are current searching: " & $WebSite, 0, 0) $state = 1 While $WebSite = "" $msg = GUIGetMsg() If $msg = $WebsiteItemGoogle Then $WebSite = "Google" If $msg = $WebsiteItemYahoo Then $WebSite = "Yahoo" If $msg = $WebsiteItemBing Then $WebSite = "Bing" If $msg = $WebsiteItemYouTube Then $WebSite = "YouTube" If $msg = $WebsiteItemLyrics Then $WebSite = "Lyrics" if $msg = $GUI_EVENT_CLOSE then Exit WEnd If $WebSite = "Google" Then GUIDelete() GUICreate("Quick Search", 200, 130) $WebsiteMenu = GUICtrlCreateMenu("Websites") $WebsiteItemGoogle = GUICtrlCreateMenuItem("Google", $WebsiteMenu) $WebsiteItemYahoo = GUICtrlCreateMenuItem("Yahoo!", $WebsiteMenu) $WebsiteItemBing = GUICtrlCreateMenuItem("Bing", $WebsiteMenu) $WebsiteItemYouTube = GUICtrlCreateMenuItem("YouTube", $WebsiteMenu) $WebsiteItemLyrics = GUICtrlCreateMenuItem("Lyrics", $WebsiteMenu) GUICtrlCreateLabel("You are current searching: Google", 0, 0) $SearchFunction = GUICtrlCreateEdit("What would you like to search for?", -1, 18, 200, 20) $GoogleSearchButton = GUICTRLCreateButton("Submit", 60, 46, 60, 20) GUISetState(@SW_SHOW) EndIf If $WebSite = "Yahoo" Then GUIDelete() GUICreate("Quick Search", 200, 100) $WebsiteMenu = GUICtrlCreateMenu("Websites") $WebsiteItemGoogle = GUICtrlCreateMenuItem("Google", $WebsiteMenu) $WebsiteItemYahoo = GUICtrlCreateMenuItem("Yahoo!", $WebsiteMenu) $WebsiteItemBing = GUICtrlCreateMenuItem("Bing", $WebsiteMenu) $WebsiteItemYouTube = GUICtrlCreateMenuItem("YouTube", $WebsiteMenu) $WebsiteItemLyrics = GUICtrlCreateMenuItem("Lyrics", $WebsiteMenu) GUICtrlCreateLabel("You are current searching: Yahoo!", -1, 18, 200, 20) $YahooSearchButton = GUICTRLCreateButton("Submit", 60, 46, 60, 20) GUISetState(@SW_SHOW) EndIf If $WebSite = "Bing" Then GUIDelete() GUICreate("Quick Search", 200, 100) $WebsiteMenu = GUICtrlCreateMenu("Websites") $WebsiteItemGoogle = GUICtrlCreateMenuItem("Google", $WebsiteMenu) $WebsiteItemYahoo = GUICtrlCreateMenuItem("Yahoo!", $WebsiteMenu) $WebsiteItemBing = GUICtrlCreateMenuItem("Bing", $WebsiteMenu) $WebsiteItemYouTube = GUICtrlCreateMenuItem("YouTube", $WebsiteMenu) $WebsiteItemLyrics = GUICtrlCreateMenuItem("Lyrics", $WebsiteMenu) GUICtrlCreateLabel("You are current searching: Bing", -1, 18, 200, 20) $BingSearchButton = GUICTRLCreateButton("Submit", 60, 46, 60, 20) GUISetState(@SW_SHOW) EndIf If $WebSite = "YouTube" Then GUIDelete() GUICreate("Quick Search", 200, 100) $WebsiteMenu = GUICtrlCreateMenu("Websites") $WebsiteItemGoogle = GUICtrlCreateMenuItem("Google", $WebsiteMenu) $WebsiteItemYahoo = GUICtrlCreateMenuItem("Yahoo!", $WebsiteMenu) $WebsiteItemBing = GUICtrlCreateMenuItem("Bing", $WebsiteMenu) $WebsiteItemYouTube = GUICtrlCreateMenuItem("YouTube", $WebsiteMenu) $WebsiteItemLyrics = GUICtrlCreateMenuItem("Lyrics", $WebsiteMenu) GUICtrlCreateLabel("You are current searching: YouTube", -1, 18, 200, 20) $YouTubeSearchButton = GUICTRLCreateButton("Submit", 60, 46, 60, 20) GUISetState(@SW_SHOW) EndIf If $WebSite = "Lyrics" Then GUIDelete() GUICreate("Quick Search", 200, 100) $WebsiteMenu = GUICtrlCreateMenu("Websites") $WebsiteItemGoogle = GUICtrlCreateMenuItem("Google", $WebsiteMenu) $WebsiteItemYahoo = GUICtrlCreateMenuItem("Yahoo!", $WebsiteMenu) $WebsiteItemBing = GUICtrlCreateMenuItem("Bing", $WebsiteMenu) $WebsiteItemYouTube = GUICtrlCreateMenuItem("YouTube", $WebsiteMenu) $WebsiteItemLyrics = GUICtrlCreateMenuItem("Lyrics", $WebsiteMenu) GUICtrlCreateLabel("You are current searching: Lyrics", -1, 18, 200, 20) $LyricsSearchButton = GUICTRLCreateButton("Submit", 60, 46, 60, 20) GUISetState(@SW_SHOW) EndIf While 1 $msg = GUIGetMsg() Select Case $msg = $GoogleSearchButton MsgBox(0, "Test", "GOOGLE!") Case $msg = $YahooSearchButton MsgBox(0, "Test", "YAHOO!") Case $msg = $BingSearchButton MsgBox(0, "Test", "BING!") Case $msg = $YouTubeSearchButton MsgBox(0, "Test", "YOUTUBE!") Case $msg = $LyricsSearchButton MsgBox(0, "Test", "LYRICS!!") EndSelect WEnd Func ShowGUI() if $state = 1 Then GUISetState(@SW_SHOW) $state = 0 Else GUISetState(@SW_HIDE) $state = 1 EndIf EndFunc Not quite sure why it is.. I've been at it for a while now and can't figure it out.. :/ Thanks. Most recent sig. I made Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 6, 2010 Moderators Share Posted October 6, 2010 Damein,You get the buttons firing in the GUIGetMsg loop becasue you have not declared a value for them. Any undeclared control will fire on a return of 0, which is what you get from GUIGetMsg when nothing is happening. The solution is to set a dummy value to any controls that might not be used in the GUI but are present in the GUIGetMsg loop: Globa $WebSite, $GoogleSearchButton = 9999, $YahooSearchButton = 9999, $BingSearchButton = 9999, $YouTubeSearchButton = 9999, $LyricsSearchButton = 9999, $msgIf you ever think you might have more than 10000 controls in your script, you will have to increase the dummy value! You will also see that I changed the scope to Global - you might like to read the Variables - using Global, Local and ByRef tutorial in the Wiki to see why. And one final thing, you needed a GUISetState in that code you posted to get the first GUI to display. Please ask if you any questions. 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 Link to comment Share on other sites More sharing options...
Damein Posted October 6, 2010 Author Share Posted October 6, 2010 Hmm, I see. But why did it work in the test one using Local and not in the second one? Is it just because I used more than one Case? Most recent sig. I made Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 6, 2010 Moderators Share Posted October 6, 2010 Damein,I am not sure quite which comment you are referring to, so I will cover both: If it was the undefined control: The first code worked because you only ever use $GoogleSearchButton in your loop and it is always defined by the $GoogleSearchButton = GUICTRLCreateButton(...) line. So there is no undefined control in the loop to fire on a return of 0.If it was Global/Local: As the tutorial explains, AutoIt automatically scopes variables - in the main script all variables are Global; in functions Local. Using Local as you did will not create an error (although Au3Check will fire a "Local in Global scope" warning), but the variables will still be Global in scope regardless. Read the tutorial - I think it explains it quite well.All clear now? 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 Link to comment Share on other sites More sharing options...
Damein Posted October 6, 2010 Author Share Posted October 6, 2010 It was the undefined part, and yeah I got it Also, quick question: Now that I have that all working.. I have another problem. When I click the button I get the correct MsgBox, and then my script closes. What would be the best way to allow me to press the ` key again and start from the beginning? Thanks! I'll look into it for now, but I don't quite understand why its exiting in the first place ^^; Most recent sig. I made Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 6, 2010 Moderators Share Posted October 6, 2010 Damein, I would code the whole thing like this: expandcollapse popup#include <GUIConstantsEx.au3> Global $Website HotKeySet("`", "ShowGUI") GUICreate("Quick Search", 200, 100) $WebsiteMenu = GUICtrlCreateMenu("Websites") $WebsiteItemGoogle = GUICtrlCreateMenuItem("Google", $WebsiteMenu) $WebsiteItemYahoo = GUICtrlCreateMenuItem("Yahoo!", $WebsiteMenu) $WebsiteItemBing = GUICtrlCreateMenuItem("Bing", $WebsiteMenu) $WebsiteItemYouTube = GUICtrlCreateMenuItem("YouTube", $WebsiteMenu) $WebsiteItemLyrics = GUICtrlCreateMenuItem("Lyrics", $WebsiteMenu) $hLabel = GUICtrlCreateLabel("You are currently not searching", 0, 0, 200, 20) $SearchEdit = GUICtrlCreateEdit("What would you like to search for?", 0, 20, 200, 20) GUICtrlSetState(-1, $GUI_HIDE) ; Hide this control until we have a website to search $SearchButton = GUICtrlCreateButton("Submit", 60, 50, 60, 20) GUICtrlSetState(-1, $GUI_HIDE) ; Hide this control until we have a website to search $state = 1 ShowGUI() $sCurrWebsite = "" While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $WebsiteItemGoogle $WebSite = "Google" Case $WebsiteItemYahoo $WebSite = "Yahoo" Case $WebsiteItemBing $WebSite = "Bing" Case $WebsiteItemYouTube $WebSite = "YouTube" Case $WebsiteItemLyrics $WebSite = "Lyrics" Case $SearchButton MsgBox(0, "Test", $WebSite & "!") EndSwitch If $WebSite <> $sCurrWebsite Then ; If we do check then we get flickering as we SHOW the controls on each pass GUICtrlSetData($hLabel, "You are current searching: " & $WebSite) ; This is how you change the text of a control GUICtrlSetState($SearchEdit, $GUI_SHOW) ; We have a website, so SHOW the control GUICtrlSetState($SearchButton, $GUI_SHOW) ; We have a website, so SHOW the control $sCurrWebsite = $WebSite EndIf WEnd Func ShowGUI() If $state = 1 Then GUISetState(@SW_SHOW) $state = 0 Else GUISetState(@SW_HIDE) $state = 1 EndIf EndFunc ;==>ShowGUI As you can see it is a bit shorter than your code. This is mainly because I only ever create the GUI once whereas you were constantly deleting and recreating GUIs using essentially the same code. It also allows for just the one loop so it is easy to change the website via the menu - and also removes the problem you were having earlier. Take a while to study the code and then ask if anything is unclear about what I have done or why I have done it. 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 Link to comment Share on other sites More sharing options...
Damein Posted October 6, 2010 Author Share Posted October 6, 2010 Thank you very much Melba, sorry for the constant questions. I've finished this script finally.. the problem is I have been using AHK for so long, and the commands are labeled almost identically.. but you have to set variables so often I keep forgetting to. Thanks for your help, its much appreciated. I think I get the GUI aspect a lot more now, lol. I still don't quite get how the While's work, I get that they are the same as loops.. but for some reason they confuse me still. Thanks again. Most recent sig. I made Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 6, 2010 Moderators Share Posted October 6, 2010 Damein,sorry for the constant questionsAs long as you learn from them. What about While...WEnd loops do you not understand?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 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