Python Forum
[Tkinter] Image in Frame in Tabbed Widget - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Image in Frame in Tabbed Widget (/thread-38316.html)



Image in Frame in Tabbed Widget - Columbo - Sep-27-2022

I have created a tabbed widget with 2 tabs and I put a frame in each tab. I want to place 2 images, one in each frame, but the image is not being displayed. I can get text into each frame but not an image. There are no errors being flagged and the tabbed widget is being displayed properly. What am I missing here?

Here is the code:
def tabbedPics():
   Notebook = ttk.Notebook(root, width = 317, height = 230)

    dinoFrame = ttk.Frame(Notebook)
    Notebook.add(dinoFrame, text = "Dinosaur")
    sizeFrame = ttk.Frame(Notebook)
    Notebook.add(sizeFrame, text = "Size Comparison")
    Notebook.place(x=900, y=220)

    dinoImg = ImageTk.PhotoImage(Image.open("dino_images/Anomalocaris.gif"))
    dinoLabel = Label(dinoFrame, image = dinoImg)
    dinoLabel.place(x=0, y=0)

    sizeImg = ImageTk.PhotoImage(Image.open("dinosize/Anomalocaris_size.gif"))
    sizeLabel = Label(sizeFrame, image = sizeImg)
    sizeLabel.place(x=0, y=0)



RE: Image in Frame in Tabbed Widget - Yoriz - Sep-27-2022

The images are probably being garbage collected, you need to keep a reference to them to stop that happening


RE: Image in Frame in Tabbed Widget - deanhystad - Sep-27-2022

You've been around the forum long enough that you know better than posting code without using Python tags.

When posting code, you should post code that other people can run. It makes it easier for them to see the problem you are having. I modified the code like this:
from tkinter import *  # Wildcard imports are bad.  They clog the namespace.
from tkinter.ttk import *  # But if you wildcard import tkinter, also do it for ttk
from PIL import ImageTk, Image

def tabbedPics():
    book = Notebook(root)  # Variable names should be lower case and should not be function or class names.
    book.pack()

    dino_frame = Frame(book)
    book.add(dinoFrame, text="Dinosaur")  # Convention is to use snake_case instead of camelCase.
    sizeFrame = Frame(book)   # Can go against convention if good reason.
    book.add(sizeFrame, text="Size Comparison")

    dinoImg = ImageTk.PhotoImage(Image.open("dice1.png"))  # I used some image files I have laying around
    dinoLabel = Label(dinoFrame, image=dinoImg)
    dinoLabel.pack()

    sizeImg = ImageTk.PhotoImage(Image.open("dice2.png"))
    sizeLabel = Label(sizeFrame, image=sizeImg)
    sizeLabel.pack()  # Only designer programs use place.  Use pack() or grid()

root = Tk()  # Added this code to make a a window to display the notebook.
tabbedPics()
root.mainloop()
The program does not display any images when run.

Google "tkinter image not showing" and you will learn the answer. It has to do with variables, functions and scope.


RE: Image in Frame in Tabbed Widget - Columbo - Sep-28-2022

@deanhystad

When I post code I clicked on the menu button that says "Insert code". I assumed that was what was required. I know other forums use the
  
but given there was a button I thought that was what this forum used.


RE: Image in Frame in Tabbed Widget - deanhystad - Sep-28-2022

Use the blue and yellow "Insert python" button to post code. Use the "Insert error" button to post error message and traces. Use the "Insert output" button to post output from your program.

Always use the "Preview Post" button before submitting.

The Insert Code Snippet doesn't appear to do anything.