如何更改ttk.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',('!disabled',bg)]}},'TEntry': {  # 'insertforeground' and 'insertforeground' does not seem to do anything
        'configure': {'insertwidth': '4','insertforeground': ebg,'insertforeground': ebg},'map': {'fieldbackground': [('active',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()

Example output

解决方法

“ ttk输入”小部件的选项为insertcolor

...
'TEntry': {
    'configure': {...,'insertcolor': ebg},...

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

Changing Widget Colors