Home > Back-end >  Tkinter - Python, how do I cause a button click to assign a value to a variable?
Tkinter - Python, how do I cause a button click to assign a value to a variable?

Time:10-22

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)

CodePudding user response:

There are several ways to accomplish your goal.

One way is to write a single function that will take a value to assign to your variable. This way you can have as many buttons as you like and only a single function.

Not if you are using functions you have to either pass the variable to the function or let the function know it is in the global namespace.

import tkinter as tk


root = tk.Tk()
selection = ''


def assign_value(value):
    global selection
    selection = value
    lbl["text"] = value
    print(selection)


lbl = tk.Label(root, text='Selection Goes Here')
lbl.grid(row=0, column=0)
tk.Button(text="General Knowledge", command=lambda: assign_value("General Knowledge")).grid(row=1, column=0)
tk.Button(text="Science", command=lambda: assign_value("Science")).grid(row=2, column=0)
tk.Button(text="Entertainment", command=lambda: assign_value("Entertainment")).grid(row=3, column=0)
tk.Button(text="Miscellaneous", command=lambda: assign_value("Miscellaneous")).grid(row=4, column=0)

root.mainloop()

Or you can assign the value directly from the button.

import tkinter as tk


root = tk.Tk()
selection = tk.StringVar()
selection.set('Selection Goes Here')


lbl = tk.Label(root, textvariable=selection)
lbl.grid(row=0, column=0)
tk.Button(text="General Knowledge", command=lambda: selection.set("General Knowledge")).grid(row=1, column=0)
tk.Button(text="Science", command=lambda: selection.set("Science")).grid(row=2, column=0)
tk.Button(text="Entertainment", command=lambda: selection.set("Entertainment")).grid(row=3, column=0)
tk.Button(text="Miscellaneous", command=lambda: selection.set("Miscellaneous")).grid(row=4, column=0)

root.mainloop()

I am sure if I spent more time on this I could think up something else but the idea is basically write your code in a more DRY (Don't Repeat Yourself) fashion and make sure you are assigning the value to the variable in the global namespace or else it will not work as you expect.

  • Related