I want to change the value of a variable just with a button, i don't want to create a new entire function just like that:
from Tkinter import *
variable = 1
def makeSomething():
global variable
variable = 2
root = Tk()
myButton = Button(root, text='Press me',command=makeSomething).pack()
How i can do that? (I need to do that for six buttons, making six functions it's not an option)
If each button modifies the same global variable, then have
makeSomething
accept avalue
parameter:If each button modifies a different global, then condense all your globals into a single global dict, which
makeSomething
can then modify.In either case, you still only require one function.
By the way, don't do this:
This assigns the result of
pack()
to myButton, so myButton will beNone
instead of referring to your button. Instead, do: