跳转至

ColorChooserDialog

ttkbootstrap.dialogs.colorchooser.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.

Examples:

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

create_body(self, master)

Create the dialog body.

This method should be overridden and is called by the build method. Set the self._initial_focus for the widget that should receive the initial focus.

Parameters:

Name Type Description Default
master Widget

The parent widget.

required
Source code in ttkbootstrap/dialogs/colorchooser.py
def create_body(self, master):
    self.colorchooser = ColorChooser(master, self.initialcolor)
    self.colorchooser.pack(fill=BOTH, expand=YES)

create_buttonbox(self, master)

Create the dialog button box.

This method should be overridden and is called by the build method. Set the self._initial_focus for the button that should receive the intial focus.

Parameters:

Name Type Description Default
master Widget

The parent widget.

required
Source code in ttkbootstrap/dialogs/colorchooser.py
def create_buttonbox(self, master):
    frame = ttk.Frame(master, padding=(5, 5))

    # OK button
    ok = ttk.Button(frame, bootstyle=PRIMARY, width=6, 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, width=6, 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)