1
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.
2
  • Did the error message tell you what line the problem was on? If we knew it might be easier to identify the problem. Commented Mar 13, 2016 at 23:58
  • Line with a method active(), self.btns[index].config(relief = "sunken") Commented Mar 14, 2016 at 0:00

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.