Как вставить картинку в ткинтер
In this Python tutorial, we will learn how to add image in Python Tkinter. let us start the Python Tkinter Image with below examples.
- Python Tkinter Image
- Python Tkinter Image Display
- Python Tkinter Image Button
- Python Tkinter Image Background
- Python Tkinter Image Resize
- Python Tkinter Image Size
- Python Tkinter Image Label
- Python Tkinter Image Doesn’t Exist
Python Tkinter Image
Python Tkinter has the method PhotoImage which allows reading images in Python Tkinter. And then Image can be placed by providing adding PhotoImage variable in image property of widgets like Label, Button, Frame, etc.
- There are three ways of adding images on Python Tkinter.
- PhotoImage
- PillowModule
- In our below example, we have demonstrated the use of PhotoImage. Pillow is discussed in the later sections of the same tutorial. We will be demonstrating the best method to be used in the situation.
Code
This is the basic code to demonstrate how to add images in Python Tkinter. Since the below code is just to display an image so we have used the PhotoImage method in Python Tkinter.
Output
In this output, image is displayed using label widget and since we have not provided any geometry so the application size is to the size of image.

Python Tkinter Image Display
Image in Python Tkinter can be displayed either by using the PhotoImage module or by using the Pillow library.
- In this section, we will display images using both PhotoImage and Pillow libraries. Also, we will use the create_image method from the canvas.
- Canvas is used to add images or text on the application screen.
Code using PhotoImage method
In this code, you can observe that we have not imported any libraries as PhotoImage automatically loaded when everything is imported.
Output of PhotoImage method
The image is displayed on the canvas and it is showing incomplete because we have provided height and width of canvas less than the size of image.

Code using Pillow Library
In this code, we have imported ImageTk, the Image method from the PIL library. PIL is the short name for a pillow. Though the code looks much similar to the previous one now we can read more image extensions like jpg, png, bitmap, etc.
Output Of image displayed using Pillow Library
The output is similar to the PhotoImage section, and photo is incomplete because the size of the window provided is less than the original size of image.

Python Tkinter Image Button
Button widget in Python Tkinter has image property, by providing image variable we can place image on the button widget.
- The first step in the process is to read the image and to do so we will use the PhotoImage method in Python Tkinter.
- Button in Tkinter has image property, you can provide the variable assigned to PhotoImage here.
- In this way an image can be placed on the button widget in Python Tkinter.
In this section, we will learn how to put image on the button in Python Tkinter.
Code
In this code, we have added an image on the button. User can click on the image to perform the action.
Output
In this output, Download image is added on the button. Users can click on this image to download the file.
Python Tkinter Image Background
In this section, we will learn how to insert a background image in Python Tkinter. In other words, how to set background image in Python Tkinter.
- There are mainly two ways of placing background in Python Tkinter
- Using Place layout manager we can put the background image on the label and the stretch it all the way to the screen.
- Using canvas create_image method we can put the image on the screen and later using create_text method of canvas we can place text widget.
Code
In this code, we have imported webbrowser so that we can open the webpage. We have placed image in the background using canvas method create_image and then using create _text and create_window we have placed the button and text on the window.
Output
In this output, you can see that application has background image, text and button. When user will click on the button he/she will be redirected to a website.

Python Tkinter Image Resize
In this section, we will learn how to resize the image in Python Tkinter.
- Using Pillow library in Python Tkinter we can resize the images. to import Pillow use this code from PIL import Image, ImageTk
- image.resize((w, h)) this command allows us to change the height(h) and width(w) of the image.
- In the below example, we have created an application in which the user can provide width and height and the image will change the size in real-time.
Code
In this code, we have used Image method from Pillow module to change the size of the image.
Output
In this output, you can see that image is resizing as per the height and width provided by the user. The is a full fledged application created in Python Tkinter that can be used for daily activities.
Python Tkinter Image Size
In this section, we will learn how to get the image size in Python Tkinter.
- first thing in the process is to import Image, ImageTk from PIL.
- then ImageTk provides width and height method using which we can get the size of an image.
Code
In this code, text=f’width: height: ‘ this command displays the height and width of the image. Here, img is the variable that stores the file file.
Output
In this output, you can see that height and width of the image is displayed at the bottom of the page.

Python Tkinter Image Label
In this section, we will learn how to set image on the Label widget in Python Tkinter.
- The label widget in Python Tkinter is used to display text and images on the application window.
- The label widget has a property image. Adding an image file to this property will set the image on the label widget.
- If you are dealing with PNG images then you can simply use the PhotoImage method which is a built-in method in Python Tkinter.
- For advanced features and all extensions, use the Image, ImageTk in pillow library.
Code
Here is the simple code to display image using Label widget in Python Tkinter.
Output

