Python 面对更改Tkinter组合框突出显示背景色的问题
请看一下这个演示,让我知道为什么我不能将高光背景从蓝色改为白色 正如你所看到的,我已经设置了 “highlightbackground”:“黄色” 在 请看一下这个演示,让我知道为什么我不能将高光背景从蓝色改为白色 首先,当你发布你的代码时——重现你的问题 如果您询问combobox字段高亮显示的问题,那么您可以使用以下行:Python 面对更改Tkinter组合框突出显示背景色的问题,python,tkinter,Python,Tkinter,请看一下这个演示,让我知道为什么我不能将高光背景从蓝色改为白色 正如你所看到的,我已经设置了 “highlightbackground”:“黄色” 在 请看一下这个演示,让我知道为什么我不能将高光背景从蓝色改为白色 首先,当你发布你的代码时——重现你的问题 如果您询问combobox字段高亮显示的问题,那么您可以使用以下行: 'selectbackground': 'white', 'selectforeground': 'black' 现在还不清楚,虽然您提到了“highlightbackg
'selectbackground': 'white',
'selectforeground': 'black'
现在还不清楚,虽然您提到了“highlightbackground”:“Yellow”,但为什么您不能将高光背景从蓝色更改为白色?也许你在应用这种风格时做错了什么
您使用highlightbackground的原因是什么?据我所知,没有这样的选项(如果有请更正),因此如果您需要在组合框字段中将突出显示颜色更改为yellow
,则将selectbackground
行更改为'selectbackground':'yellow'
如果您想更改列表框中的突出显示颜色,这件事有点棘手。根据tcl文档,你不能直接做这样的事情
combobox小部件利用pre-ttk Listbox作为其下拉元素,因此当前需要使用'option'命令来设置Listbox选项
虽然“-selectbackground”和“-selectforeground”(适用于“selected”文本)可以与configure命令一起使用,但在使用相关列表框的选项数据库时,我们必须分别使用相应的数据库名称“selectbackground”和“selectforeground”,并注意数据库名称似乎区分大小写(有关完整列表,请参阅)
关联列表框的上述“selectForeground”和“selectBackground”选项用于在通过鼠标指针进行选择时实现标准的“悬停”效果
所以可以这样解决:
root.option_add('*TCombobox*Listbox.selectBackground', 'yellow') # change highlight color
root.option_add('*TCombobox*Listbox.selectForeground', 'black') # change text color
完整示例:
# imports
try:
import tkinter as tk
import tkinter.ttk as ttk
except ImportError:
import Tkinter as tk
import ttk
import random
import string
# Main - application
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# style initiation
self.style = self.get_style()
self.style.theme_use('combostyle')
self.option_add('*TCombobox*Listbox.selectBackground', 'yellow')
self.option_add('*TCombobox*Listbox.selectForeground', 'black')
# some other stuff
self.resizable(width=False, height=False)
# frame
self.main_frame = tk.Frame(self, bg='gray', relief='raised', borderwidth=10)
self.combo_box = ttk.Combobox(self.main_frame)
self.combo_box.pack()
self.main_frame.pack()
# insert something to combobox
self.insert_something_to_combobox()
def insert_something_to_combobox(self):
self.combo_box['values'] = [gen_key() for _ in range(10)]
@staticmethod
def get_style():
combo_style = ttk.Style()
combo_style.theme_create('combostyle', parent='alt',
settings={
'TCombobox': {
'configure': {
'fieldbackground': 'white',
'selectbackground': 'white',
'selectforeground': 'black',
}
}
}
)
return combo_style
def gen_key(size=6, chars=string.ascii_uppercase + string.digits):
# just to generate some random stuff
return ''.join(random.choice(chars) for _ in range(size))
app = App()
app.mainloop()
谢谢你的常识!@Behseini,我很高兴能帮助你,我很好奇,你解决了什么问题?如果你能抽出时间,请编辑一下,让你的问题更清楚。因为我甚至不知道你为什么接受我的答案!
# imports
try:
import tkinter as tk
import tkinter.ttk as ttk
except ImportError:
import Tkinter as tk
import ttk
import random
import string
# Main - application
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# style initiation
self.style = self.get_style()
self.style.theme_use('combostyle')
self.option_add('*TCombobox*Listbox.selectBackground', 'yellow')
self.option_add('*TCombobox*Listbox.selectForeground', 'black')
# some other stuff
self.resizable(width=False, height=False)
# frame
self.main_frame = tk.Frame(self, bg='gray', relief='raised', borderwidth=10)
self.combo_box = ttk.Combobox(self.main_frame)
self.combo_box.pack()
self.main_frame.pack()
# insert something to combobox
self.insert_something_to_combobox()
def insert_something_to_combobox(self):
self.combo_box['values'] = [gen_key() for _ in range(10)]
@staticmethod
def get_style():
combo_style = ttk.Style()
combo_style.theme_create('combostyle', parent='alt',
settings={
'TCombobox': {
'configure': {
'fieldbackground': 'white',
'selectbackground': 'white',
'selectforeground': 'black',
}
}
}
)
return combo_style
def gen_key(size=6, chars=string.ascii_uppercase + string.digits):
# just to generate some random stuff
return ''.join(random.choice(chars) for _ in range(size))
app = App()
app.mainloop()