0

this is the part of the code in question, its a bit messy but what happens is when i run the code only button1 can be used. the other buttons turn up and can be clicked but nothing happens. any idea what going on? im pretty sure it has something to do with the bind part but i cant fix it, it looks fine to me but it doesnt work so... haha thanks in advance

##Buttons##
    self.button1 = Button(self.canvays)
    self.button1.configure(text="Pride", background="red")
    self.button1.pack(side=RIGHT)
    self.button1.focus_force()
    self.button1.place(x=85, y=367)
    self.button1.bind("<Button-1>", self.button1Click)

    self.button2 = Button(self.canvays)
    self.button2.configure(text="Murder", background="blue")
    self.button2.pack(side=TOP)
    self.button2.focus_force()
    self.button2.place(x=225, y=367)
    self.button2.bind("<Button-2>", self.button2Click)

    self.button3 = Button(self.canvays)
    self.button3.configure(text="Lions", background="Yellow")
    self.button3.pack(side=TOP)
    self.button3.focus_force()
    self.button3.place(x=380, y=367)
    self.button3.bind("<Button-3>", self.button3Click)



    self.root.mainloop()

def incorrect(self):
    self.labelincorrect = Label(text = "Sorry that wasnt right, try again")
    self.labelincorrect.pack(side=TOP)
    self.labelincorrect.place(x=250, y=250)
def correct(self):
    self.score = self.score+1

##Button Event##

def button3Click(self, event):
    self.incorrect()
def button2Click(self, event):
    self.incorrect()
def button1Click(self, event):
    self.root.destroy()
    self.correct()
    Question2()
1
  • Are you aware that you're binding the mouse wheel button to button2, and right-click to button3? If you want normal behavior, you don't have to bind anything to buttons at all. They behave like buttons by default. Please see a tutorial on Tkinter for more info. Commented Nov 18, 2015 at 13:27

1 Answer 1

1
self.button2.bind("<Button-2>", self.button2Click)

<Button-2> refers to the mouse button that this event will respond to, not the name of your widget. This button will only respond to middle clicks.

If you want each button to respond only to left clicks, bind each of them to <Button-1>.

You can also specify onclick behavior by using the command named argument in the button's initializer:

self.button1 = Button(self.canvays, command=button1Click)

... Although if you do this, you won't have access to the event value in the function.

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.