Python Forum
My Background Image Is Not Appearing (Python Tkinter) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: My Background Image Is Not Appearing (Python Tkinter) (/thread-39602.html)



My Background Image Is Not Appearing (Python Tkinter) - HailyMary - Mar-14-2023

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()



RE: My Background Image Is Not Appearing (Python Tkinter) - Axel_Erfurt - Mar-14-2023

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()



RE: My Background Image Is Not Appearing (Python Tkinter) - deanhystad - Mar-14-2023

This talks about the file formats supported by PILS

https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.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/GuiProgramming#:~:text=Cross-Platform%20Frameworks%20%20%20%20Package%20%20,cross-platform%20applica%20...%20%2017%20more%20rows%20

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