【问题标题】:ttk.ComboBox Styling does not get set properlyttk.ComboBox 样式设置不正确
【发布时间】:2014-09-24 23:05:33
【问题描述】:

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

这应该可以让我了解我想要的材料风格。这是组合框的样式块。

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

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

文字颜色 领域背景 下拉文本颜色 下拉背景

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

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

【问题讨论】:

  • 我还没有找到这个问题的满意答案。我到处搜索,找不到您正在寻找的确切答案。如果您自己找到了答案,如果您能发布它,那就太好了。

标签: python tkinter ttk


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    # 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 的样式机制。 至少上面的代码适用于 Python 3.2 和 Python 3.4

    【讨论】:

    • 此解决方案更改所有小部件的颜色,而不仅仅是组合框。
    猜你喜欢
    • 2020-07-16
    • 1970-01-01
    • 2014-02-27
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多