Skip to main content
 首页 » 编程设计

python之ttk.ComboBox 样式设置不正确

2025年05月04日6youxin

我已经创建了一个 python GUI 应用程序。它工作得很好,我已经根据自己的喜好设计了所有样式,除了 ComboBox。 ttk.Combobox 上的样式似乎不起作用。

这应该让我了解我想要的 Material 风格。这是组合框的样式 block 。

globalStyle = ttk.Style() 
globalStyle.configure('TCombobox', foreground=textColor, background=backgroundColor, fieldbackground=selectColor, fieldforeground=textColor, font='Verdana') 

我唯一能够成功更改的是文本和前景色。我希望编辑以下属性:

文字颜色 现场背景 下拉文字颜色 下拉背景

编辑:我应该提一下,使用的颜色变量都是有效的十六进制颜色代码。

selectColor = '#333333' 
backgroundColor = '#444444' 
foregroundColor = '#555555' 
textColor = '#999999' 

请您参考如下方法:

所以我遇到了同样的问题,但找到了大部分解决方案 here .您所要做的就是将以下内容添加到您的代码中:

option add *TCombobox*Listbox.background color  
option add *TCombobox*Listbox.font font  
option add *TCombobox*Listbox.foreground color  
option add *TCombobox*Listbox.selectBackground color  
option add *TCombobox*Listbox.selectForeground color 

然后要更改框内的字体(当下拉列表不存在时)将 font='font_style' 添加到您的代码中。

所以在我的例子中我有:

class CreateProfile(tk.Frame): 
    def __init__(self, parent, controller): 
        tk.Frame.__init__(self, parent, bg='dodgerblue4') 
        label = tk.Label(self, text="Create Profile", font=large_font, bg='dodgerblue4', fg='deepskyblue') 
        label.grid(columnspan=10, row=0, column=0, pady=5, padx=5) 
        self.grid_rowconfigure(1, weight=1) 
        self.grid_columnconfigure(1, weight=1) 
 
        self.option_add("*TCombobox*Listbox*Background", "dodgerblue") 
        self.option_add("*TCombobox*Listbox*Font", "pirulen") 
 
        self.list_c = ttk.Combobox(self, values=("1", "2", "3", "4"), font='pirulen') 
        self.list_c.grid(row=1, column=1, pady=5, padx=5) 

确保您还具有以下导入:

import tkinter as tk 
import tkinter.ttk as ttk 

我的问题是我只能更改实际框的背景颜色(当下拉菜单不存在时)。我仍在尝试弄清楚如何更改字体颜色(前景对我不起作用)以及框本身的颜色。因此,如果有人可以添加到这个答案中,那就太好了!