简体   繁体   中英

Load background image with Tkinter GUI

I tried all the post available on google related to my issue but still cannot solve. I am trying to load 1 image as background to the form tkinter is generating. With below code , I am getting error as : Traceback (most recent call last): File "", line 89, in AttributeError: 'Label' object has no attribute 'image1'

Below is the code:

   from tkinter import *
    import tkinter as tk
    from PIL import ImageTk, Image


    master = Tk()
    master1= Toplevel()
    master.title("Crop Yield Prediction")
    master.geometry('600x600')
   image2 = Image.open('D:\\Pictures\\god0a.jpg')
   image1 = ImageTk.PhotoImage(image2)
   background_label = tk.Label(master1, image=image1)
   background_label.image1
   background_label.photo=background
   background_label.place(x=0, y=0, relwidth=1, relheight=1)

Few changes to code and it worked!!

image2 = Image.open('D:\\Pictures\\god0a.jpg')
#image2.show()
image1 = ImageTk.PhotoImage(image2)
background_label = tk.Label(master, image=image1)
background_label.image1=image1
background_label.place(x=0, y=0, height=250, width=350)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM