ttk Combobox 以编程方式和动态设置背景颜色 [英] ttk Combobox set background colour programatically and dynamically

查看:32
本文介绍了ttk Combobox 以编程方式和动态设置背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一排包含 ttk.Combobox 的小部件,当我在行尾勾选 Checkbutton 时,我想更改行中小部件的背景颜色.使用 tkinter 只需使用 configure 就很简单,但使用 ttk 您必须使用一个主题,该主题似乎既不是动态的,也不是特定于单个小部件的.有没有办法实现这个功能?

I have a row of widgets which contains a ttk.Combobox and I want to change the background colour of the widgets in the row when I tick a Checkbutton at the end of the row. With tkinter it is simple just to use configure but with ttk you have to use a theme which seems to be neither dynamic nor specific to a single widget. Is there a way to achieve this functionality ?

谢谢.

回应 fhdrsdg 的评论.我无法让它工作,但这段代码演示了它

in response to fhdrsdg's comment. I can't get it working but this code demonstrates it

import Tkinter as tk
import ttk


def skillUsed():
    if chkUsedVar.get() == 1:
        style.map('TCombobox', background=[('readonly','green')])
        style.map('TCombobox', foreground=[('readonly','red')])
    else:
        style.map('TCombobox', background=[('readonly','white')])
        style.map('TCombobox', foreground=[('readonly','black')])

root = tk.Tk()

style = ttk.Style()

cboxVar1 = tk.StringVar()
cboxVar1.set("spam")

cboxVar2 = tk.StringVar()
cboxVar2.set("silly")

chkUsedVar = tk.IntVar()
chk = tk.Checkbutton(root, text='Used', variable=chkUsedVar, command=skillUsed)
chk.grid(row=0, column=2)

combo01 = ttk.Combobox(root, values=['spam', 'eric', 'moose'], textvariable=cboxVar1)
combo01['state'] = 'readonly'
combo01.grid(row=0, column=0)

combo02 = ttk.Combobox(root, values=['parrot', 'silly', 'walk'], textvariable=cboxVar2)
combo02['state'] = 'readonly'
combo02.grid(row=0, column=1)

root.mainloop()

单击复选框时,前景变为红色,取消选中时,前景变为黑色.问题是背景永远不会改变(但不会出错),并且样式全局应用于两个组合框,我想将它应用于单个框.

When the tick box is clicked the foreground goes red and when unticked it goes black. The issue is the background never changes (but doesn't error) and the style is applied globally to both comboboxes and I want to apply it to a single box.

我有一个解决方法,我将只使用 tkinter 的 OptionMenu,我在 tinterweb 上可以找到的所有内容都表明它不能用 ttk 小部件完成,但这似乎对 ttk 小部件有点限制,但我几乎没有使用 tkinter 或 ttk 的经验.

I have a workaround which I will use just using tkinter's OptionMenu and everything I can find on the tinterweb implies it can't be done with ttk widgets but that seems a bit of a limit to ttk widgets but I have little to no experience with tkinter or ttk.

解决方法是:-

from Tkinter import *

def skillUsed():
    if chkUsedVar.get() == 1:
        opt01.configure(bg="#000fff000")
        opt01.configure(highlightbackground="#000fff000")
        opt01.configure(activebackground="#000fff000")
        opt01.configure(highlightcolor="#000fff000")
        opt01["menu"].configure(bg="#000fff000")
    else:
        opt01.configure(bg=orgOptbg)
        opt01.configure(highlightbackground=orgOpthighlightbackground)
        opt01.configure(activebackground=orgOptactivebackground)
        opt01.configure(highlightcolor=orgOpthighlightcolor)
        opt01["menu"].configure(bg=orgOptmenu)

root = Tk()
optionList = ('parrot','silly','walk')
varopt01 = StringVar()
varopt01.set(optionList[0])
chkUsedVar = IntVar()

opt01 = OptionMenu(root, varopt01, *optionList)
opt01.grid(row=0, column=0)

orgOptbg = opt01.cget("bg")
orgOpthighlightbackground = opt01.cget("highlightbackground") 
orgOptactivebackground = opt01.cget("activebackground")
orgOpthighlightcolor = opt01.cget("highlightcolor")
orgOptmenu = opt01["menu"].cget("bg")

chk = Checkbutton(root, text='Used', variable=chkUsedVar, command=skillUsed)
chk.grid(row=0, column=1)
root.mainloop()

谢谢

推荐答案

首先,如果要将样式应用于单个组合框,请将其命名为custom.TCombobox",以便它继承自TCombobox",但不会更改默认组合框样式.然后您所要做的就是将您的组合框的样式设置为custom.TCombobox".

First, if you want to apply the style to a single combobox, give it a name like 'custom.TCombobox' so that it inherits from 'TCombobox' but doesn't change the default combobox style. Then all you have to do is set the style of your combobox to 'custom.TCombobox'.

其次,背景没有改变,因为它是你要改变的字段背景.

Secondly, the background was not changing because it's the fieldbackground you want to change.

编辑:样式中可以自定义的内容取决于所使用的 ttk 主题.例如,默认的 Mac 和 Windows 主题不允许进行太多自定义,并且组合框的字段背景颜色无法更改.但是,alt"和clam"主题允许更多自定义.

EDIT: What can be customized in a style depends on the ttk theme being used. For instance, the default Mac and Windows themes don't allow much customization and the fieldbackground color of the combobox cannot be changed. However, the 'alt' and 'clam' themes allow more customization.

这是一个基于您的代码的示例:

Here is an example based on your code:

import tkinter as tk
from tkinter import ttk


def skillUsed():
    if chkUsedVar.get() == 1:
        style.map('custom.TCombobox', fieldbackground=[('readonly','green')])
        style.map('custom.TCombobox', foreground=[('readonly','red')])
    else:
        style.map('custom.TCombobox', fieldbackground=[('readonly','white')])
        style.map('custom.TCombobox', foreground=[('readonly','black')])

root = tk.Tk()

style = ttk.Style()
style.theme_use('alt')

cboxVar1 = tk.StringVar()
cboxVar1.set("spam")

cboxVar2 = tk.StringVar()
cboxVar2.set("silly")

chkUsedVar = tk.IntVar()
chk = tk.Checkbutton(root, text='Used', variable=chkUsedVar, command=skillUsed)
chk.grid(row=0, column=2)

combo01 = ttk.Combobox(root, values=['spam', 'eric', 'moose'], textvariable=cboxVar1)
combo01['state'] = 'readonly'
combo01.grid(row=0, column=0)

combo02 = ttk.Combobox(root, values=['parrot', 'silly', 'walk'], textvariable=cboxVar2, style='custom.TCombobox')
combo02['state'] = 'readonly'
combo02.grid(row=0, column=1)

root.mainloop()

这篇关于ttk Combobox 以编程方式和动态设置背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