-1

I made an array of buttons using for loops for a date picking widget:

current = 1
for c in range(7)
    for r in range(5)
        b = Button(date_picker_frame, text=str(current), command=lambda: clicked(value_to_pass))

def clicked(value):
    self.date = date(self.year, self.month, value)

The text on each of the buttons is the day they represent. I want them to pass their text as an int to the clicked function so it can be saved as a date.

Unfortunately, I can't figure out how to.

I've tried b['text'], but that always returns the last value of the month because the reference to the button gets lost as the for loop iterates. Similarly, the methods that involve using the reference b to call something results in the last day of the month being used.

I was thinking of using a method much like an a listboxselect event:

listbox.bind("<<ListboxSelect>>", function_)

But I'm not sure how to. Any solutions?

Amaranth
  • 53
  • 4
  • Can you check https://stackoverflow.com/questions/7299955/tkinter-binding-a-function-with-arguments-to-a-widget ? – Peter Majko Aug 12 '17 at 00:22

1 Answers1

0

Why don't you just bind to the <1> event? After setting

b = Button( ... )

add

b.bind('<1>', lambda event: clicked(event.widget.cget('text'))

The command parameter of a Button doesn't seem to have any easy way of referencing the button the command was called on.