title: Change variable with button in tkinter
body: <p>The code the problem is part of is fairly big so I'm drafting a fingerpainting version here.</p>
  
  <pre><code>import tkinter
  
  variable = "data"
  
  def changeVariable():
      variable = "different data"
  
  def printVariable():
      print(variable)
  
  window = tkinter.Tk
  button1 = tkinter.Button(window, command=changeVariable)
  button1.pack()
  button2 = tkinter.Button(window, command=printVariable)
  button2.pack()
  </code></pre>
  
  <p>So in this example, I press the first button to change 'variable', then the second button to print it.  But "data" is printed instead of "different data".  I searched around a bit and decided to use <strong>global</strong> before defining the variable in both the main code and in the functions, so the code looked like this.</p>
  
  <pre><code>import tkinter
  
  global variable
  variable = "data"
  
  def changeVariable():
      global variable
      variable = "different data"
  
  def printVariable():
      global variable
      print(variable)
  
  window = tkinter.Tk()
  button1 = tkinter.Button(window, command=changeVariable)
  button1.pack()
  button2 = tkinter.Button(window, command=printVariable)
  button2.pack()
  
  window.mainloop()
  </code></pre>
  
  <p>But now it says 'name 'variable' is not defined'.</p>
  
  <p>Essentially, how can I get the variable 'variable' to change with a button in tkinter?  Was I wrong to think of using <strong>global</strong>?</p>