Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable not defined
#1
Dear Python Users,

The following program creates a command button. I defined a function called ans1 to give to the command button. It assigns the number 44 to the variable AnswerSelected. When I click on the command button it prints 44. However, at the end of the program it it supposed to print AnswerSelected. At this point it tells me that AnswerSelected is not defined.

What I am trying to do is use command buttons to assign numbers to variables. Any help would be appreciated.

#Create a button widget
from tkinter import *
root = Tk()

def ans1():
    AnswerSelected = 44
    print (AnswerSelected)

Answer1 =Button(root,text="Click me", padx=50, pady=50, bg ="red", command =ans1)
Answer1.grid( row =5, column = 5)
root.mainloop()
print (AnswerSelected)
Reply
#2
Prints in the console for me. If you want it to print in a label then you will have to let it know to.
from tkinter import *
root = Tk()

def ans1():
    AnswerSelected = 44
    label['text'] = AnswerSelected
    print (AnswerSelected)

label = Label(root)
label.grid(column=5, row=4)

Answer1 =Button(root,text="Click me", padx=50, pady=50, bg ="red", command =ans1)
Answer1.grid( row =5, column = 5)
root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
I should have explained my question better. My question is why does AnswerSelected not have a value when it gets to the end of the program?
Reply
#4
Because it's local to the ans1 function. You need to read about scope. It's also a good idea to avoid global scope for variables if you can.
Reply
#5
Thanks that helps. You are right. I decided to read more about scope and also functions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 2 54 2 hours ago
Last Post: CoderMerv
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 511 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,160 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,041 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Function will not return variable that I think is defined Oldman45 6 3,434 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,160 Aug-12-2020, 10:34 PM
Last Post: snippsat
  python library not defined in user defined function johnEmScott 2 3,766 May-30-2020, 04:14 AM
Last Post: DT2000
  Error: variable can not be defined julio2000 2 3,137 Feb-09-2020, 08:51 PM
Last Post: julio2000
  Variable defined but python wont recognize it. FWendeburg 3 3,254 Feb-19-2019, 10:43 PM
Last Post: woooee
  How do you make functions that take a variable that is not defined? magic 6 4,328 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020