GUI-калькулятор на Racket
Приведите пожалуйста пример кода простейшего калькулятора с GUI на Racket'e, достаточно 2 любых операций.
Код:
#lang racket
(require racket/class racket/gui/base)
(define frame (new frame% [label "Calculator"]
[width 300]
[height 200]))
(define field1 (new text-field% [parent frame]
[label "Number 1:"]
[stretchable-width #f]))
(define operation (new choice% [parent frame]
[label "Operation"]
[choices '("+" "-" "*")]))
(define field2 (new text-field% [parent frame]
[label "Number 2:"]
[stretchable-width #f]))
(define result (new message% [parent frame]
[label ""]
[auto-resize #t]))
(new button% [parent frame]
[label "Calculate"]
(callback (lambda (button event)
(define n1 (string->number (send field1 get-value)))
(define n2 (string->number (send field2 get-value)))
(define op (send operation get-selection))
(define res (if (= op 0)
(number->string (+ n1 n2))
(if (= op 1)
(number->string (- n1 n2))
(number->string (* n1 n2)))))
(send result set-label res))))
(send frame show #t)
(require racket/class racket/gui/base)
(define frame (new frame% [label "Calculator"]
[width 300]
[height 200]))
(define field1 (new text-field% [parent frame]
[label "Number 1:"]
[stretchable-width #f]))
(define operation (new choice% [parent frame]
[label "Operation"]
[choices '("+" "-" "*")]))
(define field2 (new text-field% [parent frame]
[label "Number 2:"]
[stretchable-width #f]))
(define result (new message% [parent frame]
[label ""]
[auto-resize #t]))
(new button% [parent frame]
[label "Calculate"]
(callback (lambda (button event)
(define n1 (string->number (send field1 get-value)))
(define n2 (string->number (send field2 get-value)))
(define op (send operation get-selection))
(define res (if (= op 0)
(number->string (+ n1 n2))
(if (= op 1)
(number->string (- n1 n2))
(number->string (* n1 n2)))))
(send result set-label res))))
(send frame show #t)
ЗЫ: Документация Racket достаточно хорошая, так что за полчаса можно самостоятельно всё найти и сделать.
Спасибо большое!