Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I identify the button that was pushed? 1

Status
Not open for further replies.

crvoss

Programmer
May 2, 2005
21
US
I need to identify the button the user has pushed within it's own command script. The buttons are identical but each operate on a different cell (frame). An example that somewhat demonstrates the problem follows:

Code:
proc myButtons {path} {
  button $path.btnmnu -text "Show Menu" -command {
    # Is there a way to determine the button path here (after it has been clicked)?
    # If so able then we know on which frame the selected menu item much apply to
    foreach itm [list Save Load Quit] {
      button $path.btn$itm -text $itm
      grid $path.btn$itm
    }
  }
  grid $path.btnmnu
}

for {set i 0} {$i < 10} {incr i} {
  frame .fra$i -bd 2 -relief sunken -padx 4 -pady 4
  myButtons .fra$i
  grid  .fra$i -sticky ew
}

The path to the menu items cannot be known since it is within the command script of the that button. If it is possible to identify the "Show Menu" button's path within it's own command script then the cell can be identified -- but I don't know how to do that. Can anyone help me?
 
I don't know of any way to retrieve the button's path programmatically (other than possibly by defining new bindings), but I don't understand why you can't make some button reference (either the path or an index) an argument to the command. That is, as you define the buttons, you specify the -command option to execute, say, a proc:
-command {yourProc $buttonIndex $arg}. Why wouldn't that work?

_________________
Bob Rashkin
 
I'm sorry, but I tried to work out something to that effect using the following modified code:

Code:
proc myButtons {path idx} {
	button $path.btnmnu -text "Show Menu" -command {
		yourProc $idx
	}
	grid $path.btnmnu
}

proc yourProc {idx} {
	puts "idx: $idx"
	#foreach itm [list Save Load Quit] {
	#	button $path.btn$itm -text $itm
	#	grid $path.btn$itm
	#}
}

for {set i 0} {$i < 10} {incr i} {
	frame .fra$i -bd 2 -relief sunken -padx 4 -pady 4
	myButtons .fra$i $i
	grid  .fra$i -sticky ew
}

Which produces the following error when one of the buttons is clicked:

can't read "idx": no such variable
while executing
"yourProc $idx"

My understanding is that the -command option is not resolved (variables replaced with the values they contain) until the button is clicked and that is done in the global scope. At that time and in that scope $idx no longer exists. Maybe there is a trick I don't know about or missed the idea. Any further suggestions?
 
Right. This is ultimately the difference between curly braces and quotes. It isn't advertised but the arguments to the -command option don't have to be in curly braces. Try: -command "yourProc $idx". Remember:
everything in Tcl is a string


_________________
Bob Rashkin
 
Hey now! That was the simple solution I was looking for!

Thanks once again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top