Pass a variable from GUI to Python

+2 graham burgess · December 9, 2014
I have returned to programming after a gap of 40 years, and in order to familiarize myself with Python I am writing a simple database. I am using using a multientry widget from Tkinter to collect user info and then write it to a file. My problem is passing the variable with the info from the GUI to Python. If I try to print the variable in python it returns an error message Name Error: name 'ans' is not defined

Post a Reply


Oldest  Newest  Rating
+2 Vaggelis Theodoridis · December 9, 2014
I guess that this variable is defined into a scope, in a function somewhere? And you try to call it in another scope.
In order to pass it in another function or in another "scope" you should define it as global in the first function that you defined it 
:S i know, my english
Then other functions can see that variable. example
def my_func():
    global my_variable  # I defined it as global here
    my_variable = 5

def my_other_func():
   print(my_variable)   # I can see this variable cause was defined as global so i can print it

# Now in order to manimulate the variable outside my_func you should defined it again as global in the scope you want to manipulate it
# example:

def my_other_func():
  global my_variable # Except my_func i defined it also here as global

  my_variable += 5
0 graham burgess · December 10, 2014
Hi Vaggelis,

Thanks very much for your help, it worked perfectly.
I should have known, in fact at one stage i did made the variable global, but failed to rewrite the window correctly
With a name like  Theodoridis am I write in thinking your Greek.
I'm English, but retired 15 years ago and came to live in Spain
Once again your help much appreciated
0 Vaggelis Theodoridis · December 10, 2014
Im glad i have helped!
Yes i am Greek.
Well, you enjoy more often the sun now, am i right? :)
-1 graham burgess · December 15, 2014
Thanks again
Good luck
  • 1



This section is all about snakes! Just kidding.

Bucky Roberts Administrator