Post by Helmut GieseHello greg,
thanks a lot for the code. Impressive: knowing about the internal
'popdown.f.l' and handling ttk::style - something I have (as yet?) not
understood.
Alas, for me it works only half: The color gets selected as wanted,
but only covers a small part of the entry field, the rest being either
blue when the combo has the focus and white when not. I made screen
shots and uploaded them to 'file.io' under the URL
https://file.io/SWF4GKTWvUva
in case you want to have a look.
Interesting: The width of the colored part seems to vary for all
colors - most notably for 'red' and 'yellow'.
Nevertheless thank you for the code
Helmut
PS: I am on Windows 10 and run Tcl8.6.14 or ...12
Hello Helmut
The solution has the following limitation: it does not work with a naive
theme.
(Can only be changed when using non-native and non-graphical theme) from
Manual
The ttk::combobox consists of a ttk::entry and a tk::listbox
It's in the manual
https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_combobox.htm
Gregor
#! /usr/bin/env tclsh
package require Tk
# Procedure to style the listbox items
# interna popdown.f.l
# https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_combobox.htm
proc styleListbox {cb} {
set popdown [ttk::combobox::PopdownWindow $cb]
set lb "$popdown.f.l"
set colors [$cb cget -values]
set i 0
foreach color $colors {
$lb itemconfigure $i -background $color
$lb itemconfigure $i -foreground $color
$lb itemconfigure $i -selectbackground $color
#text color in listbox
#$lb itemconfigure $i -selectforeground $color
incr i
}
}
# https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_combobox.htm
# Can only be changed when using non-native and non-graphical themes
ttk::style theme use clam
set selectedValue ""
# Create a combobox with the custom style
ttk::combobox .cb -style CustomCombobox.TCombobox \
-values [list "green" "red" "white" "yellow" "black"] \
-textvariable selectedValue -state readonly
pack .cb -padx 20 -pady 20
# Event binding to style the internal listbox when the combobox is opened
bind .cb <ButtonPress-1> {
after 5 [list styleListbox %W]
}
# Event binding
# https://wiki.tcl-lang.org/page/ttk%3A%3Acombobox
# SHOW Disabled/Readonly color (and pointer to color change)
# https://wiki.tcl-lang.org/page/Changing+Widget+Colors
bind .cb <<ComboboxSelected>> {
ttk::style map CustomCombobox.TCombobox -fieldbackground "readonly
$selectedValue"
ttk::style map CustomCombobox.TCombobox -foreground "readonly
$selectedValue"
ttk::style map CustomCombobox.TCombobox -background "readonly
$selectedValue"
ttk::style map CustomCombobox.TCombobox -selectforeground "readonly
$selectedValue"
ttk::style map CustomCombobox.TCombobox -selectbackground "readonly
$selectedValue"
}
#.cb set "white"
#event generate .cb <<ComboboxSelected>>