Jump to content

GUI question - call function when I click a button


 Share

Recommended Posts

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?

Link to comment
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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

Link to comment
Share on other sites

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
SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   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.

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

Link to comment
Share on other sites

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

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   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.

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