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()

Attached Files

Thumbnail(s)
   
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
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
  Function will not return variable that I think is defined Oldman45 6 1,238 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 1,116 Aug-12-2020, 10:34 PM
Last Post: snippsat
  python library not defined in user defined function johnEmScott 2 919 May-30-2020, 04:14 AM
Last Post: DT2000
  Error: variable can not be defined julio2000 2 1,386 Feb-09-2020, 08:51 PM
Last Post: julio2000
  Variable defined but python wont recognize it. FWendeburg 3 1,627 Feb-19-2019, 10:43 PM
Last Post: woooee
  How do you make functions that take a variable that is not defined? magic 6 2,138 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  If statement variable not defined MTom5 3 3,425 Sep-03-2018, 04:31 PM
Last Post: ichabod801
  User defined method as variable or argument AeranicusCascadia 5 2,345 Dec-13-2017, 04:47 PM
Last Post: AeranicusCascadia
  Variable not defined in password checker DAS 4 2,584 Aug-27-2017, 08:40 PM
Last Post: ichabod801
  input defines variable but it's not defined tozqo 5 4,363 Jun-05-2017, 02:45 AM
Last Post: tozqo

Forum Jump:

User Panel Messages

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