Search code examples
pythonimagetkinterpngtoplevel

My Tkinter photo is packed, but it shows as a blank


I can't seem to get the picture to show up on screen while having a toplevel frame on top of my main one (root). This one is just called "frame". I have circled the outline of the tkinter Frame on the included photo in this post. When I resize the picture, the green frame outline changes, but the picture itself won't show up.

I also tried to pack it on my main root window, with success, which suggests it's a toplevel window issue. I just don't know what it is. Any ideas?

Here is my code:

    def show_topLevelWindow():

      from tkinter import ttk
      print("Entered to show results")
      window_linkedin = Toplevel(root)
      window_linkedin.geometry('1000x590')

      frame = Frame(window_linkedin)
      frame.pack()

      error_frame = tkinter.Frame(frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)

      error_label = Label(frame, text="It appears there are no results for the selected country")
      error_label.config(font=("Helvetica Neue", 20))        

      im_error = Image.open("./ressources/images_gui/aw_snap.png")
      im_error = im_error.resize((500, 500), Image.ANTIALIAS)
      im_error = ImageTk.PhotoImage(file = "./ressources/images_gui/aw_snap.png")
      im_error_label = Label(frame, image=im_error)

      try:
          if ....:
          ....unimportant code ....
          else:
              error_label.pack(in_=error_frame)            
              im_error_label.pack(in_=error_frame)  
              error_frame.pack(anchor="center")          
      except Exception as e:
          error_label.pack(in_=error_frame)            
          im_error_label.pack(in_=error_frame)            
          error_frame.pack(anchor="center")

Packed image shows as blank


Solution

  • The single most important issue you are having is your image is not being saved for reference. if you add global im_error to the very top of your function your image will be visible.

    That said there are some issues with your code you should correct.

    First: Do not import in a function. Instead write all your imports at the top of your code.

    Second: I am not sure why you are doing .pack(in_=error_frame). This is not something one would ever really need. Just make sure that your label is already assigned to the correct frame. The in_ argument is rarely used and probably most people never use it. I have been on here for two years now and this is the first time I have seen anyone use that argument.

    Third: You have not shown your imports for Tkinter however based on how you have written you code it looks like you have done:

    import tkinter
    from tkinter import *
    

    This is overkill and is not a good idea. Just do import tkinter as tk and make sure you use tk. prefix where it applies.

    Here is your code remade:

    import tkinter.ttk as ttk
    import tkinter as tk
    from PIL import ImageTk, Image
    
    
    
    def show_toplevel_window():
        global im_error
        window_linkedin = tk.Toplevel(root)
        window_linkedin.geometry('1000x590')
    
        frame = tk.Frame(window_linkedin)
        frame.pack()
    
        error_frame = tk.Frame(frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)
        error_frame.pack()
    
        error_label = tk.Label(frame, font=("Helvetica Neue", 20), text="It appears there are no results for the selected country")
        error_label.pack()  
    
        im_error = Image.open("./ressources/images_gui/aw_snap.png")
        im_error = im_error.resize((500, 500), Image.ANTIALIAS)
        im_error = ImageTk.PhotoImage(file = "./ressources/images_gui/aw_snap.png")
        im_error_label = tk.Label(error_frame, image=im_error)
        im_error_label.pack()
    
    root = tk.Tk()
    show_toplevel_window()
    root.mainloop()