如何设置 ttk.Combobox 的背景颜色 [英] How to set the background color of a ttk.Combobox
问题描述
我在使用带有vista"主题的 tkinter ttk 为 Combobox 设置背景颜色时遇到问题(我使用的是 Python 3).我试过这里的代码 ttk.Combobox 状态为只读且失焦时出现故障
I have a problem to set background color for Combobox using tkinter ttk with 'vista' theme (I'm using Python 3). I've tried code from here ttk.Combobox glitch when state is read-only and out of focus
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
combo = ttk.Combobox(root, values=['1', '2', '3'])
combo['state'] = 'readonly'
combo.pack()
tk.Entry(root).pack()
style = ttk.Style()
style.map('TCombobox', selectbackground=[('readonly', 'red')])
#style.map('TCombobox', fieldbackground=[('readonly', 'blue')]) #not working as well
但这只会改变文本的背景,组合框的其余部分为白色.我还在 tcl 论坛上看到了一个帖子:http://wiki.tcl.tk/15780 和我'已经尝试过'fieldbackground',但似乎tkinter忽略了这个参数.你知道如何解决它吗?也许有一种方法可以在特定主题中仅配置特定样式?我看到对于默认"主题,如果状态为只读",背景会变为灰色.
But this will change only background for text, rest part of combobox rests white. Also I saw a post on the tcl forum: http://wiki.tcl.tk/15780 and I've tried with 'fieldbackground' but it seems that tkinter ignores this parameter. Do you have any idea how to solve it? Maybe there is a way to configure only specific style in specific theme? I saw that for 'default' theme, the background changes to gray color if state is 'readonly'.
推荐答案
显然,设置新样式属性的顺序对于确定新样式的某个属性是否会是很重要的应用与否.例如,如果我先设置 background
而不是 selectbackground
,那么选择的颜色不会改变,而只是带有箭头的迷你按钮颜色(列出选项).
Apparently, the order you set the properties of the new style is important to determine if a certain property of the new style will be applied or not. For example, if I set first the background
instead of selectbackground
, then the color of selection will not be changed, but just the mini button color with the arrow (to list down the options).
我还注意到,根据 parent
的值,我认为这是派生新 style 的 parent style,新样式的某些新设置和属性可能无法应用.例如,如果我在 parent
设置为 aqua
时尝试更改 fieldbackground
属性,则它不起作用,但是如果 parent
设置为 alt
,它的工作原理.(我希望更多的专家用户可以帮助并改进这个答案,这对 ttk
和 tkinter
的未来用户也有帮助).
I noted also that depending on the value of parent
, which I suppose is the parent style from which the new style is derived, some of the new settings and properties of the new style might not be applied. For example, if I try to change the fieldbackground
property when parent
is set to aqua
, it does not work, but if parent
is set to alt
, it works. (I hope more expert users can help and contribute to improve this answer, which could be helpful also for future users of ttk
and tkinter
).
这是我的解决方案,我在其中创建了一个全新的样式:
This is my solution, where I created a complete new style:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
combostyle = ttk.Style()
combostyle.theme_create('combostyle', parent='alt',
settings = {'TCombobox':
{'configure':
{'selectbackground': 'blue',
'fieldbackground': 'red',
'background': 'green'
}}}
)
# ATTENTION: this applies the new style 'combostyle' to all ttk.Combobox
combostyle.theme_use('combostyle')
# show the current styles
# print(combostyle.theme_names())
combo = ttk.Combobox(root, values=['1', '2', '3'])
combo['state'] = 'readonly'
combo.pack()
entry = tk.Entry(root)
entry.pack()
root.mainloop()
由于我不是 ttk
的专家,我无法将新的 主题 仅应用于 ttk.Combobox类型的某个实例code>,但我将 theme 应用于未来可能的
ttk.Combobox
的所有实例.如果有人可以改进这个答案,我真的很感激这个手势!
Since I am not an expert on ttk
, I was not able to apply a new theme just to a certain instance of type ttk.Combobox
, but I applied the theme to all instances of future possible ttk.Combobox
. If someone can improve this answer, I would really appreciate the gesture!
For more information on how to create and set new styles, see here or here.
这篇关于如何设置 ttk.Combobox 的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!