-1

I have a simplier question. However, can't get my head around it.

I basically have class with two functions. In the first function a button is created aiming at the second function when the button is pressed.

def function1(self):
   self.data = 100

   button = Button(Frame, text='I am a Button!', bg='#ffffff', command=lambda: self.function2(someVariable)).grid(row=3, column=1, sticky=W)

   self.data = 200

Before the button creation a variable is created and updated after the creation.

In the second function there is:

def function2(self, someVariable):
   print(self.data*int(someVariable))

The problem now is that the wrong data (=100) is included in the calculation of the print. But I want the updated form.

How do I get that to work?

Cheers

Soda
  • 89
  • 1
  • 8
  • Does this answer your question? [Understanding function call for tkinter button command](https://stackoverflow.com/questions/68588165/understanding-function-call-for-tkinter-button-command) – matszwecja Mar 02 '22 at 13:08
  • @matszwecja: Unfortunately not really, because it really doesn't solve the problem for me ^^ – Soda Mar 02 '22 at 13:16
  • Yes it does, you just need to find out how to apply that knowledge to your problem. Another two links to check out - [Why is the command bound to a Button or event executed when declared?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) and [How to pass arguments to a Button command in Tkinter?](https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter) – matszwecja Mar 02 '22 at 13:19
  • @matszwecja but this just explains how the command implementation as binding to a button works, but not how to update that specific attributes. – Soda Mar 02 '22 at 14:05
  • It is 200 in my test of your code. Also why do you use `Frame` (a class) as the parent of `Button(...)`? – acw1668 Mar 02 '22 at 15:42

1 Answers1

-1
class Solution:
   def function1(self):
       self.data = 100
       self.data = 200

       button = Button(Frame, text='I am a Button!', bg='#ffffff', command=lambda: self.function2(self.data)).grid(row=3, column=1, sticky=W)
       return button


   def function2(self, someVariable):
      return (self.data * int(someVariable))


cl = Solution() # to run the code 
print(cl.function1)

You need to update it before button variable because python interpreter executes line by line . on calling function2 , function 2 requers a parameter , we pass self.data to func 2 . In the above code you did a mistake

athar
  • 31
  • 4