[racket] Changing the background color of a GUI button.

From: Kieron Hardy (kieron.hardy at gmail.com)
Date: Thu Feb 23 12:31:32 EST 2012

I was thinking I'd eventually need more than two states, and so hadn't
considered a checkbox. Happily, today's problem only requires the two
states, and so a little rework has yielded what looks to be a better
solution. Thanks for the suggestion.


On Tue, Feb 21, 2012 at 10:53 AM, Matthew Flatt <mflatt at cs.utah.edu> wrote:

> The idea of `set-state-normal' and `set-state-selected' sounds like a
> `check-box%' rather than a `button%'.
>
> Otherwise, there's no way to change the background color of a `button%'
> instance. Classes like `button%' correspond to platform-specific GUI
> widgets that typically either don't support or strongly discourage the
> possibility of changing a button's background (because button colors
> are normally determined by a theme). You can create a `canvas%' that
> draws a custom button image, in which case you have complete control
> over how the button looks and reacts, but there isn't much of an
> in-between level of customization in the current toolkit.
>
> At Tue, 21 Feb 2012 00:25:52 -0700, Kieron Hardy wrote:
> > Hi all,
> >
> > How do I change the background color (and/or style of border etc.) of a
> GUI
> > button? Given the code below, what should I put in the set-state-x
> methods
> > to indicate a difference between a normal and selected button?
> >
> > Also any comments on program style and/or substance are appreciated (I
> > apologize in advance if the layout offends).
> >
> > Cheers,
> >
> > Kieron.
> >
> > ****
> >
> > #lang racket/gui
> >
> > (define color-button%
> >   (class button%
> >     (inherit refresh)
> >
> >     (define toggle
> >       (let ([f #f])
> >         (lambda ()
> >           (set! f (not f))
> >           f
> >           )
> >         )
> >       )
> >
> >     (define (set-state-normal)
> >       (printf "color-button%: set-state-normal:\n")
> >       ; ??? ; set background color to normal
> >       (refresh)
> >       )
> >
> >     (define (set-state-selected)
> >       (printf "color-button%: set-state-selected:\n")
> >       ; ??? ; set background color to red
> >       (refresh)
> >       )
> >
> >     (define click-callback
> >       (lambda (button control-event)
> >         (printf "color-button%: click-callback:\n")
> >         (if (toggle)
> >           (set-state-selected)
> >           (set-state-normal)
> >           )
> >         )
> >       )
> >
> >     (super-new (callback click-callback))
> >
> >     )
> >   )
> >
> > (define f (new frame%
> >                [label "Colored Button"]
> >                [min-width 100]
> >                [min-height 200]
> >                )
> >   )
> >
> > (define b (new color-button%
> >               [parent f]
> >               [label "push me"]
> >               )
> >   )
> >
> > (send f show #t)
> > ____________________
> >   Racket Users list:
> >   http://lists.racket-lang.org/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120223/db0db12a/attachment.html>

Posted on the users mailing list.