python-3.x 取十六进制码/rgb,在tkinter中显示颜色

k2fxgqgv  于 2023-01-27  发布在  Python
关注(0)|答案(2)|浏览(167)

有没有人知道一种方法来获取一个rgb或十六进制代码并在tkinter中显示它?我只是在学习图形用户界面之类的东西。谢谢
这里我有一个简单的图形用户界面,它会告诉你一些基本颜色的RGB和十六进制代码。不知道如何做相反的事情,这就是我想知道的。

import tkinter as tk
from tkinter import *

root = tk.Tk()

def color_entry_function(color, arg=None):
    color_entry_get = color_entry.get()
    if color_entry_get.lower() == "blue":
        blue = Label(root, text="#0000FF\nrgb(0, 0, 255)\n")
        blue.pack(anchor="center")

    elif color_entry_get.lower() == "red":
        red = Label(root, text="#FF0000\nrgb(255,0,0)\n")
        red.pack(anchor="center")

    elif color_entry_get.lower() == "green":
        green = Label(root, text="#0000FF\nrgb(0, 0, 255)\n")
        green.pack(anchor="center")

    elif color_entry_get.lower() == "yellow":
        yellow = Label(root, text="#FFFF00\nrgb(255,255,0)\n")
        yellow.pack(anchor="center")

Welcome = Label(root, text="Color to Hex Converter")
Welcome.pack(anchor="center")
Welcome_2 = Label(root, text="Enter the color you want to convert:\n")
Welcome_2.pack(anchor="center")

color_entry = Entry(root, justify="center", width=20)
color_entry.focus()
color_entry.bind("<Return>", color_entry_function)
color_entry.pack(anchor="center")

color_entry_linebreak = Label(root, text="")
color_entry_linebreak.pack()

root.mainloop()
axkjgtzd

axkjgtzd1#

假设是256色RGB
要从十六进制代码字符串转换为rgb元组:

hexcode = "#000000" # black
rgb = tuple(int(hexcode.lstrip('#')[n:n+2], 16) for n in range(0, 6, 2)) # tuple comprehension

要从rgb元组转换为十六进制码字符串:

rgb = (0, 0, 0) # black
hexcode = f"#{rgb[0]:02X}{rgb[1]:02X}{rgb[2]:02X}" # converting each value
hexcode = f"#{''.join([f'{c:02X}' for c in rgb])}" # converting all values with list comprehension
hexcode = "#" + ''.join([f"{c:02X}" for c in rgb]) # for old pythons without f-string nesting

要更改tkinter.Label的颜色,请使用bg作为背景色或fg作为文本颜色。必须提供十六进制代码字符串:

import tkinter
root = tkinter.Tk()
label = tkinter.Label(root, text = "COLOR", bg = "#0000FF", fg = "#FFFF00")
label.pack()
root.mainloop()

添加了这些功能的程序:

import tkinter as tk
from tkinter import *

color_list = [
    ('black', (0, 0, 0)),
    ('blue', (0, 0, 255)),
    ('red', (255, 0, 0)),
    ('green', (0, 0, 255)),
    ('yellow', (255, 255, 0)),
    ('white', (255, 255, 255)),
]

hex_list = [f"{n:02X}" for n in range(256)]

def name_to_hex_rgb(event):
    try:
        color = color_list[name_listbox.curselection()[0]]
    except IndexError:
        color = color_list[0]
    color_name = color[0]
    color_rgb = color[1]
    color_hex = f"#{''.join([f'{rgb:02X}' for rgb in color_rgb])}"
    color_inverse = f"""#{''.join([f'{rgb:02X}' for rgb in 
        [255 - color for color in color_rgb]])}"""
    name_label.config(
        bg = color_hex,
        fg = color_inverse,
        text = f"""name: {color_name}\nhex: {color_hex}\nrgb: \
{color_rgb}""",
    )

def hex_to_rgb(event):
    try:
        color_1 = hex_listbox_1.curselection()[0]
    except IndexError:
        color_1 = 0
    try:
        color_2 = hex_listbox_2.curselection()[0]
    except IndexError:
        color_2 = 0
    try:
        color_3 = hex_listbox_3.curselection()[0]
    except IndexError:
        color_3 = 0
    color_rgb = (
        int(hex_list[color_1], 16),
        int(hex_list[color_2], 16),
        int(hex_list[color_3], 16),
    )
    color_rgb_inverse = (
        255 - int(hex_list[color_1], 16),
        255 - int(hex_list[color_2], 16),
        255 - int(hex_list[color_3], 16),
    )
    color_hex = f"""#{hex_list[color_1]}\
{hex_list[color_2]}\
{hex_list[color_3]}"""
    color_hex_inverse = f"""#{hex_list[255 - color_1]}\
{hex_list[255 - color_2]}\
{hex_list[255 - color_3]}"""
    text = f"rgb: {color_rgb}"
    color_name = [color[0] for color in color_list if color[1] == \
        color_rgb]
    if len(color_name) > 0:
        text = f"name: {color_name[0]}\n" + text
    hex_label.config(bg = color_hex, fg = color_hex_inverse,
        text = text)

