Jul-10-2020, 01:15 AM
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.
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)