dragancla

GUI question - call function when I click a button

8 posts in this topic

Hey guys,

 

I'm trying to create some sort of download speed test in AutoIT, but I'm at a loss on how to call a function ONLY when I press a certain button. My script looks something like this:

 

$SpeedTestButton = GUICtrlCreateButton("Test Download Speed", 10,50,120,35)
$SpeedTestLabel = GUICtrlCreateEdit("Download Speed is:  " & DownloadSpeed(), 170, 57, 190, 20, $ES_READONLY)

Func DownloadSpeed()
    Local $Download = "http://bitdefender.com/tv"
    Local $TempFile = @TempDir & "\Temp.temp"
    $Size = InetGetSize($Download)
    If $Size = 0 Then Return "(Not Connected)"
    $Time = TimerInit()
    $Success = InetGet($Download, $TempFile, 1, 0)
    If $Success = 0 Then Return "(Not Connected)"
    $Time = TimerDiff($Time)
    $Rate = ($Size / $Time)
    FileDelete($TempFile)
    Return Round($Rate) & " KB/Sec"
EndFunc

Currently it calls the function when I run the script, I want it to call the function when I press the button. Any idea?

Share this post


Link to post
Share on other sites



And maybe only show the Edit once the button is pressed, that would make sense.

Share this post


Link to post
Share on other sites

Welcome to AutoIt and the forum!

When asking for help please post a working reproducer :)
What you are looking for is described in the help file for GUICtrlCreateButton.
I wouldn't use an Edit control but a Label to display the result of your speed calculation.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hello welcome to the forum check GUISetOnEvent or GUIGetMsg and use GUICtrlSetData to se the data to the edit control.

 

 

Saludos

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi.

Welcome to the forum. You can loop a "while 1 - wend" and wait for pressing the button. If button is pressed run DownloadSpeed() and update the edit:

#include <GuiConstants.au3>

GUICreate("SpeedTest")
GUICtrlCreateDummy()
Local $hSpeedTestButton = GUICtrlCreateButton("Test Download Speed", 10,50,120,35)
Local $hSpeedTestLabel = GUICtrlCreateLabel("", 170, 57, 190, 20)
GUISetState(@SW_SHOW)

Local $msg
Local $iSpeed
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hSpeedTestButton
            GUICtrlSetData($hSpeedTestLabel, "")
            $iSpeed = DownloadSpeed()
            GUICtrlSetData($hSpeedTestLabel, "Download Speed is:  " & $iSpeed)
    EndSwitch
    Sleep(10)
WEnd

Func DownloadSpeed()
    SplashTextOn("Wait", "Download-Test running...", 300, 50)
    Local $Download = "http://bitdefender.com/tv"
    Local $TempFile = @TempDir & "\Temp.temp"
    Local $Size = InetGetSize($Download)
    If $Size = 0 Then Return "(Not Connected)"
    Local $Time = TimerInit()
    Local $Success = InetGet($Download, $TempFile, 1, 0)
    If $Success = 0 Then Return "(Not Connected)"
    $Time = TimerDiff($Time)
    Local $Rate = ($Size / $Time)
    FileDelete($TempFile)
    SplashOff()
    Return Round($Rate) & " KB/Sec"
EndFunc

I inserted a splash while waiting.

Conrad

Edited by Simpel
1 person likes this

SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

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

Share this post


Link to post
Share on other sites
6 hours ago, Simpel said:

Hi.

Welcome to the forum. You can loop a "while 1 - wend" and wait for pressing the button. If button is pressed run DownloadSpeed() and update the edit:

#include <GuiConstants.au3>

GUICreate("SpeedTest")
GUICtrlCreateDummy()
Local $hSpeedTestButton = GUICtrlCreateButton("Test Download Speed", 10,50,120,35)
Local $hSpeedTestLabel = GUICtrlCreateLabel("", 170, 57, 190, 20)
GUISetState(@SW_SHOW)

Local $msg
Local $iSpeed
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hSpeedTestButton
            GUICtrlSetData($hSpeedTestLabel, "")
            $iSpeed = DownloadSpeed()
            GUICtrlSetData($hSpeedTestLabel, "Download Speed is:  " & $iSpeed)
    EndSwitch
    Sleep(10)
WEnd

Func DownloadSpeed()
    SplashTextOn("Wait", "Download-Test running...", 300, 50)
    Local $Download = "http://bitdefender.com/tv"
    Local $TempFile = @TempDir & "\Temp.temp"
    Local $Size = InetGetSize($Download)
    If $Size = 0 Then Return "(Not Connected)"
    Local $Time = TimerInit()
    Local $Success = InetGet($Download, $TempFile, 1, 0)
    If $Success = 0 Then Return "(Not Connected)"
    $Time = TimerDiff($Time)
    Local $Rate = ($Size / $Time)
    FileDelete($TempFile)
    SplashOff()
    Return Round($Rate) & " KB/Sec"
EndFunc

I inserted a splash while waiting.

Conrad

That is awesome! 

Do you have any idea how I would integrate this in my own GUI? I would like to add that to a panel - code would go under _AddControlsToPanel($aPanel[1]) - and, if I just add the 2 Local variables and the while loop to it, it just hangs on Run until I hit CTRL-Break

Capture.PNG

Share this post


Link to post
Share on other sites

Therefor we have to see your code. Could be a lot of reasons. 


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

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

Share this post


Link to post
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