Skip to content
Advertisement

Change the value of a variable with a button (Tkinter)

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:

JavaScript

How i can do that? (I need to do that for six buttons, making six functions it’s not an option)

Advertisement

Answer

i need to make this for 6 buttons…

If each button modifies the same global variable, then have make_something accept a value parameter:

JavaScript

If each button modifies a different global, then condense all your globals into a single global dict, which make_something can then modify.

JavaScript

In either case, you still only require one function.


If lambdas aren’t to your taste, you could use nested functions to create separate callables for each command:

JavaScript

… And you can avoid globals by instead using attributes of a class instance:

JavaScript

By the way, don’t do this:

JavaScript

This assigns the result of pack() to myButton, so myButton will be None instead of referring to your button. Instead, do:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement