首页 > 解决方案 > 如何在 ttk.Combobox 中更改下拉菜单(即 tk.Listbox)的活动背景颜色

问题描述

以下代码创建一个 ttk.Combobox 小部件:

import tkinter.ttk as ttk
default_values = ['Peter','Scotty','Walter','Scott','Mary','Sarah','Jane',
                  'Oscar','Walley','Faith','Bill','Egor','Heley']
s=ttk.Style()
s.configure( 'TCombobox', font=('Purisa', 20, 'bold'), background ='cyan',
             fieldbackground='pink')
c=ttk.Combobox(values=default_values)
c.master.option_add( '*TCombobox*Listbox.background', 'yellow')
c.master.option_add( '*TCombobox*Listbox.selectbackground','red') #does not work
c.master.option_add( '*TCombobox*Listbox.selectforeground','grey') #does not work  
c.master.option_add( '*TCombobox*Listbox.highlightbackground','blue') # does not work
c.master.option_add( '*TCombobox*Listbox.highlightforeground','green') #does not work
c.master.option_add( '*TCombobox*Listbox.activestyle', 'underline') #does not work
c.grid()

单击组合框向下箭头后,将出现一个tk.Listbox包含 的下拉菜单(这是一个小部件)default values。当鼠标悬停在下拉菜单上时,鼠标指针下方会出现一个活动背景。我想改变这个灰色活动背景的颜色。我该怎么做?

由于下拉菜单不是 ttk 小部件,它不会响应和ttk.Style()设置。我也尝试过该.option_add方法,但只能更改列表框背景。

问题: 下拉菜单中的活动背景

标签: pythontkinter

解决方案


您正在尝试使用正确的选项来更改背景颜色:

c.master.option_add( '*TCombobox*Listbox.selectbackground','red')

但是你犯了一个小错误,即selectbackground。在selectbackground中,的第一个字母background应该是大写的。
*TCombobox*Listbox.selectbackground变成*TCombobox*Listbox.selectBackground
-----------------------__^__-----------------------------------------__^__-------

尝试:

c.master.option_add( '*TCombobox*Listbox.selectBackground','red')

同样,foreground变成Foreground


推荐阅读