Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

python - How can I build a Label in Tkinter containing Text, Emoji Icon and a Link

I want to build a label like on the sample in my GUI. I looked for a solution online, but I couldn't find anything. I want to use the FontAwesome Heart Icon.

Can you help me please?

My imports:

from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from pytube import * 
from PIL import Image, ImageTk
import fontawesome as fa

(Maybe some imports are unnecessary)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's difficult to have all three types of values in the same label, but you can split them into 4 different labels. You can make the first three using two lists and creating labels based on the text and colors in the two lists, words and colors. You can separately make the fourth label, the link, using .bind().

Code:

from tkinter import *
import webbrowser

def callback(url):
    webbrowser.open_new(url)

words = ["Made with", "?", "by"]
colors = ["white", "red", "white"]

root = Tk()
for index, word in enumerate(words):
    Label(root, text = word, bg="black", fg=colors[index]).grid(column=index, row=0)
link = Label(root, text = "LINK", font="TKDefaultFont 9 underline", fg = "white", bg = "black", cursor="hand2")
link.grid(column=len(words),row=0)
link.bind("<Button-1>", lambda e: callback("https://www.google.com"))

root.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...