python-3.x - 如何更改 ttk.Entry 插入光标颜色?

标签 python-3.x tkinter color-scheme tkinter-entry insertion

我正在使用 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()

Example output

最佳答案

ttk Entry 小部件的选项是 insertcolor

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

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

Changing Widget Colors

关于python-3.x - 如何更改 ttk.Entry 插入光标颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63363624/

相关文章:

python - 如何更改 xgboost.plot_importance 中绘图的大小?

python - tkinter Entry 小部件中的可见文本变量

Eclipse CDT禁用代码背景颜色

python - 使用 Pandas 生成 3D "matrix",基于比较两个数据帧 [Python]

python - 如何在 with 和 for 语句之前创建一个与 open() 具有相同行为的对象

python - 需要显示在 3 行而不是 1 行

visual-studio-2010 - VS2010 - SQL 脚本中数字的字体颜色

intellij-idea - 如何更改 IntelliJ 14.1.4 中事件选项卡标题的颜色?

python - 如何制作分割窗口?

python - PhotoImage 中的透明度错误