

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


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


Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! terra hill


Superior post, keep up with this exceptional work. It's nice to know that this topic is being also covered on this web site so cheers for taking the time to discuss this! Thanks again and again! how much is yours worth?


Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. instagram reseller


I love the way you write and share your niche! Very interesting and different! Keep it coming! pg slot play test


This author educates that the three main schools of management thought are: the classical; the human-behavioural; and the integrative. MBA in sports management in India


Great article Lot's of information to Read...Great Man Keep Posting and update to People..Thanks sports


This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!. pencil packing work from home


Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. pencil packing job
Login to add comment