Python Tkinter Image Doesn’t Exist
This is a common error that is encountered by almost all the programmers working with images in Python Tkinter that says, Python Tkinter Image Doesn’t Exist.
- There are mainly three reasons for this error:
- image path provided is incorrect
- You have read the image using var = Image.open(pathToImage) but you have not passed it to ImageTk.PhotoImage(var) .
- declare a global variable in case you are trying image in function.
You may like the following Python tkinter tutorials:
In this tutorial, we have learned how to add images in Python Tkinter. Also, we have covered these topics.
- Python Tkinter Image
- Python Tkinter Image Display
- Python Tkinter Image Button
- Python Tkinter Image Background
- Python Tkinter Image Resize
- Python Tkinter Image Size
- Python Tkinter Image Label
- Python Tkinter Image Doesn’t Exist

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.
Tkinter PhotoImage – Displaying Images with Tkinter
report this ad
report this ad
report this adThis tutorial explains how to use PhotoImage in Python Tkinter
Tkinter is a very well rounded GUI library with support for various GUI elements. One of the many features that Tkinter supports is the displaying of images within it’s GUI. This is accomplished using the Tkinter PhotoImage Class, which takes an image’s filepath and creates an object from it that can be used with the other GUI elements.
Using Tkinter PhotoImage
The process is quite simple. All you have to do is pass the file path of the Image you want to import, into the PhotoImage class. This creates an image object which Tkinter understands, allowing it to be used with other GUI elements like the Canvas or Labels.
Here is our PhotoImage example code:
We now have our image object stored in the variable image . In the section we’ll explain how to actually display this image now.
If the image you want to display is not in the same folder/directory, then you’ll have to specify the entire filepath, not just the name of the file.
Displaying the Image
As mentioned earlier, we need a suitable GUI element to actually display the image. Surprisingly, most GUI elements do support it in one way or the other. Let’s quickly go through them.
Tkinter Label (Image)
Several GUI elements, such as labels and buttons have the image parameter to which you can assign a suitable image object, just as we have down in the above code.

The output is as shown below.
Keep in mind that Tkinter itself does not have the ability to control (resize) the images it displays. The image displayed above was displayed in it’s original dimensions (size).
Displaying Images on a Canvas
The Tkinter canvas is designed to support features like drawing shapes, images and even displaying images. Hence it has a little more control over the images than the Tkinter Label does.
The first two parameters in the create_image() function represent the width and height of the image respectively. However, as you can see in the image below, that image appears to be cropped. This is because the dimensions we gave the image were too small.

Displaying Images on Buttons
Even though is a tutorial on the Tkinter PhotoImage class, I want to show you some other options as well. In the below code we use a combination of PhotoImage + Pillow (python image library) to display the image in the required format.
We open the Image using Pillow’s open() function, followed by the resize function, which takes a tuple containing the new width and height. Then we converted this image to a Tkinter compatible format using ImageTk.PhotoImage() .
We add the final image to the button the same way we did with the labels, and we get the following output. Personally, I think it looks pretty good. Resizing it was nessacery, otherwise the image would have inflated the size of the button.
This marks the end of the Tkinter PhotoImage tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

report this ad
How to add an image in Tkinter?

There is no «Syntax Error» in the code above — it either ocurred in some other line (the above is not all of your code, as there are no imports, neither the declaration of your path variable) or you got some other error type.
The example above worked fine for me, testing on the interactive interpreter.
Here is an example for Python 3 that you can edit for Python 2 😉


Following code works on my machine
- you probably have something missing in your code.
- please also check the code files’s encoding.
make sure you have PIL package installed
Your actual code may return an error based on the format of the file path points to. That being said, some image formats such as .gif, .pgm (and .png if tk.TkVersion >= 8.6) is already supported by the PhotoImage class.
Below is an example displaying:

or if tk.TkVersion < 8.6 :

It’s not a standard lib of python 2.7. So in order for these to work properly and if you’re using Python 2.7 you should download the PIL library first:
Python Tkinter Image + Examples
In this Python tutorial, we will learn how to add image in Python Tkinter. let us start the Python Tkinter Image with below examples.
- Python Tkinter Image
- Python Tkinter Image Display
- Python Tkinter Image Button
- Python Tkinter Image Background
- Python Tkinter Image Resize
- Python Tkinter Image Size
- Python Tkinter Image Label
- Python Tkinter Image Doesn’t Exist
Table of Contents
Python Tkinter Image
Python Tkinter has the method PhotoImage which allows reading images in Python Tkinter. And then Image can be placed by providing adding PhotoImage variable in image property of widgets like Label, Button, Frame, etc.
- There are three ways of adding images on Python Tkinter.
- PhotoImage
- PillowModule
- In our below example, we have demonstrated the use of PhotoImage. Pillow is discussed in the later sections of the same tutorial. We will be demonstrating the best method to be used in the situation.
Code
This is the basic code to demonstrate how to add images in Python Tkinter. Since the below code is just to display an image so we have used the PhotoImage method in Python Tkinter.
Output
In this output, image is displayed using label widget and since we have not provided any geometry so the application size is to the size of image.

