为ttk.Combobox设置样式

问题描述 投票:1回答:1

要设置ttk.Combobox的样式,我可以执行以下操作:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

combostyle = ttk.Style()

combostyle.theme_create('combostyle', parent='alt',
                         settings = {'TCombobox':
                                     {'configure':
                                      {'selectbackground': 'blue',
                                       'fieldbackground': 'red',
                                       'background': 'green'
                                       }}}
                         )
combostyle.theme_use('combostyle') 

combo = ttk.Combobox(root, values=['1', '2', '3'])
combo['state'] = 'readonly'
combo.pack()

entry = tk.Entry(root)
entry.pack()

root.mainloop()

但是这为all tkinter和ttk小部件设置了主题。我想为only组合框设置样式。我该怎么办?

我正在Windows 10上使用Python 3。

非常感谢您的帮助。

python tkinter ttk
1个回答
0
投票

您的原始方法配置了全局主题。若要将主题附加到一个组件,您必须创建一个主题并将其附加到小部件类。

combostyle.configure('MyCustomStyleName.TCombobox', selectbackground = 'blue', ........) 
combo = ttk.Combobox(root, values=['1', '2', '3'], style = 'MyCustomStyleName.TCombobox')
© www.soinside.com 2019 - 2024. All rights reserved.