• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Playing around with tkinter and background colors

menator01

Silver Coder
Changes background color every 3 seconds
Python:
from dataclasses import dataclass, astuple
import tkinter as tk
from random import randint

  
@dataclass
class RGB:
    __slots__ = ['r','g','b']
    r: int
    g: int
    b: int

    def __str__(self):
        return f'#%02x%02x%02x'.upper() % astuple(self)
    
def color(arg, arg2):
    atup = [randint(0,255) for i in range(3)]
    atup = tuple(atup)
    colors = RGB(*atup)
    arg['text'] = f'Color: {colors}'
    arg2['bg'] = colors
    root.after(3000, lambda: color(arg, arg2))

root = tk.Tk()
root.geometry('400x400+300+300')
colors = RGB(22,50,23)

label = tk.Label(root, font=(None, 28, 'normal'))
label['text'] = f'Color: {colors}'
label.pack(side='top')

bgcolor = tk.Label(root, bg=colors, relief='sunken')
bgcolor.pack(side='top', expand=True, fill='both', padx=5, pady=3)

root.after(3000, lambda: color(label, bgcolor))
root.mainloop()
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

300x250
Top Bottom