assigning variable values in tk

From: Tammer (tammer12_at_yahoo.com)
Date: 04/21/04

  • Next message: Eric J. Roode: "ANNOUNCE: Readonly 1.03"
    Date: 21 Apr 2004 08:12:55 -0700
    
    

    I'm trying to create a series of buttons in tk using a loop similar to
    this (metacode):

    sub create_buttons {
      for ($x = 1..3) {
        create button [-text => "$x", -command => sub {print
    $x}]->pack(blah)
      }
    }

    What this does is create three buttons, side-by-side, labeled "1", "2"
    and "3" (good).
    But when you click on one of the buttons, "" is returned (bad). This
    is because when the button is pressed the command "print $x" is
    evaluated, but $x has no value (the loop is completed and the sub
    exited, so no value of $x is saved).

    Even if I make $x global this doesn't fix things as it will simply
    hold the last value ("3") and all the buttons will print "3" when
    pressed (instead of "1", "2" or "3", as I want).

    Essentially the problem is that the sub {print $x} command is being
    evaluated when the button is PRESSED, but I want it evaluated when the
    button is CREATED. There has to be some (simple) way to do this, yes?


  • Next message: Eric J. Roode: "ANNOUNCE: Readonly 1.03"
  • Quantcast