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

根据这篇文章中接受的答案,按钮上的使用.configure(highlightbackground='red')应该在按钮周围应用颜色,但是在测试中我无法重现海报在他们的 gif 录制中展示的内容。


这是我的测试用例:(注意即使复制粘贴海报代码我也无法获得它们显示的突出显示效果)


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', highlightcolor='red')

btn.pack()

btn.focus_set()

root.mainloop()

结果应用程序:


在此处输入图像描述


通过一些广泛的搜索,我在highlightbackground关于同一问题的 Q/A 方式中没有找到太多信息,因此可能缺少某些内容。我还尝试设置焦点,因为本文档指出小部件需要焦点,但结果相同。


也许它可能与版本或操作系统相关......


操作系统 - Windows 10 专业版


蟒蛇 - 3.6.2


使用 Krrr 的帖子更新了示例。所以这确实有点工作,但是这里的问题是它正在调整按钮的大小并且没有提供正确的突出显示颜色。


import tkinter as tk



def 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.widget



root = tk.Tk()

button_list = []

previous_button = None


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


红颜莎娜
浏览 80回答 2
2回答

米琪卡哇伊

不幸的是,Windows 操作系统似乎没有正确触发state和default小部件配置。但是,这可以通过您自己的绑定来实现。如果您只有少数需要此行为的小部件,则可以创建一个小部件包装器:def ResponsiveWidget(widget, *args, **kwargs):&nbsp; &nbsp; bindings = {&nbsp; &nbsp; &nbsp; &nbsp; '<FocusIn>': {'default':'active'},&nbsp; &nbsp; # for Keyboard focus&nbsp; &nbsp; &nbsp; &nbsp; '<FocusOut>': {'default': 'normal'},&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; '<Enter>': {'state': 'active'},&nbsp; &nbsp; &nbsp; &nbsp;# for Mouse focus&nbsp; &nbsp; &nbsp; &nbsp; '<Leave>': {'state': 'normal'}&nbsp; &nbsp; }&nbsp; &nbsp; # Create the widget instance&nbsp; &nbsp; w = widget(*args, **kwargs)&nbsp; &nbsp; # Set the bindings for the widget instance&nbsp; &nbsp; for k, v in bindings.items():&nbsp; &nbsp; &nbsp; &nbsp; w.bind(k, lambda e, kwarg=v: e.widget.config(**kwarg))&nbsp; &nbsp; # Remember to return the created and binded widget&nbsp; &nbsp; return wbtn = ResponsiveWidget(tk.Button, root, text='test3', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activeforeground="#000000", highlightbackground='red', highlightcolor='green')btn2 = ResponsiveWidget(tk.Button, root, text='test4', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activeforeground="#000000", highlightbackground='green', highlightcolor='red')另一方面,如果您希望小部件的整个类始终正确触发默认/状态,则可以bind_class改用:bindings = {&nbsp; &nbsp; '<FocusIn>': {'default':'active'},&nbsp; &nbsp; # for Keyboard focus&nbsp; &nbsp; '<FocusOut>': {'default': 'normal'},&nbsp;&nbsp;&nbsp; &nbsp; '<Enter>': {'state': 'active'},&nbsp; &nbsp; &nbsp; &nbsp;# for Mouse focus&nbsp; &nbsp; '<Leave>': {'state': 'normal'}}for k, v in bindings.items():&nbsp; &nbsp; root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))这似乎很好地触发了事件。如果您只想复制突出显示颜色的功能,则不太理想的方法是更改highlightcolor焦点配置:bindings = {&nbsp; &nbsp; &nbsp; &nbsp; '<FocusIn>': {'highlightcolor':'red'},&nbsp; &nbsp; &nbsp; &nbsp; '<FocusOut>': {'highlightcolor': 'SystemButtonFace'},&nbsp; &nbsp; &nbsp; &nbsp; '<Enter>': {'state': 'active'},&nbsp; &nbsp; &nbsp; &nbsp; '<Leave>': {'state': 'normal'}&nbsp; &nbsp; }for k, v in bindings.items():&nbsp; &nbsp; 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 buttonsbtn = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activeforeground="#000000", highlightcolor='SystemButtonFace', default='active')# ...我认为这更像是一种 hacky 方法。编辑:为了完整起见,这里有一个 MCVE 使用bind_class:import tkinter as tkroot = tk.Tk()bindings = {&nbsp; &nbsp; &nbsp; &nbsp; '<FocusIn>': {'highlightcolor':'red'},&nbsp; &nbsp; &nbsp; &nbsp; '<FocusOut>': {'highlightcolor': 'SystemButtonFace'},&nbsp; &nbsp; &nbsp; &nbsp; '<Enter>': {'state': 'active'},&nbsp; &nbsp; &nbsp; &nbsp; '<Leave>': {'state': 'normal'}&nbsp; &nbsp; }&nbsp;for k, v in bindings.items():&nbsp; &nbsp; root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))btns = list(range(5))for btn in btns:&nbsp; &nbsp; btns[btn] = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=5, activebackground="#ffffff",&nbsp; &nbsp; &nbsp; &nbsp; activeforeground="#000000", highlightcolor='SystemButtonFace', default='active', padx=5, pady=5)&nbsp; &nbsp; btns[btn].pack()btns[0].focus_set()root.mainloop()和 MCVE 使用ResponsiveWidget功能:import tkinter as tkroot = tk.Tk()def ResponsiveWidget(widget, *args, **kwargs):&nbsp; &nbsp; bindings = {&nbsp; &nbsp; &nbsp; &nbsp; '<FocusIn>': {'highlightcolor':'red'},&nbsp; &nbsp; # for Keyboard focus&nbsp; &nbsp; &nbsp; &nbsp; '<FocusOut>': {'highlightcolor': 'SystemButtonFace'},&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; '<Enter>': {'state': 'active'},&nbsp; &nbsp; &nbsp; &nbsp;# for Mouse focus&nbsp; &nbsp; &nbsp; &nbsp; '<Leave>': {'state': 'normal'}&nbsp; &nbsp; }&nbsp; &nbsp; # Create the widget instance&nbsp; &nbsp; w = widget(*args, **kwargs)&nbsp; &nbsp; # Set the bindings for the widget instance&nbsp; &nbsp; for k, v in bindings.items():&nbsp; &nbsp; &nbsp; &nbsp; w.bind(k, lambda e, kwarg=v: e.widget.config(**kwarg))&nbsp; &nbsp; # Remember to return the created and binded widget&nbsp; &nbsp; return wbtns = list(range(5))for btn in btns:&nbsp; &nbsp; btns[btn] = ResponsiveWidget(tk.Button, root, text=f'test{btn}', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",&nbsp; &nbsp; &nbsp; &nbsp; activeforeground="#000000", highlightcolor='SystemButtonFace', default='active', padx=5, pady=5)&nbsp; &nbsp; btns[btn].pack()btns[0].focus_set()root.mainloop()

明月笑刀无情

谢谢您的问题,您的代码很好,只需使用即可解决您的问题default="active"import tkinter as tkroot = tk.Tk()btn = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=4,&nbsp;&nbsp;activebackground="#ffffff",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activeforeground="#000000", highlightbackground='red',&nbsp;default="active", highlightcolor='red')btn.pack()btn.focus_set()root.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python