How to Create an Image Button in Python Using PIL

Publicado em: 16 Janeiro 2023
no canal de: JameSparKing
565
13

It's very simple! In this tutorial, I walk through creating image buttons in python using Tkinter and PIL. You can make any image into a functionally button. You can also resize the image and place it anywhere you like.

I hope you guys find this tutorial useful. Feel free to like, comment, subscribe, and share this video. Thank you so much!

Steps:
1. Import all necessary libraries
2. Get an image you want to turn into a button
3. Put the image in the folder
4. Open image using Image.open()
5. Resize the image
6. Get the image using ImageTk.PhotoImage()
7. Configure the button and the command
8. Place the button

Code used in this video:

from tkinter import *
import tkinter as tk
import tkinter.font as font
from PIL import Image, ImageTk

def open_start_window():
start_window = Tk()
start_window.title('start window')
start_window.geometry('400x300')

def gallery_page():
print("Subscribe to JameSparKing")

def exit_window():
start_window.destroy()

gallery_button = tk.Button(text='Open Gallery', bg='blue', fg='white', command=gallery_page)
font_style = font.Font(family='Arial', size=15, weight='bold')
gallery_button['font'] = font_style
gallery_button.config(height=5, width=20)
gallery_button.pack())

gallery_button = Image.open("./JameSparKing_pic.jpg")
resize_gallery_button = gallery_button.resize((150,150))
gallery_button_picture = ImageTk.PhotoImage(resize_gallery_button)
image_gallery_button = Button(image=gallery_button_picture, command=gallery_page)
image_gallery_button.place(x=125, y=25)

exit_button = tk.Button(text='Exit', command=exit_window)
exit_button.place(x=180, y=200)

start_window.mainloop()

open_start_window()


Nesta página do site você pode assistir ao vídeo on-line How to Create an Image Button in Python Using PIL duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário JameSparKing 16 Janeiro 2023, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 565 vezes e gostou 13 espectadores. Boa visualização!