user393267
user393267

Reputation:

Tkinter: change class variable when pressing a button

I am writing a function that get called when I click a button, but before calling this function, I would like to change a class variable.

How do you do that in a button ? As now I did declare the button as following:

calculatebutton = Button(self, text="run function", command=self.calculate_total(self.totals))
calculatebutton.place(x=150, y=600)

This run my function, but I want to change also a class variable, and I want to change it before I run the function.

Upvotes: 0

Views: 506

Answers (1)

Lafexlos
Lafexlos

Reputation: 7735

You can wrap your method with new one and in it, before calling your method, you can do whatever you want.

def wrapper(self):
    self.your_value = new_value
    self.calculate_total(self.totals)

calculatebutton = Button(self, text="run function", command=self.wrapper)
calculatebutton.place(x=150, y=600)

Also note that, if you want to pass an argument to your method when calling from button, you should use lambdas.

Upvotes: 1

Related Questions