ttk.组合框样式设置不正确

2024-09-30 06:23:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个pythongui应用程序。它工作得很好,除了组合框之外,我已经按我的喜好设计了所有的样式。在ttk.组合框好像不管用。在

这应该能让我知道我想要的材料风格。这是组合框的样式块。在

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

我唯一能成功改变的就是文本和前景颜色。我要编辑以下属性:

文本颜色 现场背景 下拉文本颜色 下拉式背景

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

^{pr2}$

Tags: 文本应用程序编辑style颜色风格configure样式
2条回答

我知道这个问题已经有半年了,但我也遇到了类似的问题,并设法解决了。要更改ttk组合框弹出框的颜色,可以使用以下代码:

# these imports are indeed only valid for python 3.x
import tkinter as tk
import tkinter.ttk as ttk

# for python 2.x the following import statements should work:
# import Tkinter as tk
# import ttk

root = tk.Tk()

# adding the options to the root elements
# (all comboboxes will receive this options)
root.option_add("*background", "#444444"),
root.option_add("*foreground", "#999999"),

# create a combobox
ttk.Combobox(root, values=["Banana", "Coconut", "Strawberry"]).pack()

root.mainloop()

我不确定我是否正确理解了tk的风格机制。 至少上面的代码适用于python3.2和python3.4

所以我遇到了同样的问题,但找到了大部分的解决方案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'。在

所以在我的案例中我有:

^{pr2}$

确保还具有以下导入:

import tkinter as tk
import tkinter.ttk as ttk

我的问题是我只能更改实际框的背景色(当下拉框不存在时)。我仍在尝试如何更改字体颜色(前景对我不起作用),以及方框本身的颜色。所以如果有人能补充这个答案那就太好了!在

相关问题 更多 >

    热门问题