How to Create an Image Button in Python Using PIL

Published: 16 January 2023
on channel: 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()


On this page of the site you can watch the video online How to Create an Image Button in Python Using PIL with a duration of hours minute second in good quality, which was uploaded by the user JameSparKing 16 January 2023, share the link with friends and acquaintances, this video has already been watched 565 times on youtube and it was liked by 13 viewers. Enjoy your viewing!