在使用默认调色板颜色和自定义颜色时,我很难使用Tcl/Tk。
我有一个文本窗格,我希望在特定情况下设置背景颜色(例如,表示缺少用户输入),然后返回到默认背景颜色(一旦特定情况已经解决)。
到目前为止,我已经将-background选项用于小部件。例如。
$ wish
# ALERT: turn the background to pink
. configure -background pink
# SNAFU: turn the background back to white
. configure -background white但是,我刚刚发现了tk_setPalette,它允许我设置应用程序的全局调色板。不幸的是,这两种颜色似乎不是很好地结合在一起:一旦我明确地设置了一个小部件的背景色,似乎就没有办法取消它(并回到当前由调色板设置的默认颜色):
$ wish
# cheesy color-scheme
tk_setPalette brown
# ALERT: turn the background to pink
. configure -background pink
# SNAFU: turn the background back to "normal"
. configure -background white
# hmpf, no not "white"; the current palette is 'brown'
. configure -background brown一个解决方案(我想避免)是将调色板存储在一个变量中,并使用它来解释设置背景:
$ wish
# cheesy color-scheme
set mypalette brown
tk_setPalette $mypalette
# ALERT: turn the background to pink
. configure -background pink
# SNAFU: turn the background back to "normal"
. configure -background $mypalette然而,这有各种缺点:-我需要跟踪我的应用程序中发生的任何调色板更改(这可能很难做到,因为我的应用程序有一个“插件”系统,可以用于皮肤)--这只适用于background,但是设置颜色--调色板会改变颜色而不仅仅是背景颜色。
Esp。第二个缺点是一个真正的问题。目前,我的小部件之一是文本entry,它使用white作为默认背景,使用pink作为警报颜色,使用default文本颜色。每当我将配色方案更改为white以外的任何内容时,默认的文本颜色将变为white,从而使文本不可见(因为背景也是white,而不是default)。
所以问题是:
palette提供的默认值palette查询给定元素的默认颜色发布于 2016-06-01 00:43:51
我认为tk_setPalette不再受支持了。它用于与前ttk小部件的连接,并且不会与所有主题一起工作。
查询默认颜色:
要获取ttk小部件的背景色:
set bg [ttk::style lookup TFrame -background]其中TFrame是小部件的“类”。
要获取预ttk小部件的背景色:
set bg [. cget -background]此方法将适用于几乎所有的ttk小部件。
至于跟踪颜色的变化,我会保存任何调色板基色,背景,现场背景,前景和其他颜色,您可能需要时,主题插件加载。
另一种选择是在修改小部件以进行错误显示之前保存各个小部件的当前前景/背景等。
set oldbg [.myentry cget -background]
set oldfg [.myentry cget -foreground]
... change colors, display error condition...
... when error is resolved ...
.myentry configure -background $oldbg
.myentry configure -foreground $oldfg参考资料:ttk::风格 框架 setPalette
https://stackoverflow.com/questions/37554993
复制相似问题