Home > Archive > Tcl > November 2006 > Trouble with updating a label
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 |
Trouble with updating a label
|
|
| Olaf Schmidt 2006-11-24, 4:03 am |
| Hi folks,
after my last question showed clearly my incapability in reading the
TclTk documentation I fear that my next question is equally dumb.
Nevertheless here it goes.
I have an entry widget which defines a text variable. Every time I press
return in that widget a label widget should get updated. For the purpose
of this test only the content of the textvariable.
for this i have written the following which ly does not even do the
simplest thing (namely create the label and entry widget):
proc bindprob { } {
set topl [toplevel .top]
set framewidget [frame $topl.frame]
set displayvalue 0.1
set entryvalue 10
entry $framewidget.entry -textvariable [format "test %s" $entryvalue]
label $framewidget.label -textvariable [format "test2 %s"
$displvalue]
grid $framewidget.label -column 0 -row 0
grid $framewidget.entry -column 0 -row 1
set displvalue $entryvalue # <- that is so completedly wrong....
update idletasks
}
Apart from not showing the entry and label my main concern is the
updating label with the entryvalue. I suspect i have to work with
bindings but there are some other things which hinder my progress.
1)bind $framewidget.entry <KeyPress-Return> {puts "Return"}
does nothing... or rather i see nothing on standard out.
2) The corresponding version in my other code (where the label and entry
are displayed) the line
bind $framewidget.entry <KeyPress-Return> {$framewidget.label configure
-text $displvalue}
gets the errormessage can't read "framewidget" no such variable. if I
explecitedly name it like this
bind $framewidget.entry <KeyPress-Return> {.top.frame.label configure
-text $displvalue}
the error is not displayed but the label is not updated.
This is probably the worst errordiscription/question I have put forth in
ages but here you go.
Olaf
-----------
email: supstitute "nospam" with my surename and germany with "de"
| |
| Uwe Klein 2006-11-24, 4:03 am |
| Olaf Schmidt wrote:
> Hi folks,
> after my last question showed clearly my incapability in reading the
> TclTk documentation I fear that my next question is equally dumb.
C.l.t is well known for not beheading those that come humbly with questions ;-)
> Nevertheless here it goes.
>
> I have an entry widget which defines a text variable. Every time I press
> return in that widget a label widget should get updated. For the purpose
> of this test only the content of the textvariable.
>
> for this i have written the following which ly does not even do the
> simplest thing (namely create the label and entry widget):
>
> proc bindprob { } {
>
> set topl [toplevel .top]
> set framewidget [frame $topl.frame]
>
> set displayvalue 0.1
> set entryvalue 10
>
> entry $framewidget.entry -textvariable [format "test %s" $entryvalue]
> label $framewidget.label -textvariable [format "test2 %s" $displvalue]
the -textvariable option needs a variable "name" ( "textvariable", see ;-)
this variable will be at global scope.
accessing this variable in a proc needs either
global <varname>
or upvar #0 <varname> <localvar>
or use ::<varname>
i.e entry .myentry -textvariable ::mytextvariable
set ::mytextvariable "goddamm value i want"
and to copy content form entry to label:
set ::labelvar $::entryvar
>
> grid $framewidget.label -column 0 -row 0
> grid $framewidget.entry -column 0 -row 1
>
> set displvalue $entryvalue # <- that is so completedly wrong....
not at all ;-), mind the scope though!
> update idletasks
>
> }
> Apart from not showing the entry and label my main concern is the
> updating label with the entryvalue. I suspect i have to work with
> bindings but there are some other things which hinder my progress.
> 1)bind $framewidget.entry <KeyPress-Return> {puts "Return"}
> does nothing... or rather i see nothing on standard out.
>
> 2) The corresponding version in my other code (where the label and entry
> are displayed) the line
>
> bind $framewidget.entry <KeyPress-Return> {$framewidget.label configure
> -text $displvalue}
at invokation time and scope of the binding "$framewidget" is unknown
[ list $framewidget.label configure -text $displvalue] ;# which is still rubbish
bind $framewidget.entry <KeyPress-Return> {set ::labelvar $::entryvar }
would do your bidding
uwe
| |
| Ralf Fassel 2006-11-24, 8:02 am |
| * "Olaf Schmidt" <nospam@ifg.uni-hannover.germany>
| proc bindprob { } {
--<snip-snip>--
|
| set displayvalue 0.1
| set entryvalue 10
This sets two local variables in this proc. They are not visible at
global scope and will not get used when bound to -textvariable (since
-textvariable uses global scope for resolving variable names).
| entry $framewidget.entry -textvariable [format "test %s" $entryvalue]
| label $framewidget.label -textvariable [format "test2 %s" $displvalue]
This creates one global variable named "test 10" (variable _name_
includes a SPACE character) and then raises an error since the
variable 'displvalue' is not defined. You set _displayvalue_ above
(different spelling).
Most probably you want something along the lines of
# set _global_ variables to initial values
set ::displayvalue 0.1
set ::entryvalue 10
# now use them as textvariables:
entry $framewidget.entry -textvariable entryvalue
label $framewidget.label -textvariable displayvalue
To transfer the value from the entry to the label, you could use
bind $framewidget.entry <Return> {set displayvalue $entryvalue}
Note that you cannot use [list] here to construct the bind command
(which is usually recommended), since 'list' will quote the '$' so it
does not expand later.
R'
| |
| Bryan Oakley 2006-11-24, 7:02 pm |
| Olaf Schmidt wrote:
> Hi folks,
> after my last question showed clearly my incapability in reading the
> TclTk documentation I fear that my next question is equally dumb.
> Nevertheless here it goes.
>
> I have an entry widget which defines a text variable. Every time I press
> return in that widget a label widget should get updated. For the purpose
> of this test only the content of the textvariable.
>
> for this i have written the following which ly does not even do the
> simplest thing (namely create the label and entry widget):
>
> proc bindprob { } {
>
> set topl [toplevel .top]
> set framewidget [frame $topl.frame]
>
> set displayvalue 0.1
> set entryvalue 10
>
> entry $framewidget.entry -textvariable [format "test %s" $entryvalue]
> label $framewidget.label -textvariable [format "test2 %s"
> $displvalue]
>
> grid $framewidget.label -column 0 -row 0
> grid $framewidget.entry -column 0 -row 1
>
> set displvalue $entryvalue # <- that is so completedly wrong....
> update idletasks
>
> }
Others have mentioned the problems you have with the -textvariable
option, variable misspellings, etc.
One thing I don't think anyone else has mentioned is that it doesn't
look like you are packing/placing/gridding $topl.frame. You are placing
the label and entry in this frame OK but you aren't making the frame as
a whole visible.
|
|
|
|
|