简体   繁体   English

如何在 tkinter 中显示彩色表情符号?

[英]How to display colored emojis in tkinter?

Is there any way to display colored emojis in tkinter?有没有办法在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:当我运行这段代码时,我得到如下信息:

在此处输入图像描述

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? tkinter有什么办法可以实现吗?

It would be great if anyone could help me out.如果有人能帮助我,那就太好了。

It seems impossible (to me) to show coloured emojis in Tkinter whether using ttk or tk. (对我来说)似乎不可能在 Tkinter 中显示彩色表情符号,无论是使用 ttk 还是 tk。 So for showing coloured emoji in Tkinter, I have an idea.因此,对于在 Tkinter 中显示彩色表情符号,我有一个想法。 First I have downloaded a picture of the emoji from google.首先,我从谷歌下载了一张表情符号的图片。 Image link: https://i.pinimg.com/originals/79/c2/71/79c2714528ebf4669603e32121ae6019.png图片链接: 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.最后,您必须在 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:这是我的 output: 我的输出

I think it is your desired output.我认为这是您想要的 output。

I'm still working on my English.我还在努力学习我的英语。

pip install pywin32, pip install pillow pip 安装 pywin32,pip 安装 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()

I modified @kimhyunju answer for my own purposes.我出于自己的目的修改了@kimhyunju 的答案。 This is the easiest solution to my problem of trying to integrate full color emojis into my tkinter app.这是我尝试将全彩色表情符号集成到我的 tkinter 应用程序中的问题的最简单解决方案。 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.我还使用 customtkinter,它返回 CTkImage 而不是 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM