Home > Archive > Tcl > June 2005 > How to change the color of the Tk entry based on Variable value?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to change the color of the Tk entry based on Variable value?
|
|
| manozp@gmail.com 2005-06-09, 8:58 pm |
| Hi,
I want to change the color/style of the entry in Tk based upon the
variable value of the entry.
For example...
label .top.l -text Command: -padx 0
entry .top.cmd -width 20 -relief sunken -textvariable var
pack .top.l -side left
pack .top.cmd -side left -fill x -expand true
If the value of var is greater than 0, I want the entry and label to be
displayed in grey. Else, they should be in red.
Can somebody tell me how I can do this?
Thanks in advance.
-Manoj.
| |
| Kaitzschu 2005-06-09, 8:58 pm |
| On Thu, 9 Jun 2005 manozp@gmail.com wrote:
> Hi,
>
> I want to change the color/style of the entry in Tk based upon the
> variable value of the entry.
>
> For example...
> label .top.l -text Command: -padx 0
> entry .top.cmd -width 20 -relief sunken -textvariable var
> pack .top.l -side left
> pack .top.cmd -side left -fill x -expand true
>
> If the value of var is greater than 0, I want the entry and label to be
> displayed in grey. Else, they should be in red.
>
> Can somebody tell me how I can do this?
One way is to use entry validation:
label .top.l -text Command: -padx 0 -foreground gray
entry .top.cmd -width 20 -relief sunken -textvariable var\
-validate key -vcmd {evcc %P} -foreground gray
proc evcc {str} {
set c red
# we can use strict here, empty string is not greater than zero
if {[string is integer -strict $str] &&\
[scan $str %d] > 0} {set c gray}
.top.cmd configure -foreground $c
.top.l configure -foreground $c
return 1
}
However, this will fail when number grows beyond integer. But it is
something to start from. And that [scan] hides the fact that your user may
be inputting something so weird.
But everyone [scan]s inputs anyway, don't you.
--
-Kaitzschu
s="TCL ";while true;do echo -en "\r$s";s=${s:1:${#s}}${s:0:1};sleep .1;done
|
|
|
|
|