title: Tkinter lambda function
body: <p>(As the 'homework' tag indicates, this is part of a <em>big</em> project in Computer Science.)</p>
  
  <p>I am writing a <em>Jeopardy!</em> simulation in Python with tkinter, and I'm having a big problem regarding the use of the lambda function in buttons. Assume <code>root = Tk()</code> and <code>categories</code> is a list.</p>
  
  
  
  <pre class="lang-python prettyprint-override"><code># Variable to keep the buttons
  root._buttons = {}
  
  # Display headers on top of page
  for i in range(5):
      # Get category name for display in main window
      name = categories[i]
      b = Label(root, text=fill(name.upper(), 10), width=18, height=3,\
          bg="darkblue", fg="white", font=("Helvetica bold", "", 11))
      b.grid(row=0, column=i)
  
      # Create list of buttons in that variable (root._buttons)
      btnlist = [None]*5
  
      # Display individual questions
      for j in range(5):
  
          # Make a button for the question
          b = Button(root, text="$" + str(200 * (j+1)), width=8, height=1,
              bg="darkblue", fg="orange", font=("Impact", "", 30))
          b.cat = name
          b.value = 200 * (j + 1)
          b.sel = lambda: select(b.cat, b.value)
  
          # Add callback event to button
          print(b.cat, b.value, b.sel)
          b.config(command=b.sel)
  
          # Add button to window
          b.grid(row=j+1, column=i)
  
          # Append to list
          btnlist[j] = b
  
      root._buttons[categories[i]] = btnlist
  </code></pre>
  
  <p>For all of the code, see <a href="http://chancehenrik.x10.mx/code/view.php?file=jeopardy.py" rel="nofollow">my little Code Viewer (under construction!)</a></p>
  
  <p>It's at <code>lambda: select(b.cat, b.value)</code> where the problem seems to occur, because when I click any button on the board, it always goes to the one last button on the board. I've tried other approaches, unfortunately all using <code>lambda</code>, and I have not seen any approach that does not involve <code>lambda</code>.</p>