Getting color codes of the current tk palette

Wendla

In tkinter, is it possible to get the colours of the currently used palette, so that I can use them e.g. when drawing a rectangle on the canvas?

import tkinter as tk
root = tk.Tk()
root.tk_bisque() # change the palette to bisque
canvas = tk.Canvas(root, width=500, height=500)
canvas.create_rectangle(10, 10, 100, 100, fill='???') # What to enter here?

I know I can use e.g. 'bisque' as a colour name, however the documentation speaks of a database containing entries like 'activeBackground', 'highlightColor', etcetera. I want to know how to use those as colours for my canvas items, or alternatively simply how to get their rgb values at runtime.

j_4321

You can use root.option_get(name, '.') to get the default colors of the widget:

import tkinter as tk
root = tk.Tk()
root.tk_bisque() # change the palette to bisque
print(root.option_get('background', '.'))
print(root.option_get('activeBackground', '.'))
print(root.option_get('foreground', '.'))
print(root.option_get('highlightColor', '.'))

gives

#ffe4c4
#e6ceb1
black
black

If you need the color for a specific widget class, replace '.' by the class name. As mentioned in the comments, if you need the RGB value of the color, you can use root.winfo_rgb(color) where color is either in HEX format or one of tkinter predefined colors such as black, ... (you can find a list here for instance).

However, on my computer (I am using Linux and I don't know if the behavior is the same on all platforms) it only works after setting the color scheme to bisque, for the default color scheme it always return ''.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related