
- 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 have image and text in one button in Tkinter?
We can load the images in a Tkinter application using the PhotoImage(image location) function, which takes the image location as the parameter and displays the image on the window object. However, when we try to add an image to the button, it generally appears on the button while hiding the button text. Therefore, to make the button text and pictures relative to each other, we typically use the compound property. It takes one of four positional arguments – LEFT, RIGHT, TOP, and BOTTOM, each defining the image’s position on the button.
Example
In this example, we have used this image to make it relative to the button.
#Import tkinter library from tkinter import * from PIL import Image,ImageTk #Create an instance of Tkinter frame or window win= Tk() #Set the geometry of tkinter frame win.geometry("750x250") #Define a function to close the window def close_win(): win.destroy() #Load the image image = Image.open('preview.png') #Resize the Image image = image.resize((50,50), Image.ANTIALIAS) #Convert the image to PhotoImage img= ImageTk.PhotoImage(image) #Create a Label Label(win, text="Click the below button to close the window",font=('Aerial 15 bold')).pack(pady=20) #Create a label with the image button= Button(win, text="Click Me",font= ('Helvetica 15 bold'),image=img, compound= LEFT, command=close_win) button.pack() win.mainloop()
Output
The above code will display a window containing a button with an image and a text label. When we click the button, it will close the window.
- Related Questions & Answers
- How To Dynamically Resize Button Text in Tkinter?
- How to use an Image as a button in Tkinter?
- How to change Tkinter label text on button press?
- Add image on a Python Tkinter button
- How do I print and have user input in a text box in Tkinter?
- Get the text of a button widget in Tkinter
- How to force Tkinter text widget to stay on one line?
- How to word-wrap text in Tkinter Text?
- Change the focus from one Text widget to another in Tkinter
- How to add an image in Tkinter?
- How to highlight text in a tkinter Text widget?
- How to Disable / Enable a Button in TKinter?
- How to update a Button widget in Tkinter?
- How to highlight a tkinter button in macOS?
- How to use Bitmap images in Button in Tkinter?