Python 3 Tkinter Random Color Hexadecimal (RGB) Generator With Preview GUI Desktop App

Python 3 Tkinter Random Color Hexadecimal (RGB) Generator With Preview GUI Desktop App

Python 3 Tkinter Random Color Hexadecimal (RGB) Generator With Preview GUI Desktop App

#!/usr/bin/env python

import tkinter as Tkinter

FONT = ('courier new',24,'bold')

def main():
    mainWindow = Tkinter.Tk()
    mainWindow.title("Color Preview")
    mainFrame = Tkinter.Frame(mainWindow)
    mainFrame.pack(side=Tkinter.RIGHT, expand=Tkinter.YES, fill=Tkinter.BOTH)
    colorSpace = ColorCanvas(mainWindow)
    redScale   = Tkinter.Scale(mainFrame,orient = Tkinter.HORIZONTAL,from_=0,to=255,relief = Tkinter.RAISED,length=300,sliderlength=20,resolution=1, command=colorSpace.setRed)
    greenScale = Tkinter.Scale(mainFrame,orient = Tkinter.HORIZONTAL,from_=0,to=255,relief = Tkinter.RAISED,length=300,sliderlength=20,resolution=1, command=colorSpace.setGreen)
    blueScale  = Tkinter.Scale(mainFrame,orient = Tkinter.HORIZONTAL,from_=0,to=255,relief = Tkinter.RAISED,length=300,sliderlength=20,resolution=1, command=colorSpace.setBlue)
    redLabel = Tkinter.Label(mainFrame, text="Red")
    greenLabel = Tkinter.Label(mainFrame, text="Green")
    blueLabel = Tkinter.Label(mainFrame, text="Blue")
    colorSpace.addScales(redScale, greenScale, blueScale)
    colorSpace.pack(side=Tkinter.LEFT, fill=Tkinter.BOTH)
    redLabel.pack(side=Tkinter.TOP)
    redScale.pack(side=Tkinter.TOP, expand=Tkinter.YES, fill=Tkinter.Y)
    greenLabel.pack(side=Tkinter.TOP)
    greenScale.pack(side=Tkinter.TOP, expand=Tkinter.YES, fill=Tkinter.Y)
    blueLabel.pack(side=Tkinter.TOP)
    blueScale.pack(side=Tkinter.TOP, expand=Tkinter.YES, fill=Tkinter.Y)
    mainWindow.protocol('WM_DELETE_WINDOW', mainWindow.destroy)
    mainWindow.mainloop()

class ColorCanvas(Tkinter.Canvas):
    def __init__(self, master, *args, **kwargs):
        Tkinter.Canvas.__init__(self, master, *args, **kwargs)
        self.__redScale, self.__greenScale, self.__blueScale = [False] * 3
        self.__t = self.create_text(10, 10, text='', font=FONT, anchor=Tkinter.NW)
        try:
            self.__red   = int(kwargs['background'][1:3], 16)
            self.__green = int(kwargs['background'][3:5], 16)
            self.__blue  = int(kwargs['background'][5:], 16)
        except:
            self.__red, self.__green, self.__blue = [0] * 3

    def addScales(self, redScale, greenScale, blueScale):
        self.__redScale, self.__greenScale, self.__blueScale = redScale, greenScale, blueScale

    def toHex(self, integer):
        if integer < 16:
            return '0%s' % hex(integer)[2:]
        else:
            return '%s' % hex(integer)[2:]

    def showHexColor(self, colorstring):
        if (self.__red + self.__green + self.__blue) / 3 > 128:
            textcolor = '#000000'
        else:
            textcolor = '#ffffff'
        self.itemconfigure(self.__t, text=colorstring, fill=textcolor)

    def setRed(self, event=None):
        if self.__redScale:
            self.__red = self.__redScale.get()
            colorstring = '#%s%s%s' % (self.toHex(self.__red), self.toHex(self.__green), self.toHex(self.__blue))
            self.configure(background=colorstring)
            self.showHexColor(colorstring)

    def setGreen(self, event=None):
        if self.__greenScale:
            self.__green = self.__greenScale.get()
            colorstring = '#%s%s%s' % (self.toHex(self.__red), self.toHex(self.__green), self.toHex(self.__blue))
            self.configure(background=colorstring)
            self.showHexColor(colorstring)

    def setBlue(self, event=None):
        if self.__blueScale:
            self.__blue = self.__blueScale.get()
            colorstring = '#%s%s%s' % (self.toHex(self.__red), self.toHex(self.__green), self.toHex(self.__blue))
            self.configure(background=colorstring)
            self.showHexColor(colorstring)


main()
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment