我试图在窗口小部件没有焦点时更改Mac OS X上Tkinter文本窗口小部件中所选文本的默认背景颜色.默认的未聚焦选择颜色为灰色.经过几个小时的搜索,我无法找到一个开箱即用的解决方案来做到这一点.这是我尝试过的:
selectbackground
当窗口小部件未聚焦时,使用该选项更改选择颜色不会更改选择颜色.例如它保持灰色.Text.tag_configure("sel", background=...)
ttk.Style.map
与"!focus"
入境部件(及其他)国家的作品,而不是文本部件.所以我不得不自己动手(见下文).有一个更好的方法吗?
import Tkinter as tk
# Replace 'tag_out' with 'tag_in'
def replace_tag(widget, tag_out, tag_in):
ranges = widget.tag_ranges(tag_out)
widget.tag_remove(tag_out, ranges[0], ranges[1])
widget.tag_add(tag_in, ranges[0], ranges[1])
def focusin(e):
replace_tag(e.widget, "sel_focusout", "sel")
def focusout(e):
replace_tag(e.widget, "sel", "sel_focusout")
root = tk.Tk()
# Create a Text widget with a red selected text background
text = tk.Text(root, selectbackground="red")
text.pack()
# Add some text, and select it
text.insert("1.0", "Hello, world!")
text.tag_add("sel", "1.0", "end") …
Run Code Online (Sandbox Code Playgroud)