Skip to content

ColorChooserDialog

Bases: Dialog

A class which displays a color chooser dialog. When a color option is selected and the "OK" button is pressed, the dialog will return a namedtuple that contains the color values for rgb, hsl, and hex. These values can be accessed by indexing the tuple or by using the named fields.

Examples:

```python
>>> cd = ColorChooserDialog()
>>> cd.show()
>>> colors = cd.result
>>> colors.hex
'#5fb04f'
>>> colors[2]
'#5fb04f
>>> colors.rgb
(95, 176, 79)
>>> colors[0]
(95, 176, 79)
```
Source code in src/ttkbootstrap/dialogs/colorchooser.py
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
class ColorChooserDialog(Dialog):
    """A class which displays a color chooser dialog. When a color
    option is selected and the "OK" button is pressed, the dialog will
    return a namedtuple that contains the color values for rgb, hsl, and
    hex. These values can be accessed by indexing the tuple or by using
    the named fields.

    ![](../../assets/dialogs/querybox-get-color.png)

    Examples:

        ```python
        >>> cd = ColorChooserDialog()
        >>> cd.show()
        >>> colors = cd.result
        >>> colors.hex
        '#5fb04f'
        >>> colors[2]
        '#5fb04f
        >>> colors.rgb
        (95, 176, 79)
        >>> colors[0]
        (95, 176, 79)
        ```
    """

    def __init__(
            self, parent: Optional[tkinter.Misc] = None, title: str = "Color Chooser",
            initialcolor: Optional[str] = None) -> None:
        """Create a color chooser dialog.

        The dialog displays a ColorChooser widget in a modal dialog with OK
        and Cancel buttons. When OK is pressed, the result property returns a
        ColorChoice namedtuple containing the selected color in multiple formats
        (rgb, hsl, hex). The dialog also includes a color dropper tool for
        selecting colors from the screen (Windows/Linux only).

        Parameters:

            parent (Widget):
                Parent widget. The dialog will be modal and centered on this
                widget.

            title (str):
                The dialog window title (default='Color Chooser', will be
                localized).

            initialcolor (str):
                Initial color to display in the chooser. Can be any valid color
                string format (e.g., '#FF0000', 'red', 'rgb(255,0,0)'). If None,
                uses the theme's background color.

        Returns:
            The result property contains a ColorChoice namedtuple with fields:
            - rgb: tuple (r, g, b) with values 0-255
            - hsl: tuple (h, s, l) with h=0-360, s=0-100, l=0-100
            - hex: string in format '#RRGGBB'
            Returns None if the dialog was cancelled.
        """
        title = MessageCatalog.translate(title)
        super().__init__(parent=parent, title=title)
        self.initialcolor = initialcolor
        self.dropper = ColorDropperDialog()
        self.dropper.result.trace_add('write', self.trace_dropper_color)

    def create_body(self, master: tkinter.Misc) -> None:
        self.colorchooser = ColorChooser(master, self.initialcolor)
        self.colorchooser.pack(fill=BOTH, expand=YES)

    def create_buttonbox(self, master: tkinter.Misc) -> None:
        frame = ttk.Frame(master, padding=(5, 5))

        # OK button
        ok = ttk.Button(frame, bootstyle=PRIMARY, text=MessageCatalog.translate('OK'))
        ok.bind("<Return>", lambda _: ok.invoke())
        ok.configure(command=lambda b=ok: self.on_button_press(b))
        ok.pack(padx=2, side=RIGHT)

        # Cancel button
        cancel = ttk.Button(frame, bootstyle=SECONDARY, text=MessageCatalog.translate('Cancel'))
        cancel.bind("<Return>", lambda _: cancel.invoke())
        cancel.configure(command=lambda b=cancel: self.on_button_press(b))
        cancel.pack(padx=2, side=RIGHT)

        # color dropper (not supported on Mac OS)
        if self._toplevel.winsys != 'aqua':
            dropper = ttk.Label(frame, text=PEN, font=('-size 16'))
            ToolTip(dropper, MessageCatalog.translate('color dropper'))  # add tooltip
            dropper.pack(side=RIGHT, padx=2)
            dropper.bind("<Button-1>", self.on_show_colordropper)

        frame.pack(side=BOTTOM, fill=X, anchor=S)

    def on_show_colordropper(self, event: tkinter.Event) -> None:
        self.dropper.show()

    def trace_dropper_color(self, *_: Any) -> None:
        values = self.dropper.result.get()
        self.colorchooser.hex.set(values[2])
        self.colorchooser.sync_color_values('hex')

    def on_button_press(self, button: ttk.Button) -> None:
        if button.cget('text') == MessageCatalog.translate('OK'):
            values = self.colorchooser.get_variables()
            self._result = ColorChoice(
                rgb=(values.r, values.g, values.b),
                hsl=(values.h, values.s, values.l),
                hex=values.hex
            )
            self._toplevel.destroy()
        self._toplevel.destroy()

__init__(parent=None, title='Color Chooser', initialcolor=None)

Create a color chooser dialog.

The dialog displays a ColorChooser widget in a modal dialog with OK and Cancel buttons. When OK is pressed, the result property returns a ColorChoice namedtuple containing the selected color in multiple formats (rgb, hsl, hex). The dialog also includes a color dropper tool for selecting colors from the screen (Windows/Linux only).

Parameters:

parent (Widget):
    Parent widget. The dialog will be modal and centered on this
    widget.

title (str):
    The dialog window title (default='Color Chooser', will be
    localized).

initialcolor (str):
    Initial color to display in the chooser. Can be any valid color
    string format (e.g., '#FF0000', 'red', 'rgb(255,0,0)'). If None,
    uses the theme's background color.

Returns:

Type Description
None

The result property contains a ColorChoice namedtuple with fields:

None
  • rgb: tuple (r, g, b) with values 0-255
None
  • hsl: tuple (h, s, l) with h=0-360, s=0-100, l=0-100
None
  • hex: string in format '#RRGGBB'
None

Returns None if the dialog was cancelled.

Source code in src/ttkbootstrap/dialogs/colorchooser.py
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
def __init__(
        self, parent: Optional[tkinter.Misc] = None, title: str = "Color Chooser",
        initialcolor: Optional[str] = None) -> None:
    """Create a color chooser dialog.

    The dialog displays a ColorChooser widget in a modal dialog with OK
    and Cancel buttons. When OK is pressed, the result property returns a
    ColorChoice namedtuple containing the selected color in multiple formats
    (rgb, hsl, hex). The dialog also includes a color dropper tool for
    selecting colors from the screen (Windows/Linux only).

    Parameters:

        parent (Widget):
            Parent widget. The dialog will be modal and centered on this
            widget.

        title (str):
            The dialog window title (default='Color Chooser', will be
            localized).

        initialcolor (str):
            Initial color to display in the chooser. Can be any valid color
            string format (e.g., '#FF0000', 'red', 'rgb(255,0,0)'). If None,
            uses the theme's background color.

    Returns:
        The result property contains a ColorChoice namedtuple with fields:
        - rgb: tuple (r, g, b) with values 0-255
        - hsl: tuple (h, s, l) with h=0-360, s=0-100, l=0-100
        - hex: string in format '#RRGGBB'
        Returns None if the dialog was cancelled.
    """
    title = MessageCatalog.translate(title)
    super().__init__(parent=parent, title=title)
    self.initialcolor = initialcolor
    self.dropper = ColorDropperDialog()
    self.dropper.result.trace_add('write', self.trace_dropper_color)