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
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.
from tkinter import * ws = Tk() ws.title('PythonGuides') ws.geometry('500x300') ws.config(bg='yellow') img = PhotoImage(file="python-tkinter-background-image.png") label = Label( ws, image=img ) label.place(x=0, y=0) text = Text( ws, height=10, width=53 ) text.place(x=30, y=50) button = Button( ws, text='SEND', relief=RAISED, font=('Arial Bold', 18) ) button.place(x=190, y=250) ws.mainloop()
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
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.
Как сделать фон питон
- Python | Creating a button in tkinter
- Python | Add style to tkinter button
- Python | Add image on a Tkinter button
- Python Tkinter – Label
- Python Tkinter | Create LabelFrame and add widgets to it
- RadioButton in Tkinter | Python
- Python Tkinter – Checkbutton Widget
- Python Tkinter – Canvas Widget
- Python Tkinter | Create different shapes using Canvas class
- Python Tkinter | Create different type of lines using Canvas class
- Python Tkinter | Moving objects using Canvas.move() method
- Combobox Widget in tkinter | Python
- maxsize() method in Tkinter | Python
- minsize() method in Tkinter | Python
- resizable() method in Tkinter | Python
- Python Tkinter – Entry Widget
- Tkinter – Read only Entry Widget
- Python Tkinter – Text Widget
- Python Tkinter – Message
- Python | Menu widget in Tkinter
- Python Tkinter – Menubutton Widget
- Python Tkinter – SpinBox
- Progressbar widget in Tkinter | Python
- Python-Tkinter Scrollbar
- Python Tkinter – ScrolledText Widget
- Python Tkinter – ListBox Widget
- Scrollable ListBox in Python-tkinter
- Python Tkinter – Frame Widget
- Scrollable Frames in Tkinter
- How to make a proper double scrollbar frame in Tkinter
- Python Tkinter – Scale Widget
- Hierarchical treeview in Python GUI application
- Python-Tkinter Treeview scrollbar
- Python Tkinter – Toplevel Widget
- Python | askopenfile() function in Tkinter
- Python | asksaveasfile() function in Tkinter
- Python – Tkinter askquestion Dialog
- Python Tkinter – MessageBox Widget
- Create a Yes/No Message Box in Python using tkinter
- Change the size of MessageBox – Tkinter
- Different messages in Tkinter | Python
- Change Icon for Tkinter MessageBox
- Python – Tkinter Choose color Dialog
- Popup Menu in Tkinter
- Getting screen’s height and width using Tkinter | Python
- Python | How to dynamically change text of Checkbutton
- Python | focus_set() and focus_get() method
- Search String in Text using Python-Tkinter
- Autocomplete ComboBox in Python-Tkinter
- Autohiding Scrollbars using Python-tkinter
- Python Tkinter – Validating Entry Widget
- Tracing Tkinter variables in Python
- Python | setting and retrieving values of Tkinter variable
- Tkinter | Adding style to the input text using ttk.Entry widget
- Python | after method in Tkinter
- destroy() method in Tkinter | Python
- Text detection using Python
- Python | winfo_ismapped() and winfo_exists() in Tkinter
- Collapsible Pane in Tkinter | Python
- Creating a multiple Selection using Tkinter
- Creating Tabbed Widget With Python-Tkinter
- Open a new Window with a button in Python-Tkinter
- Cryptography GUI using python
- Python | Simple GUI calculator using Tkinter
- Create Table Using Tkinter
- Python | GUI Calendar using Tkinter
- File Explorer in Python using Tkinter
- Python | ToDo GUI Application using Tkinter
- Python: Weight Conversion GUI using Tkinter
- Python: Age Calculator using Tkinter
- Python | Create a GUI Marksheet using Tkinter
- Python | Loan calculator using Tkinter
- Python | Create a digital clock using Tkinter
- Make Notepad using Tkinter
- Color game using Tkinter in Python
- Python | Simple FLAMES game using Tkinter
- Simple registration form using Python Tkinter
- How to create a COVID19 Data Representation GUI?
- Python | Creating a button in tkinter
- Python | Add style to tkinter button
- Python | Add image on a Tkinter button
- Python Tkinter – Label
- Python Tkinter | Create LabelFrame and add widgets to it
- RadioButton in Tkinter | Python
- Python Tkinter – Checkbutton Widget
- Python Tkinter – Canvas Widget
- Python Tkinter | Create different shapes using Canvas class
- Python Tkinter | Create different type of lines using Canvas class
- Python Tkinter | Moving objects using Canvas.move() method
- Combobox Widget in tkinter | Python
- maxsize() method in Tkinter | Python
- minsize() method in Tkinter | Python
- resizable() method in Tkinter | Python
- Python Tkinter – Entry Widget
- Tkinter – Read only Entry Widget
- Python Tkinter – Text Widget
- Python Tkinter – Message
- Python | Menu widget in Tkinter
- Python Tkinter – Menubutton Widget
- Python Tkinter – SpinBox
- Progressbar widget in Tkinter | Python
- Python-Tkinter Scrollbar
- Python Tkinter – ScrolledText Widget
- Python Tkinter – ListBox Widget
- Scrollable ListBox in Python-tkinter
- Python Tkinter – Frame Widget
- Scrollable Frames in Tkinter
- How to make a proper double scrollbar frame in Tkinter
- Python Tkinter – Scale Widget
- Hierarchical treeview in Python GUI application
- Python-Tkinter Treeview scrollbar
- Python Tkinter – Toplevel Widget
- Python | askopenfile() function in Tkinter
- Python | asksaveasfile() function in Tkinter
- Python – Tkinter askquestion Dialog
- Python Tkinter – MessageBox Widget
- Create a Yes/No Message Box in Python using tkinter
- Change the size of MessageBox – Tkinter
- Different messages in Tkinter | Python
- Change Icon for Tkinter MessageBox
- Python – Tkinter Choose color Dialog
- Popup Menu in Tkinter
- Getting screen’s height and width using Tkinter | Python
- Python | How to dynamically change text of Checkbutton
- Python | focus_set() and focus_get() method
- Search String in Text using Python-Tkinter
- Autocomplete ComboBox in Python-Tkinter
- Autohiding Scrollbars using Python-tkinter
- Python Tkinter – Validating Entry Widget
- Tracing Tkinter variables in Python
- Python | setting and retrieving values of Tkinter variable
- Tkinter | Adding style to the input text using ttk.Entry widget
- Python | after method in Tkinter
- destroy() method in Tkinter | Python
- Text detection using Python
- Python | winfo_ismapped() and winfo_exists() in Tkinter
- Collapsible Pane in Tkinter | Python
- Creating a multiple Selection using Tkinter
- Creating Tabbed Widget With Python-Tkinter
- Open a new Window with a button in Python-Tkinter
- Cryptography GUI using python
- Python | Simple GUI calculator using Tkinter
- Create Table Using Tkinter
- Python | GUI Calendar using Tkinter
- File Explorer in Python using Tkinter
- Python | ToDo GUI Application using Tkinter
- Python: Weight Conversion GUI using Tkinter
- Python: Age Calculator using Tkinter
- Python | Create a GUI Marksheet using Tkinter
- Python | Loan calculator using Tkinter
- Python | Create a digital clock using Tkinter
- Make Notepad using Tkinter
- Color game using Tkinter in Python
- Python | Simple FLAMES game using Tkinter
- Simple registration form using Python Tkinter
- How to create a COVID19 Data Representation GUI?
How to add background image in Python Tkinter
In this post, you will learn how to add a background image in Python Tkinter.
Tkinter is a standard cross-platform package for creating graphical user interfaces (GUIs). It is also called the Tk interface. It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python. The greatest strength of Tkinter is its simplicity. There are different ways to add a background image. But always, a label widget is used to set the background.
Add a background image using PhotoImage()
The PhotoImage class is used to display images in labels, buttons, canvases, and text widgets, which are present in the tkinter package. This function takes a file path as an argument and returns the image object. We can place the geometry manager that allows users to put the widget anywhere on the screen by providing x & y coordinates. The following code demonstrates this-
from tkinter import * from PIL import ImageTk, Image app = Tk() app.title("Welcome") img =Image.open('C:\\Python37\\scripts\\projects\\kittens.jpg') bg = ImageTk.PhotoImage(img) app.geometry("650x450") # Add image label = Label(app, image=bg) label.place(x = 0,y = 0) # Add text label2 = Label(app, text = "Hello kittens", font=("Times New Roman", 24)) label2.pack(pady = 50) # Execute tkinter app.mainloop()
Output of the above code:
Add a background image using create_image()
In the given example, we have added a background image using the create_image() method in the canvas widget. The Canvas is a rectangular area planned for drawing pictures or other complex layouts. We can put graphics, text, widgets, or frames on a Canvas. The create_image() method of Canvas is used to draw an image on a canvas. The create_image() function doesn’t accept an image directly. It uses an object that is created by the PhotoImage() method.
The process of adding a background image in Python Tkinter using the Canvas is almost the same as the above. First, we added the image file, then created a canvas and set the width and height. Then we have displayed the image using the create_image() function and set the text using the create_text() function.
#Import the required library from tkinter import * from PIL import Image, ImageTk from tkinter import ttk # Create object root = Tk() # Define the geometry of the window root.geometry("500x450") # Add the image file bg = ImageTk.PhotoImage(file="C:\\Python37\\scripts\\projects\\natural_img1.png") # Create a canvas canvas = Canvas(root,width= 400, height= 300) canvas.pack(fill= "both", expand=True) # Display image canvas.create_image(0, 0, image=bg, anchor="nw") # Add a text in canvas canvas.create_text(250,250,text="Good Morning!", font=("Times New Roman", 24)) # Execute tkinter root.mainloop()
Output of the above code: