简体   繁体   中英

Set image as background of frame tkinter

I am trying to set an image as background of frame.However when I run the code given below, nothing happens. Only a button and label are created.

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent,bg='#a1dbcd')
        im = Image.open('ASL.jpg')
        tkimage = ImageTk.PhotoImage(im)
        myvar=yk.Label(self,image = tkimage)
        myvar.place(relx=0.5, rely=0.735, anchor=CENTER)



        label = tk.Label(self, text="Click Below To start numeral recognition",fg='black',bg='#a1dbcd')
        label.config(width=45)
        label.config(font=("Helvetica", 25))
        label.pack(pady=10,padx=10)
        s = ttk.Style()
        s.configure('my.TButton', font=('Helvetica', 25),fg="#a1adbcd",bg="#ff0000")

        button = tk.Button(self, text="Numeral Recognition",fg='white',bg='#383a39',
                            command=A,height=2,width=50)

        #button.size(height=50,width=50)
        #button.configure(state = "normal", relief="raised", bg = "red")
        button.config(width = 35 )
        button.grid(row=1,column=1)
        button.pack()

You need to save a reference to tkimage somewhere (perhaps as self.tkimage ) to keep it from being garbage collected as soon as the function returns. Merely having the image used by a widget is unfortunately not enough to keep the object alive.

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