如何更改 ttk.Entry 插入光标颜色?

Mel*_*elS 2 color-scheme tkinter insertion python-3.x tkinter-entry

我正在使用 Python3 和 tkinter 8.6 开发一个应用程序。其中一部分是一个相当复杂的对话框,它使用深色背景。随附的代码仅使用几个小部件来显示我无法工作的区域。除此之外,我的对话框使具有键盘焦点的小部件变得明显(通过突出显示的背景颜色,只需选项卡)。我已经尝试了所有可能的搜索字符串来寻找解决方案,但无济于事。事实证明 'insertwidth': '4' 确实有效。我想要的就是更改 ttkEntry 小部件插入点光标的颜色。我发现的建议答案似乎都不是解决方案。提前致谢。

    #!/usr/bin/env python3
import tkinter as tk
import tkinter.ttk as ttk    # ttk widgets use styles
import tkinter.font as tkfont

def app_exit():
    root.destroy()


root = tk.Tk()
root.geometry('400x50+400+100')
bg = '#2C2B3B'    # dark blue
hlbg = '#3F3D5C'  # dark blue a shade lighter
fg = 'white'
ebg = 'pink'   # Entry inserts
root.configure(background=bg)  # fills in around labels and entries
font14 = tkfont.Font(size=14)
font10 = tkfont.Font(size=10)
root.option_add('*TEntry*Font', font14)
ttstyle = ttk.Style()
ttstyle.theme_use('default')
ttstyle.theme_settings('default', {
    '.': {   # this sets the general defaults for everybody
        'configure': {'font': font14, 'background': bg, 'foreground': fg}},
    'TButton': {
        'map': {'background': [('active', hlbg), ('focus', hlbg), ('!disabled', bg)]}},
    'TEntry': {  # 'insertforeground' and 'insertforeground' does not seem to do anything
        'configure': {'insertwidth': '4', 'insertforeground': ebg, 'insertforeground': ebg},
        'map': {'fieldbackground': [('active', hlbg), ('focus', hlbg), ('!disabled', bg)],
                'foreground': [('!disabled', fg)]}},
    'TLabel': {
        'configure': {'font': font10}}})

lbl1 = ttk.Label(root, text='Hello ')
tbx = ttk.Entry(root)
btnOK = ttk.Button(root, text='Exit', command=app_exit)

lbl1.grid(row=0, column=0, sticky='w')
tbx.grid(row=0, column=1, sticky='ew')
btnOK.grid(row=0, column=2, sticky='e')
tbx.insert(0,'AbCdEf')
tbx.focus()

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

输出示例

Bry*_*ley 5

ttk Entry 小部件的选项是insertcolor

...
'TEntry': {
    'configure': {..., 'insertcolor': ebg},
...
Run Code Online (Sandbox Code Playgroud)

这是 ttk 小部件所有选项的便捷参考。它位于 tcler 的 wiki 上,因此语法是 tcl 而不是 python,但选项名称本身在语言之间是相同的。

更改小部件颜色