Python Tkinter Image Display
Image in Python Tkinter can be displayed either by using the PhotoImage module or by using the Pillow library.
- In this section, we will display images using both PhotoImage and Pillow libraries. Also, we will use the create_image method from the canvas.
- Canvas is used to add images or text on the application screen.
Code using PhotoImage method
In this code, you can observe that we have not imported any libraries as PhotoImage automatically loaded when everything is imported.
Output of PhotoImage method
The image is displayed on the canvas and it is showing incomplete because we have provided height and width of canvas less than the size of image.

Code using Pillow Library
In this code, we have imported ImageTk, the Image method from the PIL library. PIL is the short name for a pillow. Though the code looks much similar to the previous one now we can read more image extensions like jpg, png, bitmap, etc.
Output Of image displayed using Pillow Library
The output is similar to the PhotoImage section, and photo is incomplete because the size of the window provided is less than the original size of image.

Python Tkinter Image Button
Button widget in Python Tkinter has image property, by providing image variable we can place image on the button widget.
- The first step in the process is to read the image and to do so we will use the PhotoImage method in Python Tkinter.
- Button in Tkinter has image property, you can provide the variable assigned to PhotoImage here.
- In this way an image can be placed on the button widget in Python Tkinter.
In this section, we will learn how to put image on the button in Python Tkinter.
Code
In this code, we have added an image on the button. User can click on the image to perform the action.
Output
In this output, Download image is added on the button. Users can click on this image to download the file.
Python Tkinter Image Background
In this section, we will learn how to insert a background image in Python Tkinter. In other words, how to set background image in Python Tkinter.
- There are mainly two ways of placing background in Python Tkinter
- Using Place layout manager we can put the background image on the label and the stretch it all the way to the screen.
- Using canvas create_image method we can put the image on the screen and later using create_text method of canvas we can place text widget.
Code
In this code, we have imported webbrowser so that we can open the webpage. We have placed image in the background using canvas method create_image and then using create _text and create_window we have placed the button and text on the window.
Output
In this output, you can see that application has background image, text and button. When user will click on the button he/she will be redirected to a website.

Python Tkinter Image Resize
In this section, we will learn how to resize the image in Python Tkinter.
- Using Pillow library in Python Tkinter we can resize the images. to import Pillow use this code from PIL import Image, ImageTk
- image.resize((w, h)) this command allows us to change the height(h) and width(w) of the image.
- In the below example, we have created an application in which the user can provide width and height and the image will change the size in real-time.
Code
In this code, we have used Image method from Pillow module to change the size of the image.
Output
In this output, you can see that image is resizing as per the height and width provided by the user. The is a full fledged application created in Python Tkinter that can be used for daily activities.
Python Tkinter Image Size
In this section, we will learn how to get the image size in Python Tkinter.
- first thing in the process is to import Image, ImageTk from PIL.
- then ImageTk provides width and height method using which we can get the size of an image.
Code
In this code, text=f’width:
height: this command displays the height and width of the image. Here, img is the variable that stores the file file.‘ Output
In this output, you can see that height and width of the image is displayed at the bottom of the page.

Python Tkinter Image Label
In this section, we will learn how to set image on the Label widget in Python Tkinter.
- The label widget in Python Tkinter is used to display text and images on the application window.
- The label widget has a property image. Adding an image file to this property will set the image on the label widget.
- If you are dealing with PNG images then you can simply use the PhotoImage method which is a built-in method in Python Tkinter.
- For advanced features and all extensions, use the Image, ImageTk in pillow library.
Code
Here is the simple code to display image using Label widget in Python Tkinter.
Output

Python Tkinter Image Doesn’t Exist
This is a common error that is encountered by almost all the programmers working with images in Python Tkinter that says, Python Tkinter Image Doesn’t Exist.
- There are mainly three reasons for this error:
- image path provided is incorrect
- You have read the image using var = Image.open(pathToImage) but you have not passed it to ImageTk.PhotoImage(var) .
- declare a global variable in case you are trying image in function.
You may like the following Python tkinter tutorials:
In this tutorial, we have learned how to add images in Python Tkinter. Also, we have covered these topics.
- Python Tkinter Image
- Python Tkinter Image Display
- Python Tkinter Image Button
- Python Tkinter Image Background
- Python Tkinter Image Resize
- Python Tkinter Image Size
- Python Tkinter Image Label
- Python Tkinter Image Doesn’t Exist

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.