Jump to content

Button problem


Go to solution Solved by DatMCEyeBall,

Recommended Posts

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 310, 437, 345, 194)
$Button2 = GUICtrlCreateButton("Pauza", 80, 128, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $fPaused = False
HotKeySet("{p}","_Pause")
Func _Pause()
    $fPaused = Not $fPaused
    if not $fPaused then
        Tooltip("Program is running",0,0)
        GUICtrlSetData($Button2,"Pause")
    else
        Tooltip("Program is pause.",0,0)
        GUICtrlSetData($Button2,"Run")
    endif
    While $fPaused
        Sleep(100)
    WEnd
EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button2
            _Pause()

    EndSwitch
WEnd

Look for this script. If i click key 'p' all works (look tooltip). But why it doesn't works with button?

Link to comment
Share on other sites

You can change the while loop in the _Pause() function to look like this:

While $fPaused
        If GuiGetMsg() = $Button2 Then
            _Pause()
        EndIf
    WEnd

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

Ok it works, but now if i put a exit button and its looks like that:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 310, 437, 345, 194)
$Button2 = GUICtrlCreateButton("Pauza", 80, 128, 75, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Exit", 80, 50, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $fPaused = False
HotKeySet("{p}","_Pause")
Func _Pause()
    $fPaused = Not $fPaused
    if not $fPaused then
        Tooltip("Program is running",0,0)
        GUICtrlSetData($Button2,"Pause")
    else
        Tooltip("Program is pause.",0,0)
        GUICtrlSetData($Button2,"Run")
    endif
    While $fPaused
        If GuiGetMsg() = $Button2 Then
            _Pause()
        EndIf
    WEnd
EndFunc

Func _Exit()
    Exit
EndFunc
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button2
            _Pause()
        Case $Button3
            _Exit()
    EndSwitch
WEnd

If program is paused i can't click the exit button. But when the program is running already exit button work;/

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 310, 437, 345, 194)
$Button2 = GUICtrlCreateButton("Pauza", 80, 128, 75, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Exit", 80, 50, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $fPaused = False
HotKeySet("{p}","_Pause")
HotKeySet("{e}","_Exit")
Func _Pause()
    $fPaused = Not $fPaused
    if not $fPaused then
        Tooltip("Program is running",0,0)
        GUICtrlSetData($Button2,"Pause")
    else
        Tooltip("Program is pause.",0,0)
        GUICtrlSetData($Button2,"Run")
    endif
    While $fPaused
        If GuiGetMsg() = $Button2 Then
            _Pause()
        EndIf
    WEnd
EndFunc

Func _Exit()
    Exit
EndFunc
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button2
            _Pause()
        Case $Button3
            _Exit()
    EndSwitch
WEnd

Press "e" to Exit script..............Tested in win 7

Edit;;

While your script is paused or not script will be closed when you press "e".

Edited by Starstar

Life is like a coin. You can spend it Anyway as you wish and for your kind information. "you can spend it only once."

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 310, 437, 345, 194)
$Button2 = GUICtrlCreateButton("Pauza", 80, 128, 75, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Exit", 80, 70, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


Global $fPaused = False
HotKeySet("{p}","_Pause")
Func _Pause()
    $fPaused = Not $fPaused
    if not $fPaused then
        Tooltip("Program is running",0,0)
        GUICtrlSetData($Button2,"Pause")
    else
        Tooltip("Program is pause.",0,0)
        GUICtrlSetData($Button2,"Run")
    endif
    While $fPaused
        If GuiGetMsg() = $Button2 Then
            _Pause()
        EndIf
    WEnd
EndFunc

Func _Exit()
    if GuiGetMsg() = $Button3 Then
        Exit
    endif
EndFunc
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button2
            _Pause()
        Case $Button3
            _Exit()
    EndSwitch
WEnd

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 310, 437, 345, 194)
$Button2 = GUICtrlCreateButton("Pauza", 80, 128, 75, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Exit", 80, 70, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


Global $fPaused = False
HotKeySet("{p}","_Pause")
Func _Pause()
    $fPaused = Not $fPaused
    if not $fPaused then
        Tooltip("Program is running",0,0)
        GUICtrlSetData($Button2,"Pause")
    else
        Tooltip("Program is pause.",0,0)
        GUICtrlSetData($Button2,"Run")
    endif
    While $fPaused
        If GuiGetMsg() = $Button2 Then
            _Pause()
        endif
        if GuiGetMsg() = $Button3 Then
            _Exit()
        endif
    WEnd
EndFunc

Func _Exit()
    Exit
EndFunc
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button2
            _Pause()
        Case $Button3
            _Exit()
    EndSwitch
WEnd

I don't know i understand you but i do this and work BUT when i clicked many times in one second the script is lagged;/

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 310, 437, 345, 194)
$Button2 = GUICtrlCreateButton("Pauza", 80, 128, 75, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Exit", 80, 70, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


Global $fPaused = False
HotKeySet("{p}", "_Pause")
Func _Pause()
    $fPaused = Not $fPaused
    If Not $fPaused Then
        ToolTip("Program is running", 0, 0)
        GUICtrlSetData($Button2, "Pause")
    Else
        ToolTip("Program is pause.", 0, 0)
        GUICtrlSetData($Button2, "Run")
    EndIf
    While $fPaused
        Switch GUIGetMsg()
            Case $Button2
                _Pause()
            Case $Button3
                _Exit()
        EndSwitch
    WEnd
EndFunc   ;==>_Pause

Func _Exit()
    Exit
EndFunc   ;==>_Exit
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button2
            _Pause()
        Case $Button3
            _Exit()
    EndSwitch
WEnd

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Ok thank you. I have next problem ;/

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 310, 437, 345, 194)
$Button2 = GUICtrlCreateButton("Pauza", 80, 128, 75, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Exit", 80, 70, 75, 25, $WS_GROUP)
$Button4 = GUICtrlCreateButton("Count", 80, 95, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $i
Global $fPaused = False
HotKeySet("{p}", "_Pause")
Func _Pause()
    $fPaused = Not $fPaused
    If Not $fPaused Then
        ToolTip("Program is running", 0, 0)
        GUICtrlSetData($Button2, "Pause")
    Else
        ToolTip("Program is pause.", 0, 0)
        GUICtrlSetData($Button2, "Run")
    EndIf
    While $fPaused
        Switch GUIGetMsg()
            Case $Button2
                _Pause()
            Case $Button3
                _Exit()
            Case $Button4
                ToolTip("You can't run script while program is paused",0,0)
        EndSwitch
    WEnd
EndFunc   ;==>_Pause

Func _Count()
    While 1
        $i = $i+1
        ToolTip("Time: "&$i,0,0)
        sleep(1000)
    wend
EndFunc

Func _Exit()
    Exit
EndFunc   ;==>_Exit
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button2
            _Pause()
        Case $Button3
            _Exit()
        Case $Button4
            _Count()
    EndSwitch
WEnd

when i add next function and click "count" i can't pause the script and exit. Any ideas? Because i don't have any idea. I tried all ;/

Link to comment
Share on other sites

I added variable:

Global $fCount = False

and do very similar what i have in pause function

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 310, 437, 345, 194)
$Button2 = GUICtrlCreateButton("Pauza", 80, 128, 75, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Exit", 80, 70, 75, 25, $WS_GROUP)
$Button4 = GUICtrlCreateButton("Count", 80, 95, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $i
Global $fPaused = False
Global $fCount = False
HotKeySet("{p}", "_Pause")

Func _Pause()
    $fPaused = Not $fPaused
    If Not $fPaused Then
        ToolTip("Program is running", 0, 0)
        GUICtrlSetData($Button2, "Pause")
    Else
        ToolTip("Program is pause.", 0, 0)
        GUICtrlSetData($Button2, "Run")
    EndIf
    While $fPaused
        Switch GUIGetMsg()
            Case $Button2
                _Pause()
            Case $Button3
                _Exit()
            Case $Button4
                ToolTip("You can't run script while program is paused",0,0)
        EndSwitch
    WEnd
EndFunc   ;==>_Pause

Func _Count()
    $fCount = not $fCount
    if $fCount then
        While 1
            $i = $i+1
            ToolTip("Time: "&$i,0,0)
            sleep(1000)
        wend
    else
        tooltip("??",0,0)
    endif
    While $fCount
        Switch GUIGetMsg()
            Case $Button2
                _Pause()
            Case $Button3
                _Exit()
            Case $Button4
                _Count()
        EndSwitch
    WEnd    
EndFunc

Func _Exit()
    Exit
EndFunc   ;==>_Exit
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button2
            _Pause()
        Case $Button3
            _Exit()
        Case $Button4
            _Count()
    EndSwitch
WEnd

i _Pause it works but doesn't in _Count

Link to comment
Share on other sites

Do NOT use that like that or eventually your script will crash. You're calling the _Pause function from within the _Pause function, you're going to get a recursion error eventually if you use this for any length of time.You should be returning from all your functions and use one While loop, not a separate one in each function, not only is it a bitch to troubleshoot problems that way, it's not necessary if you write the code to deal with things like this.

Check out the GUIRegisterMsg demo I have linked in my signature, you can use one loop and still action buttons without needing to use the terrible code I see in this thread.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...