How to use an Image as a button in Tkinter?


In this example, we will create a rounded button in a window that can be used in many other applications like forms, games, dialogue boxes, etc.

The best way to create rounded buttons in Tkinter is to use the desired images of buttons and turn it into a clickable button in the frame. That is really possible by using PhotoImage() function which grabs the desired image of the button.

So, the following steps make the desired image a button,

  • First, we will create a dummy button which can be used to make the image clickable.

  • Grab the image from the source using PhotoImage(file) function.

  • Pass the image file as the value in Button function

  • Remove the borderwidth=0.

  • Now, we got the button rounded.

For this example we will use this image and will make it clickable.

#Import all the necessary libraries
from tkinter import *

#Define the tkinter instance
win= Toplevel()
win.title("Rounded Button")

#Define the size of the tkinter frame
win.geometry("700x300")

#Define the working of the button

def my_command():
   text.config(text= "You have clicked Me...")

#Import the image using PhotoImage function
click_btn= PhotoImage(file='clickme.png')

#Let us create a label for button event
img_label= Label(image=click_btn)

#Let us create a dummy button and pass the image
button= Button(win, image=click_btn,command= my_command,
borderwidth=0)
button.pack(pady=30)

text= Label(win, text= "")
text.pack(pady=30)

win.mainloop()

Output

Running the above code will produce the following output −

Updated on: 15-Sep-2023

26K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements