0

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

1 Answer 1

1

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.

2
  • now the label superimposes the image.How can i show that it is part of image?
    – Rock
    Commented May 25, 2017 at 17:17
  • @Rock: You would need to use Canvas.create_window() to make a "window" object to hold the Button widget.
    – martineau
    Commented Jan 18, 2019 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.