Main Content

Multiple buttons calling the same proc in wish (tcl/tk)

Archive - Originally posted on "The Horse's Mouth" - 2012-01-12 23:30:28 - Graham Ellis

Question:

1. I have created a table dynamically using a for loop in Tcl/Tk.
2. One of the columns in the table is a column of buttons
3. All the buttons call the same action proc as the each do something similar

How can I set up the buttons so that I know which was pressed in the action proc?

Answer:

Pass in a parameter to the proc that includes the button number.

Example:

  proc report {value} {
    puts "Pressed button $value"
    }
  for {set k 1} {$k < 6} {incr k} {
    button .x$k -text "Button $k" -command "report $k"
    pack .x$k
    }
  button .quit -text Exit -command exit
  pack .quit