• A ttk:combox with colors?

    From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Sat Nov 2 22:24:28 2024
    From Newsgroup: comp.lang.tcl

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From greg@gregor.ebbing@gmx.de to comp.lang.tcl on Sun Nov 3 08:54:41 2024
    From Newsgroup: comp.lang.tcl

    Am 02.11.24 um 22:24 schrieb 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>>


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Sun Nov 3 23:46:45 2024
    From Newsgroup: comp.lang.tcl

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From greg@gregor.ebbing@gmx.de to comp.lang.tcl on Mon Nov 4 00:58:04 2024
    From Newsgroup: comp.lang.tcl

    Am 03.11.24 um 23:46 schrieb 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>>
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Mon Nov 4 20:32:12 2024
    From Newsgroup: comp.lang.tcl

    Hello 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
    --- Synchronet 3.20a-Linux NewsLink 1.114