0
import tkinter
from tkinter import ttk
from PIL import Image, ImageTk

def decrypte():
    style = ttk.Style()
    style.theme_use("alt")

    style.configure("green.Horizontal.TProgressbar", foreground='green', background='green')
    decrypte_window = tkinter.Toplevel(root)
    decrypte_window.title("Decrypt your files")
    decrypte_window.geometry("900x700")
    area_decrypte =  tkinter.Frame(decrypte_window, bg="black", width=900, height=400)
    area_decrypte.pack()
    area_decrypte.pack_propagate(False)
    img_hdd = ImageTk.PhotoImage(Image.open('hdd.png'))
    tkinter.Label(area_decrypte, image=img_hdd, width=100, height=100).pack()

def block_all():
    global root
    global background
    global label
    decrypt = tkinter.Button(background, text="Decrypt", width=14, height=2, command=decrypte, bg="#0078FF", foreground="#fff").pack(side="right", anchor="se", pady=30, padx=60)
    root.mainloop()


root = tkinter.Tk()
root.geometry("1100x900")
background = None
block_all()

I want to display an image at the top level, but I noticed that my image is not displayed, the path is correct. I realized that the problem is at the top level, how can I display an image at the top level with tkinter?

Jason Yang
  • 11,284
  • 2
  • 9
  • 23
Dkns
  • 45
  • 2
  • 10
  • 1
    Local variable `img_hdd` will not exist after function `decrypte` called, so image not shown. Maybe add one statement `global img_hdd` in , then it should be OK. – Jason Yang Nov 17 '21 at 10:08
  • Function `pack` of widget return `None`, so don't use statement like `label = tkinter.Label(area_decrypte, image=img_hdd, width=100, height=100).pack()`, the value of `label` will be `None`. – Jason Yang Nov 17 '21 at 10:13
  • it is common problem and you can see many questions for this: there is `bug` in `PhotoImage` which removes image from memory when it is assigned to local variable in function - and you see empty image. You have to assign to `global` variable or to some variable in other object - ie. common solution `label.image = img` See `Note` at page [PhotoImage](https://web.archive.org/web/20201112023229/http://effbot.org/tkinterbook/photoimage.htm) – furas Nov 17 '21 at 10:32

0 Answers0