Gromit
Gromit

Reputation: 61

Python tkinter, not Event error

Class xy:
 ...
 ...
 ...
    self.btns = []
    for x in enumerate(dates):
        self.btns.append(tk.Button(bf, text = x[1], height = 2, width = 12, command = lambda i = x[0]: self.show_frame(pages[i])))
        self.btns[x[0]].pack(side = "left")
        self.btns[x[0]].bind("<Button-1>", lambda i = x[0]: self.active(i)) # THERE IS AN ERROR

    self.show_frame(pages[0])


def show_frame(self, key):
    frame = self.frames[key]
    frame.tkraise()

def active(self, index):
    self.btns[index].config(relief = "sunken")

When I do this, that happen:

"TypeError: list indices must be integers or slices, not Event"I'm trying figure it out for hour.

Upvotes: 1

Views: 86

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385820

When you use bind, tkinter will automatically pass an event object as the first parameter. Even though you're using lambda to set a default for i, tkinter always sets it to the event object.

You need to change your lambda to this:

lambda event, i=x[0]: self.active(i)

Upvotes: 3

Related Questions