简体   繁体   English

Tkinter 悬停在按钮上-> 颜色变化

[英]Tkinter Hovering over Button -> Color change

Is there a possibility to change the background-color of a Button after hovering on it?悬停在Button上后是否有可能更改按钮的背景颜色? What is the code for this in Tkinter? Tkinter 中的代码是什么?

Sadly the activebackground and activeforeground options only seem to work when you are clicking on the button rather than when you hover over the button.遗憾的是, activebackgroundactiveforeground选项似乎只在您单击按钮时才起作用,而不是在您将鼠标悬停在按钮上时。 Use the <Leave> and <Enter> events instead使用<Leave><Enter>事件代替

import tkinter as tk

def on_enter(e):
    myButton['background'] = 'green'

def on_leave(e):
    myButton['background'] = 'SystemButtonFace'

root = tk.Tk()
myButton = tk.Button(root,text="Click Me")
myButton.grid()


myButton.bind("<Enter>", on_enter)
myButton.bind("<Leave>", on_leave)

root.mainloop()

As pointed out in the comments, if we want multiple buttons, we can bind the buttons to functions that use the event data for the click event to change the background of the button.正如评论中指出的,如果我们想要多个按钮,我们可以将按钮绑定到使用单击事件的事件数据来更改按钮背景的函数。

import tkinter as tk

def on_enter(e):
    e.widget['background'] = 'green'

def on_leave(e):
    e.widget['background'] = 'SystemButtonFace'

root = tk.Tk()
myButton = tk.Button(root,text="Click Me")
myButton.grid()


myButton.bind("<Enter>", on_enter)
myButton.bind("<Leave>", on_leave)

myButton2 = tk.Button(root,text="Click Me")
myButton2.grid()


myButton2.bind("<Enter>", on_enter)
myButton2.bind("<Leave>", on_leave)

root.mainloop()

A slicker way to do it for multiple buttons would be to create a new Button class that modifies the behaviour of the default button so that the activebackground actually works when you hover.对多个按钮执行此操作的一种更灵活的方法是创建一个新的 Button 类,该类修改默认按钮的行为,以便在悬停时activebackground实际工作。

import tkinter as tk

class HoverButton(tk.Button):
    def __init__(self, master, **kw):
        tk.Button.__init__(self,master=master,**kw)
        self.defaultBackground = self["background"]
        self.bind("<Enter>", self.on_enter)
        self.bind("<Leave>", self.on_leave)

    def on_enter(self, e):
        self['background'] = self['activebackground']

    def on_leave(self, e):
        self['background'] = self.defaultBackground

root = tk.Tk()

classButton = HoverButton(root,text="Classy Button", activebackground='green')
classButton.grid()

root.mainloop()

Plain and simple干净利落

In your Button object properties you have the tags: activebackground & activeforeground that are activated whenever there is interaction with the instace created.在您的Button对象属性中,您有标签: activebackgroundactiveforeground ,只要与创建的实例进行交互就会激活它们。 That is: the button object you've created.即:您创建的按钮对象。

eg例如

from tkinter import *

root = Tk()

button = Button(root, text="Click me", bg="#000", fg="#fff", activebackground="#f00", activeforeground="#fff")
button.pack()

root.mainloop()

It works perfectly on mouseover for me on Linux python3.它在 Linux python3 上对我来说在鼠标悬停时完美运行。

<\/blockquote>

Unfortunately it doesn't work at all on Windows 10不幸的是,它在 Windows 10 上根本不起作用

"

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

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