thatguy_001
thatguy_001

Reputation: 74

Adding background image to a new window in Tkinter

def func_checkprice():
    try:
        global product_link
        
        product_link = link.get()
        page =requests.get(product_link)
        soup = BeautifulSoup(page.content, 'html.parser')
        product_name = soup.find(class_='_35KyD6').get_text()
        price = soup.find(class_='_1vC4OE _3qQ9m1').get_text()
        print(product_name ,price, ctime())
        product_window = Tk()
        product_window.geometry("200x200")
        back_img1 = ImageTk.PhotoImage(Image.open("388275.jpg"))
        back_label1 = Label(product_window, image=back_img1)
        back_label1.place(x=0, y=0, relheight=1, relwidth=1)
        details_name = Label(product_window , text=product_name, wraplength=120)
        details_price = Label(product_window , text=price, wraplength=120)
        details_name.grid(row=0,column=0)
        details_price.grid(row=0,column=1)
        okay_button = Button(product_window, text="Okay", command=product_window.destroy)
        okay_button.grid(row=1, column=0, columnspan=2, ipadx=10, pady=5)
        link.delete(0,END)
    except:
        popup = messagebox.showerror("Error","Invalid Link") 

The background code works fine for the root window. This product_window opens when I click on a button on the root window. I just can't add the background to the new window tried everything I possibly could.

Upvotes: 0

Views: 358

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

Either say global back_img1 on top of your function to keep a reference or you can also say back_label1.image=back_img1. This will keep the image from being garbage collected by pythons garbage collector.

And i assume your product_window the first window(parent) in your program, as your not allowed to use more than one instance of Tk(), if its not the main window of your program then replace Tk() with Toplevel() as those are used for referencing child windows to the parent Tk() window. If there is more than one instance of Tk() youll receive an error, that pyimagex does not exist

Hope it cleared your doubts.

Cheers

Upvotes: 1

Related Questions