Hi everyone,
Is there anyway I can use (dynamic) string variable as the label of
menu ? (created by menubutton, then add command -label).
shenye...@gmail.com <shenyeh.chen@gmail.com> wrote:
Hi everyone,
Is there anyway I can use (dynamic) string variable as the label of
menu ? (created by menubutton, then add command -label).
Yes, but you have to handle it yourself instead of setting a -variable
or -textvariable option:
package require lambda ;# install Tcllib to get 'lambda' package
menubutton .mb -menu .mb.m -text "Menu Button"
pack .mb
menu .mb.m
.mb.m add command -label xyz
set ::thevar something
trace add variable ::thevar write [lambda {w idx var _ _} {
global $var
$w entryconfigure $idx -label [set $var]
} .mb.m 1]
The magic is in the lambda attached to the trace. The trace provides
an "event" signal that the variable is written into, and the code in
the lambda takes care of reconfiguring the menu entry with the new
value of the variable.
Type the above into a console, then you can manually do "set thevar
string" commands to see the menu entry label update.
Note, if you want to use this often, it is probably best to create a
wrapper proc to call that sets everything up.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 285 |
Nodes: | 16 (2 / 14) |
Uptime: | 33:10:11 |
Calls: | 6,449 |
Calls today: | 1 |
Files: | 12,052 |
Messages: | 5,254,980 |