Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเพิ่มรูปภาพใน Tkinter ด้วย PIL/Pillow

รูปภาพใน Tkinter สามารถจัดการได้ด้วยแพ็กเกจ PIL/Pillow ซึ่งมีฟังก์ชันโหลด, ย่อ/ขยาย, และแสดงภาพผ่าน Widget เช่น Label

ตัวอย่างโค้ด

# Import the Tkinter library
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import Image, ImageTk

# Create an instance of Tkinter frame
win = Tk()

# Define the geometry
win.geometry("750x350")
win.title("Image Gallery")

def select_file():
    path = filedialog.askopenfilename(title="Select an Image", filetypes=(('image files', '*.jpg'), ('all files', '*.*')))
    img = Image.open(path)
    img = ImageTk.PhotoImage(img)
    label = Label(win, image=img)
    label.image = img  # keep reference
    label.pack()

# Create a label and a Button to Open the dialog
Label(win, text="Click the Button below to select an Image", font=('Caveat 15 bold')).pack(pady=20)
button = ttk.Button(win, text="Select to Open", command=select_file)
button.pack(ipadx=5, pady=15)

win.mainloop()

ผลลัพธ์

โปรแกรมแสดงปุ่มเลือกไฟล์ภาพ และแสดงภาพที่เลือกในหน้าต่าง

วิธีเพิ่มรูปภาพใน Tkinter ด้วย PIL/Pillow

วิธีเพิ่มรูปภาพใน Tkinter ด้วย PIL/Pillow