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


Messages In This Thread
My Background Image Is Not Appearing (Python Tkinter) - by HailyMary - Mar-14-2023, 01:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 914 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,601 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,709 Jul-22-2022, 10:26 AM
Last Post: menator01
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 3,025 Apr-16-2022, 04:04 PM
Last Post: DBox
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 4,040 Jan-07-2022, 10:10 PM
Last Post: finndude
  How to redraw an existing image to a different image TkInter zazas321 6 6,062 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,741 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 4,162 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 18,012 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,219 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