By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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.

Color name and hex value from web


Gold Coder
Not posted in a while so, here is a quick tkinter app for getting a hex value for color. Enjoy.

import tkinter as tk
from tkinter import ttk
from bs4 import BeautifulSoup
import requests

class Data:
    ''' Data queries the web for color hex and names from the web
        parses the page and stores the color name and hex values in a dict
    page = requests.get('https://htmlcolors.com/color-names')

    soup = BeautifulSoup(page.text, 'html.parser')
    colors = {}
    lines = soup.find_all('td', attrs={"class": "wt"})

    for line in lines:
        hextag = line['style'].split()[1]
        for color in line:
            colors[color.lower()] = hextag

class View:
        View is for display
    def __init__(self, parent):
        self.parent = parent
        self.parent['padx'] = 8
        self.parent['pady'] = 8

        header = tk.Label(parent, text=f'Color name and hex value: {len(Data.colors)} colors', pady=8, padx=8)
        header['font'] = (None, 28, 'bold')
        header['highlightbackground'] = 'black'
        header['highlightcolor'] = 'black'
        header['highlightthickness'] = 1
        header.pack(fill='x', side='top', padx=8, pady=8)

        self.label = tk.Label(parent, relief='groove',fg='white')
        self.label['font'] = (None, 16, 'normal')
        self.label.pack(side='top', fill='both', expand=True, padx=8, pady=8)

        self.var = tk.StringVar()

        self.dropbox = ttk.Combobox(parent, textvariable=self.var)
        self.dropbox['font'] = (None, 16, 'normal')
        self.dropbox.pack(side='top', fill='x', padx=8, pady=8)

class Controller:
        Controller handles the communications between classes
    def __init__(self, view, data):
        self.view = view
        self.data = data
        color_names = list(self.data.colors.keys())
        colors = [c.title() for c in color_names]
        self.view.dropbox['values'] = colors
        self.callback(self.view.dropbox.current(0), 0, 'w')

        self.view.var.trace('w', self.callback)
    def callback(self, var, index, mode):
        color = self.view.var.get().lower()
        self.view.label['bg'] = self.data.colors[color]
        self.view.label['text'] = self.data.colors[color].upper()

if __name__ == '__main__':
    root = tk.Tk()
    controller = Controller(View(root), Data())

New Threads

Latest posts

Buy us a coffee!

Top Bottom