【发布时间】:2019-01-12 14:12:35
【问题描述】:
我正在使用 TKinter(在 Python 3 中)开发 GUI。完成后,我想为所有小部件设置颜色。更改 Text 和 Button 小部件的颜色没有任何问题,我只是使用了 bg 选项:
t = Text(master, wrap = WORD, bg = '#ffffb3')
然后,我尝试设置 ttk.Combobox 小部件的颜色,我注意到它们没有此选项,所以我最终使用主题更改了它们的颜色,如本文所述:How to set the background color of a ttk.Combobox。但是我在将主题应用于所有窗口时遇到了问题。如果您尝试以下代码,您将看到,主题仅适用于第一个窗口中的 Combobox,但不适用于按下按钮时创建的窗口中的 Combobox:
from tkinter import *
from tkinter import ttk
r = Tk()
def callback():
r2 = Tk()
c2 = ttk.Combobox(r2)
c2.pack()
b = Button(r, text = 'Open', command = callback)
b.pack()
combostyle = ttk.Style()
combostyle.theme_create('combostyle', parent = 'alt', settings = {'TCombobox':{'configure':
{'fieldbackground': '#ffff99', 'background': '#ffcc66'}}})
combostyle.theme_use('combostyle')
c = ttk.Combobox(r)
c.pack()
r.mainloop()
这是我第一次在 TKinter 中使用主题,如果我犯了一个愚蠢的错误,请原谅我。我已经通过互联网搜索并没有找到任何解决方案。任何帮助将不胜感激!
【问题讨论】:
标签: python python-3.x tkinter combobox themes