使用 tkinter 悬停时更改按钮颜色
Change button colour when hovering over with tkinter
我正在尝试制作一个带有按钮的小部件,当我将鼠标悬停在按钮上时,这些按钮会改变颜色。使用我在 Internet 上找到的代码,我正在尝试编写一个可以执行此操作的小型测试程序。我希望能够在左键单击时激活颜色切换并在右键单击时停用它。我可以激活它,但当我右键单击时它不会停用。
import tkinter
class App:
def __init__(self, root):
self.root = root
self.mouse_pressed = False
self.root.bind("<ButtonPress-1>", self.OnMouseDown)
self.root.bind("<ButtonRelease-3>", self.OnMouseUp)
self.Hover1 = tkinter.Button(root,text="Red color", bg="SystemButtonFace")
self.Hover1.pack()
self.Hover2 = tkinter.Button(root,text="Yellow color", bg="SystemButtonFace")
self.Hover2.pack()
def do_work(self):
if self.mouse_pressed:
self.Hover1.bind("<Enter>", lambda event, h=self.Hover1: h.configure(bg="red"))
self.Hover1.bind("<Leave>", lambda event, h=self.Hover1: h.configure(bg="SystemButtonFace"))
self.Hover2.bind("<Enter>", lambda event, h=self.Hover2: h.configure(bg="yellow"))
self.Hover2.bind("<Leave>", lambda event, h=self.Hover2: h.configure(bg="SystemButtonFace"))
def OnMouseDown(self, event):
self.mouse_pressed = True
self.do_work()
def OnMouseUp(self, event):
self.mouse_pressed = False
self.do_work()
root=tkinter.Tk()
app = App(root)
root.mainloop()
如何停用它以使颜色停止变化?也可以使它仅在按下鼠标左键时处于活动状态吗?
当 mouse_pressed
为 False
时,您的 do_work()
方法不执行任何操作。当它是 False
.
时,您应该添加一些功能
def do_work(self):
if self.mouse_pressed:
...
else:
#unbind events from both buttons
self.Hover1.unbind("<Enter>")
self.Hover1.unbind("<Leave>")
self.Hover2.unbind("<Enter>")
self.Hover2.unbind("<Leave>")
#return their color to original state
self.Hover1.configure(bg="SystemButtonFace")
self.Hover2.configure(bg="SystemButtonFace")
没有去激活,因为按钮还绑定了事件,需要解绑。试试这个:
def do_work(self):
if self.mouse_pressed:
self.Hover1.bind("<Enter>", lambda event, h=self.Hover1: h.configure(bg="red"))
self.Hover1.bind("<Leave>", lambda event, h=self.Hover1: h.configure(bg="SystemButtonFace"))
self.Hover2.bind("<Enter>", lambda event, h=self.Hover2: h.configure(bg="yellow"))
self.Hover2.bind("<Leave>", lambda event, h=self.Hover2: h.configure(bg="SystemButtonFace"))
else:
self.Hover1.unbind("<Enter>")
self.Hover1.unbind("<Leave>")
self.Hover2.unbind("<Enter>")
self.Hover2.unbind("<Leave>")
我正在尝试制作一个带有按钮的小部件,当我将鼠标悬停在按钮上时,这些按钮会改变颜色。使用我在 Internet 上找到的代码,我正在尝试编写一个可以执行此操作的小型测试程序。我希望能够在左键单击时激活颜色切换并在右键单击时停用它。我可以激活它,但当我右键单击时它不会停用。
import tkinter
class App:
def __init__(self, root):
self.root = root
self.mouse_pressed = False
self.root.bind("<ButtonPress-1>", self.OnMouseDown)
self.root.bind("<ButtonRelease-3>", self.OnMouseUp)
self.Hover1 = tkinter.Button(root,text="Red color", bg="SystemButtonFace")
self.Hover1.pack()
self.Hover2 = tkinter.Button(root,text="Yellow color", bg="SystemButtonFace")
self.Hover2.pack()
def do_work(self):
if self.mouse_pressed:
self.Hover1.bind("<Enter>", lambda event, h=self.Hover1: h.configure(bg="red"))
self.Hover1.bind("<Leave>", lambda event, h=self.Hover1: h.configure(bg="SystemButtonFace"))
self.Hover2.bind("<Enter>", lambda event, h=self.Hover2: h.configure(bg="yellow"))
self.Hover2.bind("<Leave>", lambda event, h=self.Hover2: h.configure(bg="SystemButtonFace"))
def OnMouseDown(self, event):
self.mouse_pressed = True
self.do_work()
def OnMouseUp(self, event):
self.mouse_pressed = False
self.do_work()
root=tkinter.Tk()
app = App(root)
root.mainloop()
如何停用它以使颜色停止变化?也可以使它仅在按下鼠标左键时处于活动状态吗?
当 mouse_pressed
为 False
时,您的 do_work()
方法不执行任何操作。当它是 False
.
def do_work(self):
if self.mouse_pressed:
...
else:
#unbind events from both buttons
self.Hover1.unbind("<Enter>")
self.Hover1.unbind("<Leave>")
self.Hover2.unbind("<Enter>")
self.Hover2.unbind("<Leave>")
#return their color to original state
self.Hover1.configure(bg="SystemButtonFace")
self.Hover2.configure(bg="SystemButtonFace")
没有去激活,因为按钮还绑定了事件,需要解绑。试试这个:
def do_work(self):
if self.mouse_pressed:
self.Hover1.bind("<Enter>", lambda event, h=self.Hover1: h.configure(bg="red"))
self.Hover1.bind("<Leave>", lambda event, h=self.Hover1: h.configure(bg="SystemButtonFace"))
self.Hover2.bind("<Enter>", lambda event, h=self.Hover2: h.configure(bg="yellow"))
self.Hover2.bind("<Leave>", lambda event, h=self.Hover2: h.configure(bg="SystemButtonFace"))
else:
self.Hover1.unbind("<Enter>")
self.Hover1.unbind("<Leave>")
self.Hover2.unbind("<Enter>")
self.Hover2.unbind("<Leave>")