python - 如何在 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
方法,但只能更改列表框背景。
解决方案
您正在尝试使用正确的选项来更改背景颜色:
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
。
推荐阅读
- c - 如何使用文件描述符删除 C 中的文件?
- reactjs - Next Js:如何在不显示客户端请求的情况下调用外部 REST API(网络选项卡)
- bash - 带有变量的 wget bash 脚本进入链接
- microsoft-graph-api - MS Teams 呼叫队列 - 等待时间
- java - Android清单:如何指定应用程序需要ToF传感器(飞行时间)?
- arrays - 分配给数组时 rand() 的初始值设定项无效
- javascript - 如何更改复选框材料表ReactJS的颜色
- python - 在输入提示中启用箭头键导航
- android - 如何在颤动的形状appbar中应用渐变颜色?
- crash - iOS 14 App 使用 UIImagePicker 拍照时经常崩溃