From: Peter Freddolino (petefred_at_ks.uiuc.edu)
Date: Mon Dec 22 2008 - 14:04:35 CST

I believe you need to re-declare w inside of the command namespace, or
wrap the -command in a [namespace code ...] block to capture the current
namespace for use on the callback. Otherwise, the code inside your
-command argument executes in the global namespace and doesn't know what
$w is (see http://www.tcl.tk/man/tcl8.3/TclCmd/namespace.htm). Several
of the vmd plugins (eg., autopsf and molefacture) make use of a similar
style.

For what it looks like you're trying to do, you might want to make
$w.psfPathLab an entry instead of a label, so that users can also type
in a psf path, but that's a minor point.

Best,
Peter

Thomas Evangelidis wrote:
>
> Dear Tcl/Tk programmers,
>
> I'm preparing a GUI to incorporate it into VMD in the context of a new
> plugin. I
> have a problem related to Object Oriented Tcl and particularly
> referencing
> variable $w which points to the top level widget. The problem occurs
> when I
> try to open a .psf file (see 2 last lines of code):
>
> package provide tt_GUI 1.0
>
> namespace eval ::tt_GUI:: {
>
> namespace export startGUI
> variable w
>
> }
>
> proc ::tt_GUI::startGUI {} {
>
> variable w
>
> # make the initial window
> set w [toplevel ".tt_GUI"]
> wm title $w "Truncate Trajcetory"
> wm resizable $w 0 1
>
> label $w.psfLab -text { Select trajcetory .psf file : }
> label $w.psfPathLab -text {No file selected}
>
> button $w.psfBut -command {set types {
> {".psf Files" {.psf} }
> }
> set file [tk_getOpenFile -filetypes $types -parent .]
> $w.psfPathLab configure -text $file} \
> -text Open
>
> ....(irrelevant code)....
>
> }
>
> The error message I get is:
>
> can't read "w": no such variable can't read "w": no such variable
>
> while executing
>
> "$w.psfPathLab configure -text $file"
>
> invoked from within
>
> ".tt_GUI.psfBut invoke"
>
> ("uplevel" body line 1)
> invoked from within
>
> "uplevel #0 [ <http://wiki.tcl.tk/_edit/22154>list $w
> invoke]<http://wiki.tcl.tk/_edit/22154>
> "
>
> (procedure "tk::ButtonUp" line 22)
> invoked from within
>
> "tk::ButtonUp .tt_GUI.psfBut "
>
> (command bound to event)
>
> That should be a piece of cake for a Tcl programmer but unfortunately I'm
> not that familiar with OO Tcl. I would deeply appreciate any help.
>
> Tom
>
>
> ----- End forwarded message -----
>