How to display colored emojis in tkinter?

Question:

Is there any way to display colored emojis in tkinter?

Here’s the code:

from tkinter import *
import pyperclip

root = Tk()

def copy():
    pyperclip.copy(button['text'])
    print("Copied!")

button = Button(root , text = " " , font = "arial 70" , command = copy)
button.pack()

mainloop()

When I run this code, I get something like this:

enter image description here

Here, the emoji shown in the button is completely black and it is not colored.

I know I could have used an image of the emoji in my button, but it won’t be possible if I have to do the same thing for hundreds of emojis.

What I want is to make the emoji colored so that it will be easier for people to recognize it.

Is there any way to achieve this in tkinter?

It would be great if anyone could help me out.

Asked By: Lenovo 360

||

Answers:

It seems impossible (to me) to show coloured emojis in Tkinter whether using ttk or tk. So for showing coloured emoji in Tkinter, I have an idea. First I have downloaded a picture of the emoji from google. Image link: https://i.pinimg.com/originals/79/c2/71/79c2714528ebf4669603e32121ae6019.png

Then save the image in the same directory where your code is saved.

At last you have to use this image in a label. Here is the code of how I have used this image:

from tkinter import *
from tkinter import ttk

root = Tk()

account_bitmap = PhotoImage(file = "emoji.png") 
account_bitmap = account_bitmap.subsample(3, 3)

label = ttk.Label(root , image= account_bitmap, compound= TOP)
label.pack()

mainloop()

Here is my output:
My Output

I think it is your desired output.

Answered By: Sabil Islam

I’m still working on my English.

pip install pywin32,
pip install pillow

from PIL import Image, ImageDraw, ImageFont, ImageTk
import tkinter as tk
import win32clipboard

def emoji_img(size, text):
    font = ImageFont.truetype("seguiemj.ttf", size=int(round(size*72/96, 0))) 
    # pixels = points * 96 / 72 : 96 is windowsDPI
    im = Image.new("RGBA", (size, size), (255, 255, 255, 0))
    draw = ImageDraw.Draw(im)
    draw.text((size/2, size/2), text, embedded_color=True, font=font, anchor="mm")
    return ImageTk.PhotoImage(im)

def copy():
    emoji_data = button['text']
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, emoji_data)
    win32clipboard.CloseClipboard()
    print("Copied!", emoji_data)

root = tk.Tk()
text=" "
emoji = emoji_img(80, text)
button = tk.Button(root, image=emoji, text=text, command=copy)
button.pack()
root.mainloop()
Answered By: kimhyunju

I modified @kimhyunju answer for my own purposes. This is the easiest solution to my problem of trying to integrate full color emojis into my tkinter app. Originally I wanted full color emoji’s in button text but it doesn’t seem possible. I have altered the solution a bit and added transparency. I also use customtkinter it returns a CTkImage instead of a PhotoImage.

from customtkinter import CTkButton as Btn, CTkImage, CTk
from PIL import Image, ImageDraw, ImageFont


def emoji(emoji, size=32):
    # convert emoji to CTkImage
    font = ImageFont.truetype("seguiemj.ttf", size=int(size/1.5))
    img = Image.new("RGBA", (size, size), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    draw.text((size/2, size/2), emoji,
              embedded_color=True, font=font, anchor="mm")
    img = CTkImage(img, size=(size, size))
    return img


app = CTk()

btn = Btn(app, text=None, fg_color="transparent", image=emoji(" "))
btn.pack()

app.mainloop()
Answered By: 3V1LXD
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.