Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Temperature converter
#lang racket/gui
(define (convert-c C)
(number->string (+ 32 (* C (/ 9 5)))))
(define (convert-f F)
(number->string (* (- F 32) (/ 5 9))))
(define (update-fahrenheit control event)
(define tc (send text-celsius get-value))
(if (eq? tc "")
(begin
(send text-fahrenheit set-value "")
(send text-celsius set-field-background (make-object color% 255 255 255 1)))
(begin
(if (number? (string->number tc))
(begin
(send text-celsius set-field-background (make-object color% 255 255 255 1))
(send text-fahrenheit set-value (convert-c (string->number tc))))
(begin
(send text-celsius set-field-background (make-object color% 255 0 0 1))
(send text-fahrenheit set-value ""))))))
(define (update-celsius control event)
(define tf (send text-fahrenheit get-value))
(if (eq? tf "")
(begin
(send text-celsius set-value "")
(send text-fahrenheit set-field-background (make-object color% 255 255 255 1)))
(begin
(if (number? (string->number tf))
(begin
(send text-fahrenheit set-field-background (make-object color% 255 255 255 1))
(send text-celsius set-value (convert-c (string->number tf))))
(begin
(send text-fahrenheit set-field-background (make-object color% 255 0 0 1))
(send text-celsius set-value ""))))))
(define frame (new frame%
[label "TempConv"]
[width 150]
[height 60]))
(define main-pane (new horizontal-pane%
[parent frame]
[alignment '(center center)]))
(define text-celsius (new text-field%
[label #f]
[parent main-pane]
[callback update-fahrenheit]))
(define label-celsius (new message%
[parent main-pane]
[label "Celsius ="]))
(define text-fahrenheit (new text-field%
[label #f]
[parent main-pane]
[callback update-celsius]))
(define label-fahrenheit (new message%
[parent main-pane]
[label "Fahrenheit"]))
; Show the frame by calling its show method
(send frame show #t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment