Can I use rgb in tkinter?
No, tkinter does not support RGB, but you can write a small helper function to remedy this:
Maybe something like this, where the argument rgb
must be a valid rgb code represented as a tuple of integers.
import tkinter as tkdef _from_rgb(rgb): """translates an rgb tuple of int to a tkinter friendly color code """ return "#%02x%02x%02x" % rgb root = tk.Tk()root.configure(bg=_from_rgb((0, 10, 255))) root.mainloop()
If you find it more readable, you can also use fstrings
to achieve the exact same result:
def _from_rgb(rgb): """translates an rgb tuple of int to a tkinter friendly color code """ r, g, b = rgb return f'#{r:02x}{g:02x}{b:02x}'
Note that the colorsys
module from the Python standard library can help translate from HSV
, HLS
, and YIQ
color systems
Sorry for not answering correctly, I didn't think it through properly. Now that I have seen your comment @Mike-SMT, I've improved it
from tkinter import *import randomimport timewindow = Tk()window.title("Random colours")colours = ["black", "cyan", "magenta", "red", "blue", "gray" ]bgColour = window.configure(background = random.choice(colours))window.update()time.sleep(1)bgColour1 = window.configure(background = random.choice(colours))window.mainloop()
Have fun with this, you can get creative and do things like making this into a forever changing background colour, or making a sequence. There are so many different colours on Tkinter that you can use, so you can manually change each colour after every few seconds if you really want. Another thing I forgot to mention previously, you can't use a time.sleep() option as Mike said, but it will work if you use a ".update()" line of code, as shown above.