robotools / vanilla

A Pythonic wrapper around Cocoa.
MIT License
78 stars 28 forks source link

Button Callback function with arguments auto run #166

Closed wsimm closed 2 years ago

wsimm commented 2 years ago

Hi, when I create a button with a callback function with arguments it auto runs the function assigned

`from vanilla import Window, Button

class ButtonDemo:

 def __init__(self):
     self.w = Window((100, 40))
     self.w.button = Button((10, 10, -10, 20), "A Button",
                        callback=self.buttonCallback('somedata'))
     self.w.open()

 def buttonCallback(self, sender):
     print("button hit!")

ButtonDemo()`

justvanrossum commented 2 years ago

You have to assign the callback without the ('somedata') bit (which is calling it on the spot).

from vanilla import Window, Button

class ButtonDemo:

 def __init__(self):
     self.w = Window((100, 40))
     self.w.button = Button((10, 10, -10, 20), "A Button",
                        callback=self.buttonCallback)
     self.w.open()

 def buttonCallback(self, sender):
     print("button hit!")

ButtonDemo()