
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
- Related Questions & Answers
- How to use an image as a link in HTML?
- Add image on a Python Tkinter button
- How to use an image for the background in tkinter?
- How to use Bitmap images in Button in Tkinter?
- How to add an image in Tkinter?
- How to have image and text in one button in Tkinter?
- How to update an image in a Tkinter Canvas?
- How to Use Images as Backgrounds in Tkinter?
- How to add a button to an image with CSS?
- How to add an image to a button (action) in JavaFX?
- How to resize an image using Tkinter?
- How to use an image in a webpage?
- How to insert an image in a Tkinter canvas item?
- How to place an image into a frame in Tkinter?
- How to specify an image as a client-side image-map in HTML?