Color Handling

tks.colors provides 3 classes to obtain a color from a user.

ColorVar
A Tk variable which holds an RGB color.
ColorEntry
Displays an entry box to enter a color as well as a button to display a color selection dialog.
ColorDialog
Displays a dialog window allowing the user to select a color using a color wheel or sliders.
class tks.colors.ColorVar(master=None, value=None, name=None)

A Tkinter Variable subclass to store an RGB color tuple.

set(value)

Set the color tuple to be stored.

class tks.colors.ColorEntry(master, variable=None, color_format='rgbhex', fonts=None, colors=None)

Displays an entry to enter color information and a button to display a selection dialog.

Parameters:
  • master – Tk master widget
  • variable (tks.colors.ColorVar) – The variable which hold the color to display in the entry box.
  • color_format (str) – How to display the color in the entry box. One of the following rgbhex, rgb, hsv or hls
  • fonts – Fonts to use
rgb

RGB representation of the selected color

hsv

HSV representation of the selected color

hls

HLS representation of the selected color

class tks.colors.ColorDialog(master, title, start_color=(0.5, 0.5, 0.5), fonts=None)

Display a dialog to obtain an RGB value.

The color is returned as an (R, G, B) tuple where each component is between 0.0 and 1.0

Parameters:
  • master – The master widget
  • title (str) – The window title
  • start_color (tuple) –

    The initial (R, G, B) tuple to display.

    If any element of the tuple is greater than 1.0 then it is assumed that all values need to be scaled by 255.0 both when setting and obtaining the color value.

tks.color_wheel

Implements a color wheel with an outer ring to select a hue and a triangle within that where saturation and value can be selected

class tks.color_wheel.ColorWheel(master, variable=None, radius=125)

Displays an HSV color wheel.

tks.color_slider

3 element sliders to change RGB, HSV and HLS variables

class tks.color_slider.RGBSlider(master, variable=None, fonts=None)

Bases: tks.color_slider.ColorSlider

An RGB Color Slider

class tks.color_slider.HSVSlider(master, variable=None, fonts=None)

Bases: tks.color_slider.ColorSlider

An HSV Color Slider

class tks.color_slider.HLSSlider(master, variable=None, fonts=None)

Bases: tks.color_slider.ColorSlider

An HLS Color Slider

class tks.color_slider.ColorSlider(master, variable=None, fonts=None)

Color slider base class.

from_rgb(rgb)

Convert from RGB

to_rgb(value)

Convert to RGB

tks.color_square

ColorSquare displays a solid square which changes color depending on the value of a variable. Also displays textual color information below.

class tks.color_square.ColorSquare(master, variable=None, mode='rw', color_info=('rgbhex', ), dnd_target=True, dnd_source=True, fonts=None)

Displays a colored rectangle and a text description of the color below it. A popup menu is provided to copy the hex, RGB, HSV and HLS representations of the color.

Parameters:
  • variable (ColorVar) – The RGB color to display
  • mode (str) – One of r, w, rw. If r is specified then the widget responds to changes in the variable. If w is specified then the color is written to the variable on a left mouse click.
  • color_info

    The color information to display under the square as a text representation of the color. The elements are specified as a tuple where the following strings can be provided.

    rgbhex, rgb, hsv, hls

  • dnd_target (bool) – If True then the square responds to colors being dropped on it.
  • dnd_source (bool) – If True the square works as a drag and drop source.
rgb

The RGB tuple to display. If None the the rectangle is cleared and the text set to the empty string.

dnd_accept(source, event)

Indicate that we can handle a drag and drop operation.

dnd_enter(source, event)

Called by the drag and drop machinery when the mouse enters the canvas.

dnd_motion(source, event)

Called by the drag and drop machinery when the mouse moves within the canvas.

dnd_leave(source, event)

Called by the drag and drop machinery when the mouse leaves the canvas.

dnd_commit(source, event)

Called by the drag and drop machinery when the mouse is released over the canvas.

dnd_end(target, event)

Called by the drag and drop machinery to end the operation.

tks.color_tints_and_shades

Display tints and shades of a base color. This module provides 2 classes both of which display a number of ColorSquares enclosed in a LabelFrame along with a scale to adjust the percent between the colors.

ColorTint
Displays a set of tints
ColorShade
Displays a set of shades
class tks.color_tints_and_shades.ColorTint(master, variable, count=5, percent=(1, 5))

Displays a sequence of tints.

Parameters:
  • variable (tks.vars.ColorVar) – The RGB color to produce a set of tints from
  • count (int) – The number of tints to display
  • percent (tuple) – Determines the percent between the tints specified as a min and a max range.
class tks.color_tints_and_shades.ColorShade(master, variable, count=5, percent=(1, 5))

Displays a sequence of shades.

Parameters:
  • variable (tks.vars.ColorVar) – The RGB color to produce a set of shades from
  • count (int) – The number of shades to display
  • percent (tuple) – Determines the percentage percent between the shades specified as a min and a max range.

tks.color_palette

A Tkinter widget to select colors from a palette of colors

class tks.color_palette.PaletteSelector(master, variable=None, height=400)

A widget to display a set of colors from a palette.