如何设置ttk.Combobox的背景颜色 [英] How to set the background color of a ttk.Combobox
问题描述
我有一个问题,使用tkinter ttk使用'vista'主题(我使用Python 3)为Combobox设置背景颜色。我已尝试过此处的代码 ttk 。当状态为只读和离焦时,Combobox故障
导入tkinter为tk
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 $ b style.map('TCombobox',selectbackground = [('readonly','red')])
#style.map('TCombobox',fieldbackground = )#not working as well
但这将只改变文本的背景,组合框的其余部分。此外,我在tcl论坛上看到了一条信息: http://wiki.tcl.tk/15780 和我've tried with'fieldbackground'但似乎tkinter忽略此参数。你有什么想法如何解决呢?也许有一种方法来配置特定主题的特定样式?
此代码如下所示
code> style = ttk.Style()
style.map('TCombobox',fieldbackground = [('readonly','white')])
style.map 'TCombobox',selectbackground = [('readonly','white')])
style.map('TCombobox',selectforeground = [('readonly','black')])
self.mycombo = ttk.Combobox(self.frame,textvariable = self.combo_var,
height = 15,justify ='left',width = 21,
values = lista)
self.mycombo ['state'] ='readonly'#根据配置颜色设置状态
self.mycombo.bind('<< ComboboxSelected>>',
lambda event:self._click_combo())
`
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
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'.
This code below worked fine for me.It is important set the order of the parameters.
`
style = ttk.Style()
style.map('TCombobox', fieldbackground=[('readonly','white')])
style.map('TCombobox', selectbackground=[('readonly', 'white')])
style.map('TCombobox', selectforeground=[('readonly', 'black')])
self.mycombo = ttk.Combobox(self.frame,textvariable=self.combo_var,
height=15,justify='left',width=21,
values=lista)
self.mycombo['state'] = 'readonly' # Set the state according to configure colors
self.mycombo.bind('<<ComboboxSelected>>',
lambda event: self._click_combo())
`
这篇关于如何设置ttk.Combobox的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!