0
Tkinter - Python, how do I cause a button click to assign a value to a variable?

Using Tkinter and Python. Already created a window for the buttons to be placed on. I want there to be four buttons to appear, and I want to be able to click one of the four buttons, and be able for it to set the selection variable = "whatever I clicked", so that I can then use this variable later to call an API. When I run the program and click on the "General knowledge" button and print the selection, it does correctly print "General knowledge", but then when I try to return this selection variable it just doesn't work and I don't know why.

def select1():
    selection = "General Knowledge"
    print(selection)

def select2():
    selection = "Science"

def select3():
    selection = "Entertainment"

def select4():
    selection = "Miscellaneous"



button1 = tk.Button(text = "General Knowledge", command = select1)
button1.place(x=100, y=100)

button2 = tk.Button(text = "Science", command = select2)    
button2.place(x=100, y=140)

button3 = tk.Button(text = "Entertainment", command = select3)
button3.place(x=100, y=180)

button4 = tk.Button(text = "Miscellaneous", command = select4)
button4.place(x=100, y=220)


Python 21-10-22, 5:53 p.m. likithsai
0
I will follow your blogs and share them with everyone since this is a fantastic source of knowledge. I think highly of the author of this piece; you are quite skilled. I hope you will spread the word about them and find more success. fnf
24-10-22, 10 a.m. leemarryu


Log-in to answer to this question.