[英]Tkinter need a Button to change background color when pointer on and pointer off
I have a Python Tkinter Windows program with many buttons.我有一个带有许多按钮的 Python Tkinter Windows 程序。 I need a button to change its background color forth and back when the pointer is on it and off it.我需要一个按钮来在指针打开和关闭时来回更改其背景颜色。 This issue has been dicussed here before, and I tried to use the code snippets given to solve my problem but did not succeed.这个问题之前已经在这里讨论过,我尝试使用给出的代码片段来解决我的问题,但没有成功。 The best solution for me would be such that the method would be on such a level that it is needed only once.对我来说,最好的解决方案是使该方法达到只需要一次的水平。 In my program the user can define the background color for the buttons, however similar to all, and the pointer-on color should be able to be affected by the choice.在我的程序中,用户可以定义按钮的背景颜色,但与所有按钮类似,并且指针颜色应该能够受到选择的影响。
Below a minimal code where I have tried to use bind.下面是我尝试使用绑定的最小代码。 randint simulates user choice. randint 模拟用户选择。 But, as said, it does not work.但是,如前所述,它不起作用。 What changes do I require?我需要进行哪些更改? I am new with Python and Tkinter, so please give your answer as clear changes to the code below.我是 Python 和 Tkinter 的新手,所以请将您的答案作为对下面代码的明确更改。
import tkinter as tk
from random import randint
class PointerOnOff:
def __init__ (self, root):
root.geometry ("200x140+100+100")
color = randint (0, 2)
if color == 0:
off_color = "#aaffaa"
on_color = "#99ff99"
elif color == 1:
off_color = "#ffffaa"
on_color = "#ffff99"
else:
off_color = "#ffaaaa"
on_color = "#ff9999"
self.OK = tk.Button (root, text = "OK", bg = off_color, command = self.OKPush)
self.OK.place (x = 50, y = 20, width = 100, height = 30)
self.Cancel = tk.Button (root, text = "Cancel", bg = off_color, command = self.CancelPush)
self.Cancel.place (x = 50, y = 60, width = 100, height = 30)
self.PushedButton = tk.Label (root, text = "")
self.PushedButton.place (x = 20, y = 100, width = 160, height = 30)
def on_enter (anybutton):
anybutton.widget.config (bg = on_color)
def on_leave (anybutton):
anybutton.widget.config (bg = off_color)
self.OK.bind("<Enter>", on_enter)
self.OK.bind("<Leave>", on_leave)
self.Cancel.bind("<Enter>", on_enter)
self.Cancel.bind("<Leave>", on_leave)
def OKPush (self):
self.PushedButton.config (text = "You pushed OK button")
def CancelPush (self):
self.PushedButton.config (text = "You pushed Cancel button")
root = tk.Tk ()
master = PointerOnOff (root)
root.mainloop ()
I'm python beginner and I'm not sure of my answer but the code for changing the background color is this in my opinion:我是 python 初学者,我不确定我的答案,但我认为更改背景颜色的代码是这样的:
from tkinter import *
from random import randint
t = Tk()
t.geometry('200x200')
def change_bg():
color = ("#" + str(randint(100000, 999999)))
f = Frame(t, bg=color)
f.place(x=0, y=0, width=200, height=200)
The issue is due to incorrect indentation of the two functions: on_enter()
and on_leave()
.问题是由于两个函数的缩进不正确: on_enter()
和on_leave()
。 They need to be inner functions inside __init__()
:它们需要是__init__()
内部的内部函数:
class PointerOnOff:
def __init__ (self, root):
...
self.PushedButton = tk.Label (root, text = "")
self.PushedButton.place (x = 20, y = 100, width = 160, height = 30)
def on_enter (anybutton):
anybutton.widget.config (bg = on_color)
def on_leave (anybutton):
anybutton.widget.config (bg = off_color)
self.OK.bind("<Enter>", on_enter)
self.OK.bind("<Leave>", on_leave)
self.Cancel.bind("<Enter>", on_enter)
self.Cancel.bind("<Leave>", on_leave)
If you don't want to call the two bindings for every button, you better create a custom button class to embed the hover feature:如果您不想为每个按钮调用两个绑定,最好创建一个自定义按钮 class 以嵌入 hover 功能:
class HoverButton(tk.Button):
_colors = [
# off # on
('#aaffaa', '#99ff99'),
('#ffffaa', '#ffff99'),
('#ffaaaa', '#ff9999'),
]
def __init__(self, master=None, *args, **kw):
# if "colors" option not provided, use random choice from internal colors
self._off_color, self._on_color = kw.pop("colors", self._colors[randint(0, 2)])
super().__init__(master, *args, **kw)
self["bg"] = self._off_color
self.bind("<Enter>", lambda e: self.config(bg=self._on_color))
self.bind("<Leave>", lambda e: self.config(bg=self._off_color))
Then use this custom button class for those buttons you want to have hover effect:然后将这个自定义按钮 class 用于您想要具有 hover 效果的按钮:
def class PointOnOff:
def __init___(self, root):
...
self.OK = HoverButton(root, text="OK", colors=("orange", "gold"), command=self.OKPush)
self.OK.place(x=50, y=20, width=100, height=30)
self.Cancel = HoverButton(root, text="Cancel", command=self.CancelPush)
self.Cancel.place(x=50, y=60, width=100, height=30)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.