In this tutorial, we will cover how to use icons, images, and exit buttons in a Python Tkinter GUI application. Icons and images can be a great way to add visual interest to your application, while exit buttons are essential for providing a way for users to easily close the application.
To get started, make sure you have Python installed on your computer. You can download it from the official Python website if you don’t already have it installed.
Next, you’ll need to install the Tkinter library, which comes pre-installed with Python. You can check if you have it installed by opening a terminal and running the following command:
python -m tkinter
If Tkinter is installed, a blank window should open. If not, you can install Tkinter using pip:
pip install tk
Now that you have Tkinter installed, let’s create a new Python script for our GUI application. Open your favorite code editor and create a new file called gui.py
.
First, we’ll import the necessary modules:
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
Next, let’s create a basic GUI window with an icon:
root = tk.Tk()
root.title("My GUI Application")
root.iconphoto(True, tk.PhotoImage(file='icon.png'))
In this code snippet, we create a new Tkinter window with the title "My GUI Application" and set an icon using the iconphoto
method. Make sure to replace 'icon.png'
with the path to your own icon file.
Next, let’s add an image to our GUI:
image = Image.open("image.jpg")
photo = ImageTk.PhotoImage(image)
label = tk.Label(root, image=photo)
label.pack()
In this code snippet, we open an image file called image.jpg
using the Image
class from the PIL
(Python Imaging Library) module. We then convert the image to a Tkinter-compatible format using the ImageTk
class and create a Label
widget to display the image in the GUI window.
Finally, let’s add an exit button to our GUI:
def on_exit():
root.destroy()
exit_button = ttk.Button(root, text="Exit", command=on_exit)
exit_button.pack()
In this code snippet, we define a function called on_exit
that calls the destroy
method on the root window to close the application. We then create a Button
widget labeled "Exit" that calls the on_exit
function when clicked.
That’s it! You now have a basic GUI application with an icon, an image, and an exit button. You can customize the appearance and functionality of your GUI further by adding more widgets, changing styles, and handling user interactions.
I hope you found this tutorial helpful. Happy coding!
▶️ Watch Entire Tkinter Playlist ✅ Subscribe To My YouTube Channel:
http://bit.ly/2UFLKgj http://bit.ly/2IGzvOR
▶️ See More At: ✅ Join My Facebook Group:
https://Codemy.com http://bit.ly/2GFmOBz
▶️ Learn to Code at https://Codemy.com ✅ Buy a Codemy T-Shirt!
Take $30 off with coupon code: youtube1 http://bit.ly/2VC9WUN
very very nice❣❣❣
Any 16-byte favicon I use or create seems to produce a blank icon. Not a showstopper for my learning but I'm curious if anyone else is experiencing this. Is the codemy.ico file that John is using available anywhere? The videos are awesome by the way. I need to learn this stuff and the tutorials are at the perfect pace for me.
Hello so , I can't get my icon to display on the window
thanks a lot man!
Bonjour, comment faire apparaitre une simple image dans une fenetre avec tkinter ? Mon programme oriente l'utilisateur vers des véhicules différents j'aimerais faire apparaitre une image de véhicule prédéfini dans une fenetre en fonction du choix de l'utilisateur
Great Tutorial! I'm watching this 5 years later, but there's a function called Eval() in python. You could use this to make it a scientific calculator and make the evaluation function just evaluate the string.
im gonna be honest mr heisenberg you look extremely cute with the doggo <3
also thanks for the videos
The iconbitmap() method seems to be a Windows only thing. For those using Linux (and possible MacOS – I don't have access to a Mac to verify this), another way of setting the icon must be taken. It took some effort to figure this out, so I'll share it here. This is the simplest way of doing it, there are more complex ways for multiple-size icons, etc.
First, the .ico file format can't be used, so the image should be a PNG file.
Then, the following code must be used, assuming that (as we do here), the root window is in a variable called root.
root.iconphoto(False, tk.PhotoImage(file='<file_name>')
The file name can be just the image file name if it is in the current working directory during script execution or any absolute or relative path.
Not all GUI environments in Linux have the window icon in the top left corner, but this also sets the icon used in the task bar.
Great series, thank you very much. What I do find irritating about these software packages, IDEs etc is this trend to add plugins and packages afterwards. Never complete. It's like buying a car and discovering you still need a spare wheel, side view mirrors… Oh, do you need an engine too, sir ? No problem, just download it ! 🙂
button_quit is not working properly, application getting hanged
icon = tki.PhotoImage(file='/home/gary/Documents/msql-work/Image-viewer.png')
root.iconphoto(True, icon) # Linux version
vsiconbitmap in M$I found this to work on Linux if anyone has trouble with the icon.ico. Use a .gif instead.
# Adding icon image in Linux
img = PhotoImage(file="images/icon.gif")
root.tk.call('wm', 'iconphoto', root._w, img)
Thank you
When I use the code on it's own it pops up in front of my code. But when I add it to an if statement it pops up in the background. Why is that and how can I make the image pop up in front of my code?
Why he didnt add root under the img label?
The coupon code YOUTUBE does not work
hi sir, at 2:59, when I run the program, it doesnt show the chosen icon on the left top, it just showed the white file icon. i do not know why it happened. Can you help me about it please. Thank you
Thank you so much for the tutorial
I started learning obsidian and emacs for 2 weeks and python about 3 days from now i had a very hard time setting up things but i made it work exept <iconbitmap> no matter what i ve done i get an error that shows the path nd if i remember it can't open file and bitmap stuff…
I went with many solutions i added @ to path by the way im using deb12 i used .png instead of .ico and other things
Should i try this? Is it same ?
import tkinter as tk
root = tk.Tk()
root.title("Window with PNG Icon")
# Load the PNG image
icon_image = tk.PhotoImage(file="path_to_image.png") # Replace with your image path
# Set the image as the window icon
root.tk.call('wm', 'iconphoto', root._w, icon_image)
root.mainloop()
“`
I hope i can find a solution here
am using grid system, if i click the exit button the gui is not automatically closing can u hel me?