Python รองรับแพ็คเกจ PIL หรือ Pillow ซึ่งเป็นไลบรารีโอเพนซอร์สสำหรับการเปิด จัดการ และบันทึกรูปแบบต่างๆ ของรูปภาพในโครงการ Python เราสามารถนำมาใช้ในแอปพลิเคชัน Tkinter เพื่อประมวลผลและแสดงภาพได้
วิดเจ็ตป้ายกำกับใน Tkinter ใช้เพื่อแสดงข้อความและรูปภาพในแอปพลิเคชัน Tkinter หากต้องการแสดงรูปภาพด้วยวิดเจ็ตป้ายกำกับในแอปพลิเคชัน Tkinter เราสามารถทำตามขั้นตอนเหล่านี้ได้
-
ตรวจสอบให้แน่ใจว่าได้ติดตั้งแพ็คเกจ Pillow หรือ PIL ในระบบของคุณแล้ว
-
โหลดรูปภาพในตัวแปรโดยใช้ฟังก์ชัน ImageTk.PhotoImage(file=file_location)
-
สร้างวิดเจ็ตป้ายกำกับเพื่อกำหนดค่ารูปภาพเป็นรูปภาพ
-
รันโค้ดเพื่อแสดงภาพ
ตัวอย่าง
# Import the required libraries
from tkinter import *
from PIL import Image, ImageTk
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x470")
# load the image and convert it into Tkinter Photoimage
bg=ImageTk.PhotoImage(file="baseball.png")
# Add a label widget to display the image
label=Label(win, image=bg)
label.place(x=0, y=0)
win.mainloop() ผลลัพธ์
รันโค้ดเพื่อแสดงรูปภาพในหน้าต่าง Tkinter
