如何在 tkinter 中设置 ttk.Combobox 的背景颜色?
Tkinter 支持 ttk 小组件,该小组件用于更改 tkinter 应用程序中任何小组件的样式和属性。我们可以通过访问 ttk 中的配置函数,并将 'TCombobox' 作为第一个参数来设置 ComboBox 小组件的背景颜色、前景色和其他属性。
示例
在此示例中,我们将通过在 ttk 小组件中定义其值来设置 ComboBox 控件的背景颜色。
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Define the style for combobox widget
style= ttk.Style()
style.theme_use('clam')
style.configure("TCombobox", fieldbackground= "orange", background= "white")
# Add a label widget
label=ttk.Label(win, text= "Select a Car Model",
font= ('Aerial 11'))
label.pack(pady=30)
# Add a Combobox widget
cb= ttk.Combobox(win, width= 25, values=["Honda", "Hyundai", "Wolkswagon", "Tata", "Renault", "Ford", "Chrevolet", "Suzuki","BMW", "Mercedes"])
cb.pack()
win.mainloop()输出
运行以上代码将打开一个窗口,其中将有一个组合框小组件,可从列表中选择一个选项。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP