Python Forum
My Background Image Is Not Appearing (Python Tkinter)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My Background Image Is Not Appearing (Python Tkinter)
#1
Hello, Can someone help me in Fixing the code below? I need to put image which is school.jpg as a background image in my Python GUI using Tkinter but apparently, it didn't appear. Only grey color was present in there. Thanks.


from tkinter import *
from PIL import ImageTk, Image


class Student:

    def __init__(self, root):
        # no resize for both directions
        height = 750
        width = 1230
        x = (root.winfo_screenwidth() // 2) - (width // 2)
        y = (root.winfo_screenheight() // 2) - (width // 2)
        self.root = root
        self.root.title("Image")
        self.root.geometry('{}x{}+{}+5'.format(width, height, x, y))
        self.root.config(bg="forestgreen")

        StdID = StringVar()
        Firstname = StringVar()
        Surname = StringVar()
        DoB = StringVar()
        Age = StringVar()
        Gender = StringVar()
        Address = StringVar()
        Mobile = StringVar()



        # Frame
        img = Image.open('images\\school.jpg')
        bg = ImageTk.PhotoImage(img)

        MainFrame = Label(self.root, image = bg)
        MainFrame.place(x=0, y=0)
        MainFrame.grid()


if __name__ == '__main__':

    root = Tk()

    application = Student(root)



    root.mainloop()

Attached Files

Thumbnail(s)
   
Reply
#2
If you use png you can do it that way

from tkinter import Canvas, PhotoImage, Tk

root = Tk()
  
root.geometry("1230x750")
bg = PhotoImage(file = "school.png")
  
canvas1 = Canvas( root)
canvas1.pack(fill = "both", expand = True)

canvas1.create_image( 0, 0, image = bg, 
                     anchor = "nw")

root.mainloop()
Reply
#3
This talks about the file formats supported by PILS

https://pillow.readthedocs.io/en/stable/...rmats.html

I was able to run your program on my Windows computer using Python 3.10 and Pillow 9.2.0.

Are you sure you really want to use a background image? If so, I strongly suggest using a different GUI. Tkinter does not support background images. The only way to do background images in tkinter is to make a canvas, draw the image on the canvas, then make windows on the canvas where you can add buttons and text entries, etc... Not only is this a painful way to write a program (no layout manager, have to do any resizing yourself), it makes for an ugly interface. A probelm with placing controls on a canvas is the background does not show through the controls background. If you make a label, the label background is a solid color, not the image.

Since tkinter doesn't allow background images, which GUI does? I think the answer might be every other GUI. Instead of listing them here, I direct you to this extensive list.

https://wiki.python.org/moin/GuiProgramm...%20rows%20

I use Qt, but for a small project I would strongly consider wxPython or PySimpleGUI.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 661 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,465 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,638 Jul-22-2022, 10:26 AM
Last Post: menator01
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,943 Apr-16-2022, 04:04 PM
Last Post: DBox
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 3,934 Jan-07-2022, 10:10 PM
Last Post: finndude
  How to redraw an existing image to a different image TkInter zazas321 6 5,933 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,662 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 4,089 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 17,827 Feb-06-2021, 03:35 PM
Last Post: joe_momma
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 5,138 Dec-10-2020, 07:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020