简体   繁体   English

用Tkinter悬停时更改按钮颜色

[英]Change button colour when hovering over with tkinter

I'm trying to make a widget with buttons that change colour when I hover over them with my cursor. 我试图制作一个带有按钮的小部件,当我将鼠标悬停在按钮上时,按钮会更改颜色。 Using code I found on the Internet I'm trying to write a small test program that can do this. 我正在尝试使用在Internet上找到的代码编写一个小型测试程序来实现此目的。 I want to be able to activate the colour switching when I left-click and deactivate it when I right-click. 我希望能够在单击鼠标左键时激活颜色切换,而在单击鼠标右键时将其禁用。 I'm able to activate it but it does not deactivate when I right-click. 我可以激活它,但是单击鼠标右键时它不会失效。

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()

How do I deactivate it so that the colour stops changing? 如何禁用它,以便颜色不再改变? Also would it be possible to make is so that it is only active while the left mouse button in pressed in? 还可以使它仅在按下鼠标左键时才处于活动状态吗?

Your do_work() method does nothing when mouse_pressed is False . mouse_pressedFalse时,您的do_work()方法不执行任何mouse_pressed You should add some functionality when it is False . 如果为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")

It is not deactivated because the buttons are still bound to the events, so you need to unbind them. 由于按钮仍绑定到事件,因此未禁用它,因此您需要取消绑定它们。 Try this: 尝试这个:

    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>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM