menator01
Gold Coder
Not posted in a while so, here is a quick tkinter app for getting a hex value for color. Enjoy.
Python:
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
self.parent.geometry('1024x200+300+300')
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]
colors.sort()
self.view.dropbox['values'] = colors
self.view.dropbox.current(0)
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())
root.mainloop()