Reputation: 1
In some earlier code, I've been able to create a single frame and display an image in python using tkinter, but now I'm trying to write some code that will first ask for a participant ID (in the first frame) and then display the ID and an image (in the second frame). Unfortunately, the image won't display in the second frame :-(
The code below works fine till the start function is called after Enter is pressed on the first frame. The second frame displays the participant ID, but not the image. In the start function, I've tried to simply repeat the code to create the second frame, that I previously used to create the first frame, though I still can't seem to get the gif image to display.
from tkinter import *
from tkinter import ttk
def start(*args):
try:
setPID = participantID
startframe.destroy()
imageframe = ttk.Frame(root, padding="30 15 10 10")
imageframe.grid(column=0, row=0, sticky=(N, W, E, S))
imageframe.columnconfigure(0, weight=1)
imageframe.rowconfigure(0, weight=1)
ttk.Label(imageframe, textvariable=setPID).grid(column=2, row=1, sticky=(W, E))
ttk.Label(imageframe, text="participant ID").grid(column=1, row=1, sticky=W)
for child in imageframe.winfo_children(): child.grid_configure(padx=5, pady=5)
label = ttk.Label(root)
image1 = PhotoImage(file='/home/mj8/Dropbox/pythonImage/dog.gif')
label['image'] = image1
label.grid(column=10, row=10)
except ValueError:
pass
#
# Set up main window
#
root = Tk()
root.title("Experiment")
#
# create a frame widget, which will hold all the content
# of our user interface, and place that in our main window
#
startframe = ttk.Frame(root, padding="30 15 10 10")
startframe.grid(column=0, row=0, sticky=(N, W, E, S))
#
# if the main window is resized, the frame should expand to
# take up the extra space
#
startframe.columnconfigure(0, weight=1)
startframe.rowconfigure(0, weight=1)
#
# Create main widgets were "action" happens, such as
# entering a value, and button pressing
participantID = StringVar()
setPID = StringVar()
participantID_entry = ttk.Entry(startframe, width=7, textvariable=participantID)
participantID_entry.grid(column=2, row=1, sticky=(W, E))
ttk.Button(startframe, text="Start", command=start).grid(column=2, row=2, sticky=W)
#
# Place the widget in the grid (placement of static text labels)
#
ttk.Label(startframe, text="participant ID").grid(column=1, row=1, sticky=W)
#
# Adding padding around all grid content (make it pretty)
#
for child in startframe.winfo_children(): child.grid_configure(padx=5, pady=5)
participantID_entry.focus() # starting placement of cursor
root.bind('<Return>', start) # if user presses 'return', run start
root.mainloop()
Upvotes: 0
Views: 57
Reputation: 13729
This is a common gotcha that is described in lots of places, for example the big warning box on the bottom of this page. You need to keep a reference to the photoimage. This is usually done by attaching the photoimage to the Label instance. In your case:
label.image1 = PhotoImage(file='/home/mj8/Dropbox/pythonImage/dog.gif')
label['image'] = label.image1
Upvotes: 1