[英]How to display colored emojis in tkinter?
有没有办法在tkinter中显示彩色表情符号?
这是代码:
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()
当我运行这段代码时,我得到如下信息:
在这里,按钮中显示的表情符号是完全黑色的,没有着色。
我知道我可以在我的按钮中使用表情符号的图像,但如果我必须对数百个表情符号执行相同的操作,那将是不可能的。
我想要的是将表情符号着色,以便人们更容易识别它。
tkinter有什么办法可以实现吗?
如果有人能帮助我,那就太好了。
(对我来说)似乎不可能在 Tkinter 中显示彩色表情符号,无论是使用 ttk 还是 tk。 因此,对于在 Tkinter 中显示彩色表情符号,我有一个想法。 首先,我从谷歌下载了一张表情符号的图片。 图片链接: https://i.pinimg.com/originals/79/c2/71/79c2714528ebf4669603e32121ae6019.png
然后将图像保存在保存代码的同一目录中。
最后,您必须在 label 中使用此图像。 这是我如何使用此图像的代码:
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()
我认为这是您想要的 output。
我还在努力学习我的英语。
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()
我出于自己的目的修改了@kimhyunju 的答案。 这是我尝试将全彩色表情符号集成到我的 tkinter 应用程序中的问题的最简单解决方案。 最初我想要按钮文本中的全彩色表情符号,但它似乎不可能。 我稍微改变了解决方案并增加了透明度。 我还使用 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.