RacketGUIDesktopWidgets
GUI Programming: Building Desktop Apps | Schema Programming Part 25
3.06 min read
Md Nasim Sheikh
Racket was born with a GUI. The DrRacket IDE itself is written in Racket using the racket/gui library, which provides native widgets on Windows, macOS, and Linux.
Advertisement
A Simple Window
#lang racket/gui
; Create a top-level frame (window)
(define frame (new frame% [label "My Racket App"]
[width 300]
[height 200]))
; Show it
(send frame show #t)
Adding Widgets
Widgets are hierarchical. Buttons go inside panels or frames.
(define my-button
(new button% [parent frame]
[label "Click Me"]
[callback (lambda (button event)
(send msg set-label "Button Clicked!"))]))
(define msg (new message% [parent frame]
[label "Waiting..."]))
Drawing on a Canvas
For custom graphics, use a canvas%.
(define my-canvas
(new canvas% [parent frame]
[paint-callback
(lambda (canvas dc)
(send dc set-scale 3 3)
(send dc set-text-foreground "blue")
(send dc draw-text "Hello GUI!" 0 0))]))
Layouts
Racket handles layout automatically using containers like horizontal-panel% and vertical-panel%.
(define panel (new horizontal-panel% [parent frame]))
(new button% [parent panel] [label "Left"])
(new button% [parent panel] [label "Right"])
Advertisement
Summary
racket/gui allows you to build completely native desktop applications without leaving the language. It's powerful enough to build complex IDEs and visual tools.
Quick Quiz
Which class represents a top-level window in the Racket GUI toolkit?
Written by
Md Nasim Sheikh
Software Developer at softexForge
Verified Author150+ Projects
Published: