s c h e m a t i c s : c o o k b o o k

/ Cookbook.GUIHelloWorld

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Creating a window containing text

Problem

You want to create a GUI window containing some text.

Solution

PltScheme provides a cross-platform GUI toolkit called MrEd. MrEd builds upon the PltScheme's default object system, contained in the class.ss library in MzLib?.

Code to create a window and display the text "Hello World!" is:

(require (lib "mred.ss" "mred")
         (lib "class.ss" "mzlib"))

(define the-frame
  (new frame%
       (label "Hello!")
       (width 400)
       (height 400)))

(define the-pane
  (new pane%
       (parent the-frame)))

(define the-message
  (new message%
       (label "Hello World!")
       (parent the-pane)))

(send the-frame show #t)

Discussion

Like most GUI systems, MrEd consists of a hierarchy of graphical containers and containees.

A frame is a top-level container (meaning it has no parent container) that corresponds to a window. The code to create a frame with the given label (titlebar), height, and width is:

(define the-frame
  (new frame%
       (label "Hello!")
       (width 400)
       (height 400)))

A pane is another container that exists only to manage the layout of its children objects. In this example we don't need a pane, as with only one child, the message, there is no layout to manage. More complex examples will need to use panes. The code to create a pane is given below. Note that the parent is set to the frame.

(define the-pane
  (new pane%
       (parent the-frame)))

The message is an containee object that displays a text string. The code to create it is given below. Note how the parent is set to the pane. We could change the pane to the frame and this example would still work.

(define the-message
  (new message%
       (label "Hello World!")
       (parent the-pane)))

To actually get a frame drawn on the screen it is necessary to tell the frame to draw itself. This is done by sending the frame a draw message:

(send the-frame show #t)


Comments about this recipe

I like this example. My previous one had "instantiate widget% ()" which I found too verbose and not nicely indented. BobMcIsaac - 20 Nov 2005

Contributors

-- NoelWelsh - 10 May 2005

CookbookForm
TopicType: Recipe
ParentTopic: GUIChapter
TopicOrder: 001

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest