You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改ttk Combobox中ListBox的边框颜色或移除边框

问题描述

我有如下代码:

#TComboboxStyle

mystyle.theme_settings("MyAppStyle", settings={
"TCombobox": {"configure": {"background": "#3d3d40",
                            "foreground": "#9c9c9f",
                            "arrowcolor": "#9c9c9f",
                            "fieldbackground": "#3d3d40",
                            "relief": "flat",
                            "arrowsize": 15},
              "layout": [('Entry.textarea', {'sticky': 'nswe', 'children':
                  [('Combobox.downarrow', {'side': 'right', 'sticky': 'ns'})]})]},
"TScrollbar": {"configure": {"background": "#525252",
                             "foreground": "#0000ee",
                             "troughcolor": "#9f9f9f",
                             "relief": "flat",
                             "arrowcolor": "#9f9f9f"},
               "layout": [('Vertical.Scrollbar.trough', {'sticky': 'ns', 'children': [
    ('Vertical.Scrollbar.thumb', {'sticky': 'nswe'})]})]}})

还有这段代码:

mod_info = ScrollTextBox(self)
mod_info.xscroll.configure(style='Arrowless.Horizontal.scrollbar')
mod_info.yscroll.configure(style='Arrowless.Vertical.scrollbar')
mod_info.txt.configure(width=50, font=('Roboto/Roboto_Bold.ttf', '18', 'bold'),
                       foreground="#969698", background="#3d3d40")
mod_info_choose_mod = ttk.Combobox(self, font=('Roboto/Roboto_Bold.ttf', '20', 'bold'))
mod_info_choose_mod.option_add('*TCombobox*Listbox.font', ('Roboto/Roboto_Bold.ttf', '20', 'bold'))
mod_info_choose_mod.option_add('*TCombobox*Listbox.background', "#3d3d40")
mod_info_choose_mod.option_add('*TCombobox*Listbox.foreground', "#9c9c9f")
mod_info_choose_mod.option_add('*TCombobox*Listbox.selectBackground', "#33393f")
mod_info_choose_mod.option_add('*TCombobox*Listbox.selectForeground', "white")

当前效果中,Combobox下拉的ListBox带有边框,请问是否可以修改该边框颜色或移除边框?

解决方案

移除边框

在现有option_add代码后添加两行,将边框宽度设为0并设置无浮雕样式:

mod_info_choose_mod.option_add('*TCombobox*Listbox.borderwidth', 0)
mod_info_choose_mod.option_add('*TCombobox*Listbox.relief', 'flat')

修改边框颜色

如果需要保留边框但修改颜色,可通过调整高亮边框属性实现:

# 设置高亮边框厚度(需大于0才会显示)
mod_info_choose_mod.option_add('*TCombobox*Listbox.highlightthickness', 1)
# 设置边框常态颜色
mod_info_choose_mod.option_add('*TCombobox*Listbox.highlightbackground', '#你的目标颜色')
# 设置边框选中时的颜色(可与常态颜色一致)
mod_info_choose_mod.option_add('*TCombobox*Listbox.highlightcolor', '#你的目标颜色')

自定义主题全局配置(适配ttk主题)

若当前ttk主题覆盖了局部属性,可在主题设置中添加TListbox的全局配置:

mystyle.theme_settings("MyAppStyle", settings={
    # 原有TCombobox、TScrollbar配置...
    "TListbox": {
        "configure": {
            "borderwidth": 0,
            "relief": "flat",
            # 若需修改颜色,添加:
            # "highlightbackground": "#你的颜色",
            # "highlightcolor": "#你的颜色",
            # "highlightthickness": 1
        }
    }
})

内容的提问来源于stack exchange,提问作者Kynomi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 03:14:55