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

/ Cookbook.GUIHowToUseATimer

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

How to use a timer

Problem

How to use a timer to update elements of a GUI.

Solution

;;;
;;; Clock
;;;

;; This program demonstrates how to use a timer
;; together with a GUI.

(require (lib "date.ss"))

;;; GUI

; The GUI consists of a frame with the title "Clock".
; The contents of the frame is a message, where the
; time is written every second.

(define frame (make-object frame% "Clock"))
(define time-message (make-object message% "The time is written here" frame))
(send frame show #t)


;;; DATE AND STRING FUNCTIONS

; pad2 makes sure the string length is 2 by padding with
; an extra 0, if neccessary
(define (pad2 s)
  (if (< (string-length s) 2)
      (string-append "0" s)
      s))

; time->string : -> string
;   returns a string with the current time
(define (time->string)
  (let* ([date     (seconds->date (current-seconds))]
         [seconds  (number->string (date-second date))]
         [minutes  (number->string (date-minute date))]
         [hours    (number->string (date-hour date))] )         
    (string-append (pad2 hours)
                   ":"
                   (pad2 minutes)
                   ":"
                   (pad2 seconds))))

;;; TIMER

; The timer calls the callback function every
; 1000 milliseconds (that is every second)

(instantiate timer% () 
  (notify-callback (lambda ()
                     (send time-message set-label (time->string))))
  (interval 1000)
  (just-once? #f) )

Discussion


Comments about this recipe

Contributors

-- JensAxelSoegaard - 24 Feb 2006

CookbookForm
TopicType: Recipe
ParentTopic: GUIRecipes
TopicOrder: 005

 
 
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