Discussion:
A ttk:combox with colors?
Add Reply
Helmut Giese
2024-11-02 21:24:28 UTC
Reply
Permalink
Hello out there,
I would like to have a combobox display stripes of colors instead of
text and the selection coloring the background of the combo's text
field. How could I go about creating such a beast (or maybe it exists
already)?
Any link or idea will be highly appreciated
Helmut
greg
2024-11-03 07:54:41 UTC
Reply
Permalink
Post by Helmut Giese
Hello out there,
I would like to have a combobox display stripes of colors instead of
text and the selection coloring the background of the combo's text
field. How could I go about creating such a beast (or maybe it exists
already)?
Any link or idea will be highly appreciated
Helmut
Hello Helmut,

I use the internal listbox of combobox. When I select an element for the
first time, the text in the combobox is visible for a short time. I have
no idea.

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
$lb itemconfigure $i -selectforeground $color
incr i
}
}

# Create a combobox with the custom style
set selectedValue ""
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 100 {styleListbox .cb}
}

# Event binding
# https://wiki.tcl-lang.org/page/ttk%3A%3Acombobox
# Disabled/Readonly color (and pointer to color change)
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>>
greg
2024-11-03 07:55:51 UTC
Reply
Permalink
Post by Helmut Giese
Hello out there,
I would like to have a combobox display stripes of colors instead of
text and the selection coloring the background of the combo's text
field. How could I go about creating such a beast (or maybe it exists
already)?
Any link or idea will be highly appreciated
Helmut
Hello Helmut,

I use the internal listbox of combobox. When I select an element for the
first time, the text in the combobox is visible for a short time. I have
no idea.

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
$lb itemconfigure $i -selectforeground $color
incr i
}
}

# Create a combobox with the custom style
set selectedValue ""
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 100 {styleListbox .cb}
}

# Event binding
# https://wiki.tcl-lang.org/page/ttk%3A%3Acombobox
# Disabled/Readonly color (and pointer to color change)
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>>
Helmut Giese
2024-11-03 22:46:45 UTC
Reply
Permalink
Hello 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
greg
2024-11-03 23:58:04 UTC
Reply
Permalink
Post by Helmut Giese
Hello 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>>
Helmut Giese
2024-11-04 19:32:12 UTC
Reply
Permalink
Hello Greg,
Post by greg
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)
Ah, I see. Well, that's too bad. You see, I have never used any
'theme' whatsoever - Windows' 'out of the box' look and feel was good
enough.
Well, I have to think of something. Thanks for the clarification and
best regards
Helmut

Loading...