Python Forum
[Tkinter] Image in Frame in Tabbed Widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Image in Frame in Tabbed Widget
#1
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)
Yoriz write Sep-27-2022, 09:11 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The images are probably being garbage collected, you need to keep a reference to them to stop that happening
Reply
#3
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.
Reply
#4
@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.
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to put background image on Tkinter Frame jenkins43 2 8,747 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 9,003 Sep-30-2019, 07:30 AM
Last Post: Maksim
  [Tkinter] create and insert a new frame on top of another frame atlass218 4 11,116 Apr-18-2019, 05:36 PM
Last Post: atlass218
  how to insert image into Text widget Tkinter atlass218 5 10,013 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] Frame size only works if frame is empty(Solved) Tuck12173 7 6,447 Jan-29-2018, 10:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020