Scheme: how to clear all the messages in a dialog - scheme

i have a method that prints messages in a list on a dialog object. The code below is printing "a b c a b c d e" and i want it to be "a b c d e". How do i solve this problem? Is there a method that i can clear all the messages in a dialog object?
#lang racket/gui
(define frame (new frame%
[width 300]
[height 300]
[label "Frame"]))
(new button%
[parent frame]
[label "Messages"]
(callback (lambda (button event)
(send msg-dialog show #t))))
(define msg-dialog (new dialog%
(label "Name")
(parent frame)
(width 300)
(height 300)))
(define (make-msg%)
(new message% [parent msg-dialog]
[label "Message: "]
[min-width 80]
[min-height 30]))
(define (print-msg lst)
(if (null? lst)
(void)
(begin
(send (make-msg%) set-label (symbol->string (car lst)))
(print-msg (cdr lst)))))
(define list '(a b c))
(print-msg list)
(set! list (append list '(d e)))
(print-msg list)
(send frame show #t)

The GUIML library has a function that can do it easily:
(require (planet jphelps/guiml))
(delete-children msg-dialog)
delete-children is implemented like this if you'd rather fork it:
(define (delete-children object (id #f))
(send object change-children (λ (x)
(if id
(filter (λ (widget)
(not (eq? (send widget ___get-guiml-name) id)))
x)
'()))))

Consider using a text-field% instead.
http://docs.racket-lang.org/gui/text-field_.html?q=message%25
For a text field, the most useful methods of a text% object are the following:
(send a-text get-text) returns the current text of the editor.
(send a-text erase) deletes all text from the editor.
(send a-text insert str) inserts str into the editor at the current caret position.

Related

Racket GUI Toolkit: How to embed a link

Say we have a frame
(define my-frame (new frame% [parent #f] [label "test"]))
and a message, that should link to google.
(new message%
[parent my-frame]
[label "https://www.google.com"])
(send my-frame show #t)
The above does not work. How does one embed a link in the Racket GUI toolkit?
You can use the button, but I guess that isn't link-like so you don't want it, the following code shows how to use editor clickback to create a link-like text.
(require net/sendurl)
(define f (new frame% [parent #f]
[label "test"]
[width 300]
[height 300]))
(define editor (new text%))
(new editor-canvas% [parent f]
[editor editor])
(define t "https://www.google.com")
(send editor insert t)
(send editor set-clickback 0 (string-length t)
(λ (text start end)
(send-url t)))
(send* f
[show #t]
[center])

How to stop this sleeping program in Racket

I have following code of a countdown timer. It starts all right but it does not stop on pressing the stop button.
#lang racket/gui
(define myframe (new frame% [label "Timer"] [x 500] [y 200] ) )
(define tfsecs (new text-field% [parent myframe] [label "Secs:"]))
(define msgbmi (new message%
[parent myframe] [auto-resize #t] [label ""] ))
(define stopflag #f)
(define (OnButtonPressFn n)
(sleep 1)
(set! n (sub1 n))
(define m 0)
(send msgbmi set-label "")
(for((i (in-naturals)) #:break stopflag)
(set! m (- n i))
; (printf "running; i= ~a~n" m)
(send msgbmi set-label (number->string m))
(sleep 1) ) )
(define startbutton
(new button% [parent myframe] [label "Start"]
(callback (lambda (b e)
(define secs (string->number (send tfsecs get-value)))
(when secs (OnButtonPressFn secs))))))
(define stopbutton
(new button% [parent myframe] [label "Stop"]
(callback (lambda (b e)
(set! stopflag #t)))))
(send myframe show #t)
It stops if I limit the for loop to end it at 0. But I want it to keep showing how much time is over till the stop button is pressed.
Apparently, the stopflag being set by stop button is not being read by running loop. How can this be corrected? How can I make the stop button work properly here?
Let's follow the intended control flow.
First we click the start button. The system generates an event. The event handler (the call back is called) and the click is handled. Then later on the stop button is clicked. The system generates a new event and the event handler for the stop button is called.
The problem in your program is that the event handler for the start button never returns. The system only handles one event at a time, so if you never return from an event handler, any following events will not be handled.
One way to fix the problem is to start a new thread to do the actual work:
(define (OnButtonPressFn n)
(thread (λ ()
(sleep 1)
(set! n (sub1 n))
(define m 0)
(send msgbmi set-label "")
(for((i (in-naturals)) #:break stopflag)
(set! m (- n i))
; (printf "running; i= ~a~n" m)
(send msgbmi set-label (number->string m))
(sleep 1) ) )))
This event handler creates a new thread (that runs concurrently with the event loop) and then returns immediately. The event loop is there able to handle new events. In the mean time the new thread does the work (here printing numbers).

Why are these string different in Racket

I am running following code:
(define myframe (new frame% [label "myframe"]))
(define tf1 (new text-field% [parent myframe] [label "tf1"]))
(define tf2 (new text-field% [parent myframe][label "tf2"]))
(define tf3 (new text-field% [parent myframe][label "tf3"]))
(send myframe show #t)
(define combined_str (string-append (send tf1 get-value) "-" (send tf2 get-value) "-" (send tf3 get-value) ))
(println combined_str)
(if (eq? "--" combined_str) "same" "different")
Output is:
"--"
"different"
The combined_str is "--" because the text-fields are blank. But it is not coming as same as "--".
This is almost certainly caused by using eq? instead of equal?. See Object Identity and Comarpison for more, and also What is the difference between eq?, eqv?, equal?, and = in Scheme?. In short, eq? does a pointer comparison, which isn't what you want.
Examples:
> (eq? "--" (string-append "-" "-"))
#f
> (equal? "--" (string-append "-" "-"))
#t
> (string=? "--" (string-append "-" "-"))
#t

how to align racket GUI text fields and buttons

I am creating a GUI for a racket program where a user inputs a title and a blog and then submits it. This is my code for those fields so far:
(define blogPost%
(class horizontal-panel%
(super-new)
(define titleoutput (new text-field% (label " title")
(min-height 20)
(min-width 200)
(vert-margin 20)
(horiz-margin 10)
(parent this)))
(define output (new text-field% (label "blog")
(style '(multiple))
(min-height 20)
(vert-margin 20)
(min-width 400)
(parent this)))
(define (callback button event)
(define title-new-value (send titleoutput get-value))
(define new-value (send output get-value))
(save title-new-value new-value)
(send output set-value "")
(send titleoutput set-value "")
(send howisit show #t))
(define button (new button% (label "Submit")
(vert-margin 0)
(horiz-margin 10)
(parent this)
(callback callback)))
))
It is currently aligned like this:
But I would like the title text box to be above the blog field and the submit button to be centered at the bottom.
I'm assuming that you're running this code in the same way as for your previous question. In that, you used your class like this:
(define f (new frame% [label "blog post GUI"] [min-width 400] [min-height 500]))
(define tib (new blogPost%
[parent f]))
(send f show #t)
Now since you defined blogPost% as a subclass of horizontal-panel%, it also inherits all of the initialization arguments of horizontal-panel%, including the alignment argument. So you can pass the [alignment '(left top)] initialization argument to your blogPost% class:
(define f (new frame% [label "blog post GUI"] [min-width 400] [min-height 500]))
(define tib (new blogPost%
[parent f]
[alignment '(left top)]))
(send f show #t)
If you want to build this default into your blogPost% class, you could add it to the (super-new) form instead:
(define blogPost%
(class horizontal-panel%
(super-new [alignment '(left top)])
...))
However, if you happen to have a (new blogPost% ... [alignment '(left top)] ...) around somewhere else, I believe this will break that code.
So to avoid that, it would probably be best to make the blogPost% class it's own class, so that instead of being a horizontal-panel%, it would have a horizontal-panel%, in the same way that it already has two text-fields and a button.
This is better for the long term because after this change, code that uses blogPost% won't break if you change which initialization arguments you passed to horizontal-panel% (which was implicit in the super-new previously).
(define blogPost%
(class object% ; object% instead of horizontal-panel%
; This argument is explicit now.
; If other code relies on other arguments, specify them here.
(init parent)
(super-new)
(define panel
(new horizontal-panel% ; this new call is explicit now
[parent parent] ; you can later add more arguments
[alignment '(left top)])) ; and it won't break things
(define titleoutput
(new text-field%
[label " title"]
[min-height 20]
[min-width 200]
[vert-margin 20]
[horiz-margin 10]
[parent panel])) ; panel instead of this
(define output
(new text-field%
[label "blog"]
[style '(multiple)]
[min-height 20]
[vert-margin 20]
[min-width 400]
[parent panel])) ; panel instead of this
(define (callback button event)
(define title-new-value (send titleoutput get-value))
(define new-value (send output get-value))
(save title-new-value new-value)
(send output set-value "")
(send titleoutput set-value "")
(send howisit show #t))
(define button
(new button%
[label "Submit"]
[vert-margin 0]
[horiz-margin 10]
[parent panel] ; panel instead of this
[callback callback]))
))
(define f (new frame% [label "blog post GUI"] [min-width 400] [min-height 500]))
(define tib (new blogPost%
[parent f]))
(send f show #t)
Of course, with this method, you won't be able to use the methods defined for horizontal-panel% on instances of your blogPost% class, but in the long run that's a good thing as well. If you ever, in the future, wanted to change the implementation to use something other than a horizontal-panel%, you could.

Run Code after Button Click using the racket/gui library

I am trying to get the value of a label after a button is clicked. I know that I can use (send x get-label) to get the value of the label, but it only gets the initial value of the label in my case "No Zip Code Entered". Also, after that button is pressed I would like to run code that queries an API and parses xml information using the zip code from the label. Below is my code:
Thanks in Advanced,
Puzzledplane
GUI:
#lang racket
(require racket/gui/base)
;; Creates a Frame called mainframe
(define mainframe (new frame% [label "Forecaster - Powered by Wunderground API"]
[width 500]
[height 500]
[stretchable-width 500]
[stretchable-height 500]))
;; Creates a Current Conditions group-box-panel
(define maingroup (new group-box-panel%
[label "Current Conditions:"]
[parent mainframe]
[min-height 450]
[stretchable-height 450]))
(define cclabel (new message% [parent maingroup]
[label "Insert Conditions Here from API"] ))
;; Creates a Zip Code group-box-panel
(define zipcodegroup (new group-box-panel%
[label "Zip Code:"]
[parent mainframe]
[min-height 100]
[stretchable-height 100]))
;; Zip Code Message Label -- Defaults to No Zip Code Entered
(define zipcodelabel (new message% [parent zipcodegroup]
[label "No Zip Code Entered"] ))
;; Zip Code Text-Field
(define zipInput
(new text-field%
[parent zipcodegroup]
[label ""]
[init-value ""]
[min-width 5]
[stretchable-width 5]
[callback (lambda(f ev)
(define v (send f get-value))
(unless (string->number v)
(send f set-value (regexp-replace* #rx"[^0-9]+" v ""))))]))
;; Submit Button
(define submit-button
(new button%
[parent zipcodegroup]
[label "Submit"]
[callback (lambda (button event)
(let ([v (send zipInput get-value)])
(send zipcodelabel set-label v)
))]))
;; Show Frame
(send mainframe show #t)
XML Parsing:
#lang racket
(require net/url xml xml/path)
(define curent-cond-url (string->url "http://api.wunderground.com/api/*snip*/conditions/q/autoip.xml"))
(define current-cond-port (get-pure-port curent-cond-url))
(define response (port->string current-cond-port))
(close-input-port current-cond-port)
(define data (xml->xexpr
((eliminate-whitespace '(response))
(read-xml/element (open-input-string response)))))
(define curr-location (se-path*/list '(display_location full) data))
(define curr-weather (se-path*/list '(current_observation weather) data))
(define curr-temp (se-path*/list '(current_observation temp_f) data))
(define curr-humidity (se-path*/list '(current_observation relative_humidity) data))
(define curr-wind (se-path*/list '(current_observation wind_string) data))
(define curr-feels-like (se-path*/list '(current_observation feelslike_f) data))
(define current-conditions
(list (list 'Location: curr-location) (list 'Conditions: curr-weather)
(list 'Temperature: curr-temp) (list 'Feels-Like: curr-feels-like)
(list 'Humidity: curr-humidity) (list 'Wind: curr-wind)))

Resources