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()
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()
添加回答
举报