Sign in to follow this  
Followers 0
emendelson

Switch label text every n seconds?

17 posts in this topic

#1 ·  Posted (edited)

Is there some simple code that would let a GUI switch the text in a label every few seconds so that the user wouldn't conclude that the script has locked up during a long download? Here's the code as it stands:

GUICreate($t, 240, 75, -1, -1, $WS_DLGFRAME)
$dl_label = "Please wait during download"
GUICtrlCreateLabel($dl_label, 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
;;.....
;; use InetGet download a 10MB file here and do some error checking to make sure the file is saved, etc.
;;......
GUIDelete()

What I would like to do is have the label (the $dl_label var) switch every five seconds between "Please wait during download..." and "This may take a while..." and then back again.

This download needs to be in the foreground mode because the script can't proceed without the file. But if there was a way to put it in the background, then I would like to be able to have the GUI show the number of bytes download using @InetGetBytesRead, but that seems to work only when InetGet is in background mode.

I've searched the forums and can't find any answer to this, but maybe I haven't searched intelligently enough.

Edited by Edward Mendelson

Share this post


Link to post
Share on other sites



Check out AdlibEnable in the help file. I guess you could create a function that changes the text and use AdlibEnable on it.

Share this post


Link to post
Share on other sites

Check out AdlibEnable in the help file. I guess you could create a function that changes the text and use AdlibEnable on it.

Hmm... That's definitely the way. I suppose I need a function that works like this (pseudocode)

AdLibEnable(ChangeMyLabel, 5000)

;; do the downloading

AdLibDisable

Func ChangeMyLabel()

Check var that says which label is showing

Set var to indicate the other label

GUICreateLabel

ShowGUI

EndFunc

Or something like that? If anyone's already done this, and can point to the most elegant solution. Do I simply move the whole GUICreateLabel line into the function, and put the GUICanel line just after AdLibDisable?

Thanks again for this.

Share this post


Link to post
Share on other sites

Hmm... That's definitely the way. I suppose I need a function that works like this (pseudocode)

AdLibEnable(ChangeMyLabel, 5000)

;; do the downloading

AdLibDisable

Func ChangeMyLabel()

Check var that says which label is showing

Set var to indicate the other label

GUICreateLabel

ShowGUI

EndFunc

Or something like that? If anyone's already done this, and can point to the most elegant solution. Do I simply move the whole GUICreateLabel line into the function, and put the GUICanel line just after AdLibDisable?

Thanks again for this.

Better to use GUICtrlSetdata to change the text of the label :)

Share this post


Link to post
Share on other sites

Hmm... That's definitely the way. I suppose I need a function that works like this (pseudocode)

AdLibEnable(ChangeMyLabel, 5000)

;; do the downloading

AdLibDisable

Func ChangeMyLabel()

Check var that says which label is showing

Set var to indicate the other label

GUICreateLabel

ShowGUI

EndFunc

Or something like that? If anyone's already done this, and can point to the most elegant solution. Do I simply move the whole GUICreateLabel line into the function, and put the GUICanel line just after AdLibDisable?

Thanks again for this.

Put the GUICreateLabel() function at the top of your script then use GUICtrlSetdata() in your ChangeMyLabel() function.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Better to use GUICtrlSetdata to change the text of the label :)

