这是有问题的代码的一部分,它有点乱,但发生的是当我运行代码时,只能使用button1。其他按钮出现,可以点击但没有任何反应。知道发生了什么事吗?我很确定它与绑定部分有关但我无法解决它,它看起来不错,但它不起作用所以......哈哈,提前谢谢
##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()
答案 0 :(得分:1)
self.button2.bind("<Button-2>", self.button2Click)
<Button-2>
指的是此事件将响应的鼠标按钮,而不是窗口小部件的名称。此按钮仅响应中间点击。
如果您希望每个按钮仅响应左键单击,请将每个按钮绑定到<Button-1>
。
您还可以使用按钮初始值设定项中的command
命名参数来指定onclick行为:
self.button1 = Button(self.canvays, command=button1Click)
...虽然如果你这样做,你将无法访问该函数中的event
值。