按钮后运行代码单击使用球拍/ gui库

时间:2014-10-26 21:54:29

标签: user-interface scheme racket

我试图在单击按钮后获取标签的值。我知道我可以使用(发送x get-label)来获取标签的值,但它只获得标签的初始值,在我的情况下“No Zip Code Entered”。此外,按下该按钮后,我想运行查询API的代码,并使用标签中的邮政编码解析xml信息。以下是我的代码:

感谢高级,

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解析:

#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)))

0 个答案:

没有答案