Ah... so, something like this, but with more elegant label switching using vars, not full text (just guessing, in case I've got this completely wrong):

GUICreate($t, 240, 75, -1, -1, $WS_DLGFRAME)
$switchlabel = "Please wait during download..."
AdLibEnable(ChangeMyLabel, 5000)
$dl_label = GUICtrlCreateLabel($switchlabel, 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
;;.....
;; use InetGet download a 10MB file here and do some error checking to make sure the file is saved, etc.
;;......
AdLibDisable
GUIDelete()


Func ChangeMyLabel()
If $switchlabel = "Please wait during download..." Then
   $switchlabel = "This may take a while..."
ElseIf $switchlabel = "This may take a while..." Then
   $switchlabel = "Please wait during download..."
EndIf
GUICtrlSetData($dl_label, $switchlabel)
EndFunc

I'm a complete beginner at GUIs and virtually a beginner with AutoIt so many thanks for all help with this.

EDIT: replaced an equals sign with a comma in GUICtrlSetData.

Edited by Edward Mendelson

Share this post


Link to post
Share on other sites

I'm not a master at GUI's either, but maybe?

GUICreate($t, 240, 75, -1, -1, $WS_DLGFRAME)
$dl_label = GUICtrlCreateLabel("Please wait during download...", 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
;;.....
;; use InetGet download a 10MB file here and do some error checking to make sure the file is saved, etc.
;;......

While 1
    Sleep(5000)
    GUICtrlSetData($dl_label, "This may take a while...")
    Sleep(5000)
    GUICtrlSetData($dl_label, "Please wait during download...")
WEnd

Share this post


Link to post
Share on other sites

I'm not a master at GUI's either, but maybe?

GUICreate($t, 240, 75, -1, -1, $WS_DLGFRAME)
$dl_label = GUICtrlCreateLabel("Please wait during download...", 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
;;.....
;; use InetGet download a 10MB file here and do some error checking to make sure the file is saved, etc.
;;......

While 1
    Sleep(5000)
    GUICtrlSetData($dl_label, "This may take a while...")
    Sleep(5000)
    GUICtrlSetData($dl_label, "Please wait during download...")
WEnd

Wouldn't that cause the script to grind to a halt, unlike AdLib?

Share this post


Link to post
Share on other sites

Here's a no progress report and a plea for syntax help. I've got this in my script:

;....
$slbl1 = "Please wait during download..."
$slbl2 = "This may take a while ..."
$switchlabel = $slbl1
GUICreate($t, 240, 75, -1, -1, $WS_DLGFRAME)
AdLibEnable("ChangeMyLabel", 2000)
$dl_label = GUICtrlCreateLabel($switchlabel, 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
;; download and check here
AdLibDisable()
GUIDelete()
;....

Func ChangeMyLabel()
If $switchlabel = $slbl1 Then
   $switchlabel = $slbl2
ElseIf $switchlabel = $slbl2 Then
   $switchlabel = $slbl1
EndIf
GUICtrlSetData($dl_label, $switchlabel)
EndFunc

The GUI displays the first label correctly ("Please wait during download...") but never switches to the other one. Clearly I'm missing something that's probably very obvious. Any advice will be accepted gratefully.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

The GUI displays the first label correctly ("Please wait during download...") but never switches to the other one. Clearly I'm missing something that's probably very obvious. Any advice will be accepted gratefully.

I ran the following script (same as yours but with the #include and setting the title to a non-variable, plus a sleep where you stick your dl validation)

#include <guiconstants.au3>
;....
$slbl1 = "Please wait during download..."
$slbl2 = "This may take a while ..."
$switchlabel = $slbl1
GUICreate("hi there", 240, 75, -1, -1, $WS_DLGFRAME)
AdLibEnable("ChangeMyLabel", 2000)
$dl_label = GUICtrlCreateLabel($switchlabel, 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
;; download and check here
sleep(10000)
AdLibDisable()
GUIDelete()
;....

Func ChangeMyLabel()
If $switchlabel = $slbl1 Then
   $switchlabel = $slbl2
ElseIf $switchlabel = $slbl2 Then
   $switchlabel = $slbl1
EndIf
GUICtrlSetData($dl_label, $switchlabel)
EndFunc

and it worked fine...

Try just this script on your system, and if it works, then it may be something else in your script colliding with this snippet...

Edit: forgive me, I didn't know there was an <autoit> tag different from the <code> tag

Edited by james3mg

"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110

Share this post


Link to post
Share on other sites

#include <guiconstants.au3>
;....
$slbl1 = "Please wait during download..."
$slbl2 = "This may take a while ..."
$switchlabel = $slbl1
GUICreate("hi there", 240, 75, -1, -1, $WS_DLGFRAME)
AdlibEnable("ChangeMyLabel", 2000)
$dl_label = GUICtrlCreateLabel($switchlabel, 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd

Func ChangeMyLabel()
    If $switchlabel = $slbl1 Then
        $switchlabel = $slbl2
    ElseIf $switchlabel = $slbl2 Then
        $switchlabel = $slbl1
    EndIf
    GUICtrlSetData($dl_label, $switchlabel)
EndFunc   ;==>ChangeMyLabel


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

I ran the following script (same as yours but with the #include and setting the title to a non-variable, plus a sleep where you stick your dl validation)

#include <guiconstants.au3>
;....
$slbl1 = "Please wait during download..."
$slbl2 = "This may take a while ..."
$switchlabel = $slbl1
GUICreate("hi there", 240, 75, -1, -1, $WS_DLGFRAME)
AdLibEnable("ChangeMyLabel", 2000)
$dl_label = GUICtrlCreateLabel($switchlabel, 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
;; download and check here
sleep(10000)
AdLibDisable()
GUIDelete()
;....

Func ChangeMyLabel()
If $switchlabel = $slbl1 Then
   $switchlabel = $slbl2
ElseIf $switchlabel = $slbl2 Then
   $switchlabel = $slbl1
EndIf
GUICtrlSetData($dl_label, $switchlabel)
EndFunc

and it worked fine...

Try just this script on your system, and if it works, then it may be something else in your script colliding with this snippet...

(Sorry, I forgot the include line at the top, which is in my script).

Yes, that certainly works, but it stops the script entirely for ten seconds - but in the place where I have

;; download and check here

the script actually has these lines:

$f = InetGet("ftp://mirror.cs.wisc.edu/pub/mirrors/ghost/AFPL/current/gs" & $gsver & "w32.exe", $gsinst, 1, 0)
If $f = 0 Then
MsgBox(270352, $t, "Error: Unable to download Ghostcript " & $gsstring & ". The web site may be busy." _
     & @LF & @LF & "Wait a few moments and run this program again, or install Ghostscript manually before running this program.")
        Exit
EndIf

The whole idea is that the InetGet command gets completed before the GUI gets turned off. And if the script simply sleeps for ten seconds and then goes forward, the script has no way of knowing if the download actually happened.

I'm probably not making myself clear, but what I'm looking for is not simply a GUI that changes every few seconds, but a GUI that appears before a download begins, then changes every few seconds while the download continues, and then closes itself down when the download is complete - which happens when the InetGet command is done, and the script can proceed to the next step.

Share this post


Link to post
Share on other sites

Reply to gafrost:

Thanks for that - but again, the problem is that I want to run an InetGet command in the place where you have the While...WEnd loop and have the GUI be deleted when the InetGet command is finished. The While....Wend loop leaves the GUI up there (with the text alternating) until I press Esc.

It looks as if I may need to run the InetGet command in the background, but that's going to produce complications because I can't predict the size of the file, and I don't see how to test when the download is complete.

Share this post


Link to post
Share on other sites

Reply to gafrost:

Thanks for that - but again, the problem is that I want to run an InetGet command in the place where you have the While...WEnd loop and have the GUI be deleted when the InetGet command is finished. The While....Wend loop leaves the GUI up there (with the text alternating) until I press Esc.

It looks as if I may need to run the InetGet command in the background, but that's going to produce complications because I can't predict the size of the file, and I don't see how to test when the download is complete.

example of what you are attempting to do, just figured you would replace the loop with what you want

#include <guiconstants.au3>
;....
$slbl1 = "Please wait during download..."
$slbl2 = "This may take a while ..."
$switchlabel = $slbl1
GUICreate("hi there", 240, 75, -1, -1, $WS_DLGFRAME)
AdlibEnable("ChangeMyLabel", 2000)
$dl_label = GUICtrlCreateLabel($switchlabel, 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
; Advanced example - downloading in the background
InetGet("http://www.nowhere.com/somelargefile.exe", "test.exe", 1, 1)

While @InetGetActive
  ChangeMyLabel()
  Sleep(250)
Wend
Exit

Func ChangeMyLabel()
    If $switchlabel = $slbl1 Then
        $switchlabel = $slbl2
    ElseIf $switchlabel = $slbl2 Then
        $switchlabel = $slbl1
    EndIf
    GUICtrlSetData($dl_label, $switchlabel)
EndFunc   ;==>ChangeMyLabel

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

example of what you are attempting to do, just figured you would replace the loop with what you want

#include <guiconstants.au3>
;....
$slbl1 = "Please wait during download..."
$slbl2 = "This may take a while ..."
$switchlabel = $slbl1
GUICreate("hi there", 240, 75, -1, -1, $WS_DLGFRAME)
AdlibEnable("ChangeMyLabel", 2000)
$dl_label = GUICtrlCreateLabel($switchlabel, 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
; Advanced example - downloading in the background
InetGet("http://www.nowhere.com/somelargefile.exe", "test.exe", 1, 1)

While @InetGetActive
  ChangeMyLabel()
  Sleep(250)
Wend
Exit

Func ChangeMyLabel()
    If $switchlabel = $slbl1 Then
        $switchlabel = $slbl2
    ElseIf $switchlabel = $slbl2 Then
        $switchlabel = $slbl1
    EndIf
    GUICtrlSetData($dl_label, $switchlabel)
EndFunc   ;==>ChangeMyLabel
Thank you!! I must be more dense than I realized, because I didn't think that the background download would work, but this is exactly what I was looking for. I changed the sleep from 250 to 2000 to keep the label from changing four times a second, but this was EXACTLY what I should have figured out for myself, but couldn't. Thanks again.

Share this post


Link to post
Share on other sites

Why not use progressbar instead of only changing label (or set as label % of download size)?

Look at Helpfile at example for InetGetSize, InetGet - @InetGetBytesRead

Share this post


Link to post
Share on other sites

Why not use progressbar instead of only changing label (or set as label % of download size)?

Look at Helpfile at example for InetGetSize, InetGet - @InetGetBytesRead

Now that I realized that I could use background downloading, that's more or less exactly what I've done. Since the script can't be certain of the size of the file, I've plugged in the number of bytes downloaded, like this:

; include etc..
$slbl1 = "Please wait during download..."
GUICreate($t, 240, 75, -1, -1, $WS_DLGFRAME)
$dl_label = GUICtrlCreateLabel($slbl1, 40, 20, 160, 20, $SS_Center)
GUISetState(@SW_SHOW)
;....
$f = InetGet("ftp://mirror.cs.wisc.edu/pub/mirrors/ghost/AFPL/current/gs" & $gsver & "w32.exe", $gsinst, 1, 1)  
While @InetGetActive
If @InetGetBytesRead > 0 Then
    GUICtrlSetData($dl_label, @InetGetBytesRead & " bytes downloaded.")
EndIf
Sleep(250)
WEnd

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
Sign in to follow this  
Followers 0