How to Create an Image Button in Python Using PIL

Publicado el: 16 enero 2023
en el 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()


En esta página del sitio puede ver el video en línea How to Create an Image Button in Python Using PIL de Duración hora minuto segunda en buena calidad , que subió el usuario JameSparKing 16 enero 2023, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 565 veces y le gustó 13 a los espectadores. Disfruta viendo!