def rgb_to_hex(event):
    try:
        color_1 = rgb_listbox_1.curselection()[0]
    except IndexError:
        color_1 = 0
    try:
        color_2 = rgb_listbox_2.curselection()[0]
    except IndexError:
        color_2 = 0
    try:
        color_3 = rgb_listbox_3.curselection()[0]
    except IndexError:
        color_3 = 0
    color_rgb = (color_1, color_2, color_3)
    color_rgb_inverse = (255 - color_1, 255 - color_2, 255 - color_3)
    color_hex = f"#{color_1:02X}{color_2:02X}{color_3:02X}"
    color_hex_inverse = f"""#{255 - color_1:02X}{255 - color_2:02X}\
{255 - color_3:02X}"""
    text = f"hex: {color_hex}"
    color_name = [color[0] for color in color_list if color[1] == \
        color_rgb]
    if len(color_name) > 0:
        text = f"name: {color_name[0]}\n" + text
    rgb_label.config(bg = color_hex, fg = color_hex_inverse,
        text = text)

root = tk.Tk()
root.title("Color, RGB and Hex Converter")

name_frame = Frame(root)
name_frame.pack(anchor = "nw", fill = BOTH, side = "left")

name_label = Label(name_frame, text = " ")
name_label.pack(anchor = "center")

name_title = Label(name_frame, text = "NAME to HEX & RGB")
name_title.pack(anchor = "center")

name_listbox = Listbox(
    name_frame,
    listvariable = Variable(value = [color[0] for color in color_list]),
    justify = "center",
    width = 18,
)
name_listbox.bind("<<ListboxSelect>>", name_to_hex_rgb)
name_listbox.pack()

hex_frame = Frame(root)
hex_frame.pack(anchor = "n", fill = BOTH, side = "left")

hex_label = Label(hex_frame, text = "")
hex_label.pack(anchor = "center")

hex_title = Label(hex_frame, text = "HEX to RGB")
hex_title.pack(anchor = "center")

hex_listbox_1 = Listbox(
    hex_frame,
    listvariable = Variable(value = hex_list),
    exportselection = False,
    justify = "center",
    width = 6,
)
hex_listbox_1.bind("<<ListboxSelect>>", hex_to_rgb)
hex_listbox_1.pack(anchor = "nw", fill = BOTH, side = "left")

hex_listbox_2 = Listbox(
    hex_frame,
    listvariable = Variable(value = hex_list),
    exportselection = False,
    justify = "center",
    width = 6,
)
hex_listbox_2.bind("<<ListboxSelect>>", hex_to_rgb)
hex_listbox_2.pack(anchor = "n", fill = BOTH, side = "left")

hex_listbox_3 = Listbox(
    hex_frame,
    listvariable = Variable(value = hex_list),
    exportselection = False,
    justify = "center",
    width = 6,
)
hex_listbox_3.bind("<<ListboxSelect>>", hex_to_rgb)
hex_listbox_3.pack(anchor = "ne", fill = BOTH, side = "left")

rgb_frame = Frame(root)
rgb_frame.pack(anchor = "ne", fill = BOTH, side = "left")

rgb_label = Label(rgb_frame, text = "")
rgb_label.pack(anchor = "center")

rgb_title = Label(rgb_frame, text = "RGB to HEX")
rgb_title.pack(anchor = "center")

rgb_listbox_1 = Listbox(
    rgb_frame,
    listvariable = Variable(value = list(range(256))),
    exportselection = False,
    justify = "center",
    width = 6,
)
rgb_listbox_1.bind("<<ListboxSelect>>", rgb_to_hex)
rgb_listbox_1.pack(anchor = "nw", fill = BOTH, side = "left")

rgb_listbox_2 = Listbox(
    rgb_frame,
    listvariable = Variable(value = list(range(256))),
    exportselection = False,
    justify = "center",
    width = 6,
)
rgb_listbox_2.bind("<<ListboxSelect>>", rgb_to_hex)
rgb_listbox_2.pack(anchor = "n", fill = BOTH, side = "left")

rgb_listbox_3 = Listbox(
    rgb_frame,
    listvariable = Variable(value = list(range(256))),
    exportselection = False,
    justify = "center",
    width = 6,
)
rgb_listbox_3.bind("<<ListboxSelect>>", rgb_to_hex)
rgb_listbox_3.pack(anchor = "ne", fill = BOTH, side = "left")

name_to_hex_rgb(None)
hex_to_rgb(None)
rgb_to_hex(None)

root.mainloop()
yyyllmsg

yyyllmsg2#

可以使用十六进制代码作为颜色规范:

tk.Label(root, background="#FF0000")

您可以使用通用小部件方法winfo_rgb将颜色规范转换为包含r、g和B分量的元组:

r, g, b  = root.winfo_rgb(color_entry.get())

注:r、g和B值将在0至65535的范围内(每通道16位)

相关问题