gpt4 book ai didi

python - 如何更改 ttk.Combobox 中下拉菜单(即 tk.Listbox)事件背景的颜色

转载 作者:行者123 更新时间:2023-11-28 18:08:04 28 4
gpt4 key购买 nike

以下代码创建一个 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 小部件)。当鼠标悬停在下拉菜单上时,鼠标指针下方会出现事件背景。 我想改变这个灰色事件背景的颜色。我该怎么做?

作为dropdown menu isn't a ttk widget , 它不会响应 ttk.Style() 设置。我也尝试过 .option_add 方法,但只能更改列表框背景。

问题: active background in dropdown menu

最佳答案

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

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

关于python - 如何更改 ttk.Combobox 中下拉菜单(即 tk.Listbox)事件背景的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52453471/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com