title: Python Tkinter - Name is not defined
body: <p>Code:</p>
  
  <pre><code>def createLetters(frame, startX, startY, width, height, spacing):
  
      alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", 
                  "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
                  "S", "T", "U", "V", "W", "X", "Y", "Z"]
  
      def letterAction(letter):
          letter.destroy()
  
      for i in range(0, 26):
  
          if (i &gt;= 9 and i &lt;= 17):
              y = startY +  height + 2 * spacing
              x = startX + ((width + spacing) * (i - 9))
  
          elif (i &gt;= 17):
              y = startY + 2 * height + 3 * spacing
              x = (width + spacing) / 2 + startX + ((width + spacing) * (i - 18))
  
          elif (i &lt;= 8):
              y = startY + spacing
              x = startX + ((width + spacing) * i)
  
          exec(alphabet[i] + " = Button(" + frame + ", text = '" + alphabet[i] + "', command = letterAction(" + alphabet[i] + "))")
          exec(alphabet[i] + ".place(x = " + str(x) + ", y = " + str(y) + ", width = " + str(width) + ", height = " + str(height) + ")")
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Exception in Tkinter callback
  Traceback (most recent call last):
    File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
      return self.func(*args)
    File "E:\Hangman\hangmanTk.py", line 106, in playScreen
      createLetters("playFrame", 175, 250, 50, 50, 0)
    File "E:\Hangman\hangmanTk.py", line 95, in createLetters
      exec(alphabet[i] + " = Button(" + frame + ", text = '" + alphabet[i] + "', command = letterAction(" + alphabet[i] + "))")
    File "&lt;string&gt;", line 1, in &lt;module&gt;
  NameError: name 'A' is not defined
  </code></pre>
  
  <p>I'm attempting to create multiple tkinter buttons with a loop. I can create the buttons fine but i can't seem to create callbacks for them. When ever i try, it tells me that the variables that i use for the buttons are not defined. I tried adding "exec("global " + alphabet[i])" above where i defined the buttons but that didn't change anything.</p>