繁体   English   中英

Tkinter 需要一个按钮来在指针打开和指针关闭时更改背景颜色

[英]Tkinter need a Button to change background color when pointer on and pointer off

我有一个带有许多按钮的 Python Tkinter Windows 程序。 我需要一个按钮来在指针打开和关闭时来回更改其背景颜色。 这个问题之前已经在这里讨论过,我尝试使用给出的代码片段来解决我的问题,但没有成功。 对我来说,最好的解决方案是使该方法达到只需要一次的水平。 在我的程序中,用户可以定义按钮的背景颜色,但与所有按钮类似,并且指针颜色应该能够受到选择的影响。

下面是我尝试使用绑定的最小代码。 randint 模拟用户选择。 但是,如前所述,它不起作用。 我需要进行哪些更改? 我是 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 ()

我是 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)

问题是由于两个函数的缩进不正确: on_enter()on_leave() 它们需要是__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)

如果您不想为每个按钮调用两个绑定,最好创建一个自定义按钮 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))

然后将这个自定义按钮 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.

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