Q:

Python 3.4.2, using Tkinter buttons to return a variable to be used by the program

I am trying to create a tkinter GUI which assigns a value to a variable when a button is pressed, then returns that value to be used in the rest of the code: 
from tkinter import *

def yes_command(ans):
    ans = 'yes' 
    window.destroy()
    return (ans)

def no_command(ans):
    ans = 'no'
    window.destroy()
    return (ans)

window = Tk()

yes_no_label = Label(window, text="Yes or no?")
yes_no_label.grid(row=0, column=1)

YESbutton = Button(window, text="Yes", fg='green', command = lambda :yes_command(ans))
YESbutton.grid(row=1, column=0)
NObutton = Button(window, text = 'No', fg = 'red', command= lambda :no_command(ans))
NObutton.grid(row=1, column=2)

window.mainloop()

print(ans)

In this, the GUI comes up and once a button is pressed, it does close. However, it does not output (as this is called outside of the function I know it would work if it printed). 
What further confuses me is that if I were to replace return (ans) with print (ans) it will print. Surely this just means it will not allow the value to leave the function? In which case, why? 
I would be grateful for any help, so thanks in advance. 

A:

I have a simple solution for your question. I use a global StringVar() variable. You can set global variable in any function and  print later.
from Tkinter import *
def yes_command():
    global answer
    answer.set('yes')
    window.destroy()

def no_command():
    global answer
    answer.set('no')
    window.destroy()

window = Tk()

yes_no_label = Label(window, text="Yes or no?")
yes_no_label.grid(row=0, column=1)

global answer
answer=StringVar()

YESbutton = Button(window, text="Yes", fg='green', command = lambda :yes_command())
YESbutton.grid(row=1, column=0)
NObutton = Button(window, text = 'No', fg = 'red', command= lambda :no_command())
NObutton.grid(row=1, column=2)

window.mainloop()

print(answer.get())

