'Can I use rgb in tkinter?

Can I use rbg instead of hex in tkinter? If so, how can I do it? I am planning to use this feature to make sort of a gradient from one color to another and I plan to make a for loop to change it from 1 to 255 in a few seconds.

from tkinter import *

root = Tk()
root.configure(background="can i use rgb instead of hex here?")
root.mainloop()


Solution 1:[1]

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 tk


def _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

Solution 2:[2]

def rgbtohex(r,g,b):
    return f'#{r:02x}{g:02x}{b:02x}'

print(rgbtohex(r=255, g=255, b=255))

Hope this helps some of you

Solution 3:[3]

This will make a gradient.

from tkinter import *
root=Tk()
root.title("colors")

cv=Canvas(root, width=20, height=255)
cv.pack()

def _from_rgb(rgb):
    """translates an rgb tuple of int to a tkinter friendly color code
    """
    return "#%02x%02x%02x" % rgb 

for canvas in range(255):
    cv.create_line(0,canvas, 20,canvas, fill=_from_rgb((canvas, canvas, 
canvas)))

You can have fun with this. You can try this to:

from tkinter import *

def dec2hex(r,g,b):
    return f'#{r:02x}{g:02x}{b:02x}'

root=Tk()

cv=Canvas(highlightthickness=0)
cv.pack(expand=True, fill=BOTH)

for color in range(510):
    cv.create_line(0,color, 3000,color, fill=dec2hex(0, round((510-color)/2), round(color/2)))

for color in range(510):
    cv.create_line(0,(color+510), 3000,(color+510), fill=dec2hex(round(color/2), 0, round((510-color)/2)))

Solution 4:[4]

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 random
import time

window = 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Anton Pomieshchenko
Solution 3
Solution 4 Hussain Hammad