为了账号安全,请及时绑定邮箱和手机立即绑定

按钮的 Tkinters 突出显示对我不起作用

按钮的 Tkinters 突出显示对我不起作用

红颜莎娜 2022-06-22 16:29:26
根据这篇文章中接受的答案,按钮上的使用.configure(highlightbackground='red')应该在按钮周围应用颜色,但是在测试中我无法重现海报在他们的 gif 录制中展示的内容。这是我的测试用例:(注意即使复制粘贴海报代码我也无法获得它们显示的突出显示效果)import tkinter as tkroot = tk.Tk()btn = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=4, activebackground="#ffffff",                activeforeground="#000000", highlightbackground='red', highlightcolor='red')btn.pack()btn.focus_set()root.mainloop()结果应用程序:在此处输入图像描述通过一些广泛的搜索,我在highlightbackground关于同一问题的 Q/A 方式中没有找到太多信息,因此可能缺少某些内容。我还尝试设置焦点,因为本文档指出小部件需要焦点,但结果相同。也许它可能与版本或操作系统相关......操作系统 - Windows 10 专业版蟒蛇 - 3.6.2使用 Krrr 的帖子更新了示例。所以这确实有点工作,但是这里的问题是它正在调整按钮的大小并且没有提供正确的突出显示颜色。import tkinter as tkdef ResponsiveWidget(widget, *args, **kwargs):    bindings = {        '<FocusIn>': {'highlightbackground': 'red', 'highlightcolor':'red'},        '<FocusOut>': {'highlightbackground': '#d9d9d9', 'highlightcolor':'SystemButtonFace'},        '<Enter>': {'state': 'active'},        '<Leave>': {'state': 'normal'}    }    for k, v in bindings.items():        root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))def update_active(event):    global previous_button    if previous_button != event.widget:        previous_button.config(default='normal')        event.widget.config(default='active')        previous_button = event.widgetroot = tk.Tk()button_list = []previous_button = Nonefor i in range(5):    if i == 0:        button_list.append(tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=5,                                     activebackground="#ffffff", activeforeground="#000000", default='active'))        previous_button = button_list[-1]    else:        button_list.append(tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=5,                                     activebackground="#ffffff", activeforeground="#000000", default='normal'))    button_list[-1].pack(padx=5, pady=5)    button_list[-1].bind('<ButtonRelease-1>', update_active)root.mainloop()
查看完整描述

2 回答

?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

不幸的是,Windows 操作系统似乎没有正确触发state和default小部件配置。但是,这可以通过您自己的绑定来实现。


如果您只有少数需要此行为的小部件,则可以创建一个小部件包装器:


def ResponsiveWidget(widget, *args, **kwargs):

    bindings = {

        '<FocusIn>': {'default':'active'},    # for Keyboard focus

        '<FocusOut>': {'default': 'normal'},  

        '<Enter>': {'state': 'active'},       # for Mouse focus

        '<Leave>': {'state': 'normal'}

    }

    # Create the widget instance

    w = widget(*args, **kwargs)


    # Set the bindings for the widget instance

    for k, v in bindings.items():

        w.bind(k, lambda e, kwarg=v: e.widget.config(**kwarg))


    # Remember to return the created and binded widget

    return w


btn = ResponsiveWidget(tk.Button, root, text='test3', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",

                activeforeground="#000000", highlightbackground='red', highlightcolor='green')


btn2 = ResponsiveWidget(tk.Button, root, text='test4', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",

                activeforeground="#000000", highlightbackground='green', highlightcolor='red')

另一方面,如果您希望小部件的整个类始终正确触发默认/状态,则可以bind_class改用:


bindings = {

    '<FocusIn>': {'default':'active'},    # for Keyboard focus

    '<FocusOut>': {'default': 'normal'},  

    '<Enter>': {'state': 'active'},       # for Mouse focus

    '<Leave>': {'state': 'normal'}

}

for k, v in bindings.items():

    root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))

这似乎很好地触发了事件。


如果您只想复制突出显示颜色的功能,则不太理想的方法是更改highlightcolor焦点配置:


bindings = {

        '<FocusIn>': {'highlightcolor':'red'},

        '<FocusOut>': {'highlightcolor': 'SystemButtonFace'},

        '<Enter>': {'state': 'active'},

        '<Leave>': {'state': 'normal'}

    }

for k, v in bindings.items():

    root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))


# Note this method requires you to set the default='active' for your buttons


btn = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",

                activeforeground="#000000", highlightcolor='SystemButtonFace', default='active')


# ...

我认为这更像是一种 hacky 方法。


编辑:为了完整起见,这里有一个 MCVE 使用bind_class:


import tkinter as tk


root = tk.Tk()

bindings = {

        '<FocusIn>': {'highlightcolor':'red'},

        '<FocusOut>': {'highlightcolor': 'SystemButtonFace'},

        '<Enter>': {'state': 'active'},

        '<Leave>': {'state': 'normal'}

    } 


for k, v in bindings.items():

    root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))


btns = list(range(5))

for btn in btns:

    btns[btn] = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=5, activebackground="#ffffff",

        activeforeground="#000000", highlightcolor='SystemButtonFace', default='active', padx=5, pady=5)

    btns[btn].pack()


btns[0].focus_set()

root.mainloop()

和 MCVE 使用ResponsiveWidget功能:


import tkinter as tk


root = tk.Tk()

def ResponsiveWidget(widget, *args, **kwargs):

    bindings = {

        '<FocusIn>': {'highlightcolor':'red'},    # for Keyboard focus

        '<FocusOut>': {'highlightcolor': 'SystemButtonFace'},  

        '<Enter>': {'state': 'active'},       # for Mouse focus

        '<Leave>': {'state': 'normal'}

    }

    # Create the widget instance

    w = widget(*args, **kwargs)


    # Set the bindings for the widget instance

    for k, v in bindings.items():

        w.bind(k, lambda e, kwarg=v: e.widget.config(**kwarg))


    # Remember to return the created and binded widget

    return w


btns = list(range(5))

for btn in btns:

    btns[btn] = ResponsiveWidget(tk.Button, root, text=f'test{btn}', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",

        activeforeground="#000000", highlightcolor='SystemButtonFace', default='active', padx=5, pady=5)

    btns[btn].pack()


btns[0].focus_set()

root.mainloop()


查看完整回答
反对 回复 2022-06-22
?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

谢谢您的问题,您的代码很好,只需使用即可解决您的问题default="active"


import tkinter as tk



root = tk.Tk()


btn = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=4,  

activebackground="#ffffff",

                activeforeground="#000000", highlightbackground='red', 

default="active", highlightcolor='red')

btn.pack()

btn.focus_set()

root.mainloop()


查看完整回答
反对 回复 2022-06-22
  • 2 回答
  • 0 关注
  • 79 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信