[racket] Run Code after Button Click using the racket/gui library

From: Derek C. (puzzledplane at yahoo.com)
Date: Sun Oct 26 18:51:08 EDT 2014

Hello,




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)))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141026/7d33f88e/attachment-0001.html>

Posted on the users mailing list.