簡體   English   中英

如何在 tkinter 中顯示彩色表情符號?

[英]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: 我的輸出

我認為這是您想要的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM