Re: [racket-users] returning GUI elements in function?

2019-07-09 Thread Travis Hinkelman
It's funny how just knowing that there is an easy solution to a problem 
makes the problem easier to solve. My first thought about returning objects 
(prior to sending initial email) was that I would return all of the objects 
unnamed but then I was confused about how to specify the parents of the 
child objects in the `initialize-progress-bar` function. The code below 
solves my problem. As always, I welcome people sharing alternative ways to 
write the same code. 

#lang racket/gui

(define (initialize-progress-bar)
  (define frame (new frame%
 [label "Progress Bar"]
 [width 300]))

  (define hpane (new horizontal-pane%
 [parent frame]))

  (define gauge (new gauge%
 [label ""]
 [parent hpane]
 [range 100]))

  (define msg (new message%
   [parent hpane]
   [auto-resize #t]
   [label "0%"]))

  (send frame show #t)
  (values gauge msg))

(define (update-progress-bar new-value)
  (send the-gauge set-value new-value)
  (send the-msg set-label (string-append (~a new-value) "%")))

(define-values (the-gauge the-msg) (initialize-progress-bar))
(for ([i (in-range 1 101)])
  (sleep 0.05)
  (update-progress-bar i))




On Tuesday, July 9, 2019 at 1:05:03 AM UTC-7, Laurent wrote:
>
> There are several ways to solve this (including deriving classes), but the 
> simplest for you right now is probably to have `initialize-progress-bar' 
> return the gui widgets (in particular `gauge' and `msg'), assign the values 
> to `the-gauge' and `the-msg' (say) from the return values of 
> `(initialize-progress-bar)` call, and then pass these values to 
> `update-progress-bar' so that `gauge' and `msg' have the correct values 
> there.
>
> Does that make sense?
>
> On Tue, Jul 9, 2019 at 7:23 AM Travis Hinkelman  > wrote:
>
>> Hi All,
>>
>> I was playing around with creating a progress bar. The basic idea was 
>> straightforward.
>>
>> #lang racket/gui
>>
>> (define frame (new frame%
>>[label "Progress Bar"]
>>[width 300]))
>>
>> (define hpane (new horizontal-pane%
>>[parent frame]))
>>
>> (define gauge (new gauge%
>>[label ""]
>>[parent hpane]
>>[range 100]))
>>
>> (define msg (new message%
>>  [parent hpane]
>>  [auto-resize #t]
>>  [label "0%"]))
>>
>> (send frame show #t)
>>
>> (for ([i (in-range 1 101)])
>>   (sleep 0.05)
>>   (send gauge set-value i)
>>   (send msg set-label (string-append (~a i) "%")))
>>
>>
>> When I tried to take the next step of wrapping this up into a couple of 
>> functions, I was completely lost. I don't know how to initialize a frame 
>> with child elements by calling a function because all of the elements of 
>> the frame only exist in the function and not at the top-level (probably 
>> butchering the jargon here; hopefully it makes sense). I wasn't really sure 
>> where to look to learn more about doing such a thing. Here is some 
>> pseudocode to try to further illustrate my confusion.
>>
>> (define (initialize-progress-bar)
>>   ;; insert code that creates progress bar
>>   ;; i.e., from defining frame to sending frame in code above)
>>
>> (define (update-progress-bar new-value)
>>   (send gauge set-value new-value)
>>   (send msg set-label (string-append (~a new-value) "%")))
>>
>> (initialize-progress-bar)
>> (for ([i (in-range 1 101)])
>>   (sleep 0.05)
>>   (update-progress-bar i))
>>
>> Thanks,
>>
>> Travis
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to racket...@googlegroups.com .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/racket-users/61a3ea75-d285-45d1-90d4-de569c441c8d%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/7adee791-e04e-44b8-8f27-881298865d48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] returning GUI elements in function?

2019-07-09 Thread Laurent
There are several ways to solve this (including deriving classes), but the
simplest for you right now is probably to have `initialize-progress-bar'
return the gui widgets (in particular `gauge' and `msg'), assign the values
to `the-gauge' and `the-msg' (say) from the return values of
`(initialize-progress-bar)` call, and then pass these values to
`update-progress-bar' so that `gauge' and `msg' have the correct values
there.

Does that make sense?

On Tue, Jul 9, 2019 at 7:23 AM Travis Hinkelman 
wrote:

> Hi All,
>
> I was playing around with creating a progress bar. The basic idea was
> straightforward.
>
> #lang racket/gui
>
> (define frame (new frame%
>[label "Progress Bar"]
>[width 300]))
>
> (define hpane (new horizontal-pane%
>[parent frame]))
>
> (define gauge (new gauge%
>[label ""]
>[parent hpane]
>[range 100]))
>
> (define msg (new message%
>  [parent hpane]
>  [auto-resize #t]
>  [label "0%"]))
>
> (send frame show #t)
>
> (for ([i (in-range 1 101)])
>   (sleep 0.05)
>   (send gauge set-value i)
>   (send msg set-label (string-append (~a i) "%")))
>
>
> When I tried to take the next step of wrapping this up into a couple of
> functions, I was completely lost. I don't know how to initialize a frame
> with child elements by calling a function because all of the elements of
> the frame only exist in the function and not at the top-level (probably
> butchering the jargon here; hopefully it makes sense). I wasn't really sure
> where to look to learn more about doing such a thing. Here is some
> pseudocode to try to further illustrate my confusion.
>
> (define (initialize-progress-bar)
>   ;; insert code that creates progress bar
>   ;; i.e., from defining frame to sending frame in code above)
>
> (define (update-progress-bar new-value)
>   (send gauge set-value new-value)
>   (send msg set-label (string-append (~a new-value) "%")))
>
> (initialize-progress-bar)
> (for ([i (in-range 1 101)])
>   (sleep 0.05)
>   (update-progress-bar i))
>
> Thanks,
>
> Travis
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/61a3ea75-d285-45d1-90d4-de569c441c8d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CABNTSaEaaAaQSneGi2yOR4dv_0hOz_WYbaQ4v19q4%2BuXY_E%3Dhw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] returning GUI elements in function?

2019-07-09 Thread Travis Hinkelman
Hi All,

I was playing around with creating a progress bar. The basic idea was 
straightforward.

#lang racket/gui

(define frame (new frame%
   [label "Progress Bar"]
   [width 300]))

(define hpane (new horizontal-pane%
   [parent frame]))

(define gauge (new gauge%
   [label ""]
   [parent hpane]
   [range 100]))

(define msg (new message%
 [parent hpane]
 [auto-resize #t]
 [label "0%"]))

(send frame show #t)

(for ([i (in-range 1 101)])
  (sleep 0.05)
  (send gauge set-value i)
  (send msg set-label (string-append (~a i) "%")))


When I tried to take the next step of wrapping this up into a couple of 
functions, I was completely lost. I don't know how to initialize a frame 
with child elements by calling a function because all of the elements of 
the frame only exist in the function and not at the top-level (probably 
butchering the jargon here; hopefully it makes sense). I wasn't really sure 
where to look to learn more about doing such a thing. Here is some 
pseudocode to try to further illustrate my confusion.

(define (initialize-progress-bar)
  ;; insert code that creates progress bar
  ;; i.e., from defining frame to sending frame in code above)

(define (update-progress-bar new-value)
  (send gauge set-value new-value)
  (send msg set-label (string-append (~a new-value) "%")))

(initialize-progress-bar)
(for ([i (in-range 1 101)])
  (sleep 0.05)
  (update-progress-bar i))

Thanks,

Travis

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/61a3ea75-d285-45d1-90d4-de569c441c8d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.