Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tile question

6 views
Skip to first unread message

Robert Hicks

unread,
Aug 1, 2006, 10:11:15 AM8/1/06
to
I was just creating a small ui utility script and thought I would try
out the Tile package. Normally, I have a check on an entry field that
if the data is wrong focus goes back to it and I color its background
another color. I don't see the -bg option for a ttk::entry? Does that
mean you won't be able to change the background color under Tile?

:Robert

Schelte Bron

unread,
Aug 1, 2006, 10:55:07 AM8/1/06
to
The fact that there's no -background option (tile doesn't support
the abbreviated options) on the ttk::entry doesn't mean you can't
change the background color. However, with tile you are supposed to
go about it a bit differently. Once you are used to that method
it's actually much more convenient than the Tk way.

What you would usually do is create a custom style derived from the
original TEntry widget class. You don't have to do that if you want
all entry widgets in your application to have the new behavior, but
for the moment I'll assume you only have a few entry widgets that
are supposed to work this way.

For the custom style you create a map of the attributes that should
change when the data is wrong:

style map Validate.TEntry -foreground {invalid red} \
-fieldbackground {invalid yellow}

Now by simply changing the invalid state of the entry the colors
will change:

ttk::entry .entry1 -style Validate.TEntry -validate all \
-validatecommand {
if {[string is digit %P]} {
%W state !invalid
} else {
%W state invalid
}
return 1
}

pack .entry1

Note that in some themes it may really not be possible to change the
background color of an entry widget. The code shown above works
fine on linux with the "default" theme.


Schelte.
--
set Reply-To [string map {nospam schelte} $header(From)]

Robert Hicks

unread,
Aug 1, 2006, 10:58:52 AM8/1/06
to

Schelte Bron wrote:
> Robert Hicks wrote:
<snip>

> For the custom style you create a map of the attributes that should
> change when the data is wrong:
>
> style map Validate.TEntry -foreground {invalid red} \
> -fieldbackground {invalid yellow}
>
> Now by simply changing the invalid state of the entry the colors
> will change:
>
> ttk::entry .entry1 -style Validate.TEntry -validate all \
> -validatecommand {
> if {[string is digit %P]} {
> %W state !invalid
> } else {
> %W state invalid
> }
> return 1
> }
>
Wow...that is a bit more complex than:

.e1 configure -bg "yellow"
focus .e1

I can deal though...I hope there is a book or something. Or the Welch
book is updated for this (as well as other things).

:Robert

Neil Madden

unread,
Aug 1, 2006, 11:14:56 AM8/1/06
to

It's not really that much more complicated. All that has really changed
is that you have so set-up the new style (the single [style map ...]
line), and then you use [.e1 state invalid] instead of [.e1 configure
... various options]. I'd say it makes the code clearer too.

-- Neil

Schelte Bron

unread,
Aug 1, 2006, 11:20:57 AM8/1/06
to
Robert Hicks wrote:
> Wow...that is a bit more complex than:
>
> .e1 configure -bg "yellow"
> focus .e1
>
That's not working code. My example was. The equivalent of this
snippet of code with tile would be:

.e1 state invalid
focus .e1

So, it's less complex I would say.

Robert Hicks

unread,
Aug 1, 2006, 11:25:35 AM8/1/06
to
Not quite valid...I have that in an "if" statment that pops up a
message box with a warning and then colors the background and gives it
focus.

But yes...I can see tiles way as being nice.

Robert

Schelte Bron

unread,
Aug 1, 2006, 11:40:48 AM8/1/06
to
Robert Hicks wrote:
> Not quite valid...I have that in an "if" statment that pops up a
> message box with a warning and then colors the background and
> gives it focus.
>
Sure, I was comparing apples and oranges. But so were you. So I
thought it was an acceptable thing to do :-)

To come back to your question about documentation. There is a nice
introduction into the tile widget set at:
http://tktable.sourceforge.net/tile/tile-tcl2004.pdf

Robert Hicks

unread,
Aug 1, 2006, 12:21:40 PM8/1/06
to

Schelte Bron wrote:
> Robert Hicks wrote:
> > Not quite valid...I have that in an "if" statment that pops up a
> > message box with a warning and then colors the background and
> > gives it focus.
> >
> Sure, I was comparing apples and oranges. But so were you. So I
> thought it was an acceptable thing to do :-)
>
> To come back to your question about documentation. There is a nice
> introduction into the tile widget set at:
> http://tktable.sourceforge.net/tile/tile-tcl2004.pdf
>
> Schelte.
: )

Your answer was better than my question...

:Robert

Joe English

unread,
Aug 2, 2006, 10:52:42 AM8/2/06
to

The ttk::entry widget has '-background' and '-foreground' options,
but they're not documented, because at the moment '-background'
doesn't work ('-foreground' does, though).

But see also the approach Schelte Bron describes in
(using [$e state invalid/!invalid] and [style map]).


--Joe English

Joe English

unread,
Aug 2, 2006, 10:20:12 PM8/2/06
to
Robert Hicks wrote:
>Schelte Bron wrote:
>> For the custom style you create a map of the attributes that should
>> change when the data is wrong:
>> style map Validate.TEntry -foreground {invalid red} \
>> -fieldbackground {invalid yellow}
>> Now by simply changing the invalid state of the entry the colors
>> will change:
>> ttk::entry .entry1 -style Validate.TEntry -validate all \
>> -validatecommand {
>> if {[string is digit %P]} {
>> %W state !invalid
>> } else {
>> %W state invalid
>> }
>> return 1
>> }
>
>Wow...that is a bit more complex than:
>
>.e1 configure -bg "yellow"
>focus .e1

True. For the simple cases ("I just want a yellow entry widget"),
it takes a lot more work in Tile than it does with the standard
Tk widgets.

But things are often not that simple. A more common use case is:
"I want entry widgets to be black on white normally, red on yellow
if the text is invalid, black on light grey if the widget is readonly,
and dark gray on light grey if it's disabled. On Windows, readonly
widgets should use the system background color from the users's chosen
color scheme instead of light gray; ditto for the foreground color in
disabled widgets. Same thing for OSX."

This is where the Tile theme engine starts to make things a lot easier.
(For an extreme example: take a look at how much work the core Tk
[button] implementation does to decide what the -relief should be.
This sort of thing is a *lot* simpler in Tile.)

[ttk::label]s and [ttk::frame]s still support the simple use cases
("I just want a yellow label"), and, as of a couple weeks ago,
in CVS, it even works correctly :-). [ttk::entry] widgets will
support this sort of thing too once I can get rid of some historical
baggage (a fight with Gnome and KDE that I finally figured out how
to win...)

For other Tile widgets (notably buttons), you'll still have
to do things the hard way -- but note that the "easy" way
(".b configure -bg red") doesn't *really* work with the core
widgets either.


--Joe English

0 new messages