How to use an image for the background in tkinter?
This code doesn’t work. I want to import a background image.
![]()
3 Answers 3
One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.
You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.
Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:
Как сделать фон в python
How to Set Background to be an Image in Python Tkinter
In this Python Tkinter tutorial, we will learn how to set background to be an image in Python Tkinter.
Set Background to be an Image in Python Tkinter
- There are more than one ways to add background images but one this is common in all of them that we use the Label widget to set the background.
- The simplest way to do this is to add a background image using PhotoImage() and place other widgets using Place geometry manager.
- Place geometry manager allows users to put the widget anywhere on the screen by providing x & y coordinates. You can even overlap the widgets by providing the same coordinates.
- PhotoImage() takes a file path as an argument and can be used later in code to display images. P hotoImage(file=’image_path.png’)
- The Only drawback of using PhotoImage is it works only with png images.
- In case you want to use other formats like jpeg in that case you can use pillow library
- use command: pip install pillow to install this library
Code:
In this code, we have added a background image to the Python application. Other widgets like Text and button widgets are placed on the background image.
Output:
This is simple a dummy of email sending mechanism but it has a beautiful background image.

You may like the following Python Tkinter tutroials:
In this tutorial, we have learned how to set background to be an image in Python Tkinter

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.
Как использовать изображение для фона в tkinter?
этот код не работает. 🙁 Я хочу импортировать фоновое изображение.
2 ответов
один простой метод заключается в использовании place чтобы использовать изображение в качестве фонового изображения. Это то, что place действительно хорошо делать.
можно тут grid или pack другие виджеты в Родительском, как обычно. Просто убедитесь, что вы сначала создаете метку фона, чтобы она имела более низкий порядок укладки.
Примечание: Если вы делаете это внутри функции, убедитесь, что вы храните ссылку на изображение, иначе изображение будет уничтожен сборщиком мусора, когда функция вернется. Распространенным методом является добавление ссылки в качестве атрибута объекта label: