So I'm developing a GUI where a label, that will contain an Image, will only be shown after clicking the option onMenuUpToDown.
Other functions with the same code and same image format work properly but when it comes to this function, it just doesn't. The following should be seen.
and this is the output instead
.
As for the image being passed to the label, Image.show()
, demonstrates that it arrives correctly to the variable .
This is part of my code:
def onMenuUpToDown():
global OriginalImRight
UpperImageHolder.grid(column=0,row=0)
LowerImageHolder.grid(column=0,row=1)
auxIm=ImageTk.getimage(OriginalImRight).resize((960,500))
auxIm.show()
print(str(type(auxIm)))
UpperImageHolder['image']=ImageTk.PhotoImage(auxIm)
LowerImageHolder['image']=ImageTk.PhotoImage(auxIm)
AnaglyphImageHolder.grid_remove()
LeftImageHolder.grid_remove()
RightImageHolder.grid_remove()
# create the main window
root = tk.Tk()
root.attributes('-fullscreen', True)
root.title("Augmented Reality Glasses")
root.iconbitmap("ImagesAndIcons/Glasses.ico")
root.config(bg="darkgray")
# General Use Variables Definition
leftImage=tk.PhotoImage(file="ImagesAndIcons/Missing.png")
rightImage=leftImage
RedIm=leftImage
CyanIm=leftImage
Merged=leftImage
OriginalImLeft=leftImage
OriginalImRight=leftImage
UpperImage=ImageTk.PhotoImage(Image.open("ImagesAndIcons/Missing.png").resize((960,500)))
LowerImage=ImageTk.PhotoImage(Image.open("ImagesAndIcons/Missing.png").resize((960,500)))
# Frames definition
ImagesFrame=tk.Frame(width=1080,height=700, bg='darkgray')
ImagesFrame.grid(column=0,row=0)
#Image display
#ParallelView
LeftImageHolder=tk.Label(ImagesFrame,image=leftImage)
LeftImageHolder.grid(column=0,row=0)
RightImageHolder=tk.Label(ImagesFrame,image=leftImage)
RightImageHolder.grid(column=3,row=0)
#Anaglyph View
AnaglyphImageHolder=tk.Label(ImagesFrame,image=Merged)
AnaglyphImageHolder.grid(column=1,row=0)
AnaglyphImageHolder.grid_remove()
#UptoDown View
UpperImageHolder=tk.Label(ImagesFrame,image=UpperImage)
UpperImageHolder.grid(column=0,row=0)
UpperImageHolder.grid_remove() # here are the labels used and shown when called from menu
LowerImageHolder=tk.Label(ImagesFrame,image=LowerImage)
LowerImageHolder.grid(column=0,row=1)
LowerImageHolder.grid_remove()
# Menus Definition
MenuBar= tk.Menu(root)
fileMenu=tk.Menu(MenuBar, tearoff=0)
leftRightMenu=tk.Menu(fileMenu,tearoff=0)
leftRightMenu.add_command(label="Import left Image", command=onMenuImportLeft)
leftRightMenu.add_command(label="Import right Image", command=onMenuImportRight)
fileMenu.add_cascade(label='Import Images',menu=leftRightMenu)
EditMenu= tk.Menu(MenuBar, tearoff=0)
EditMenu.add_command(label="Anaglyph", command=onMenuAnaglyph)
EditMenu.add_command(label="Crossed sight", command=onMenuCrossed)
EditMenu.add_command(label="Parallel sight (Default view)", command=onMenuParallel)
EditMenu.add_command(label="Up to down sight", command=onMenuUpToDown) #Here's the command to the error
MenuBar.add_cascade(label='Import', menu=fileMenu)
MenuBar.add_cascade(label='Edit', menu=EditMenu)
root.config(menu=MenuBar)
# create a label and pack it into the main window
# run the GUI
root.mainloop()
I changed to tk.photoImage yet it throws and error
TypeError: __str__ returned non-string (type Image)
I also tried to diagnose the type of the image but is just a regular PIL.image.image and as said, when using show() the image is correct.
A similar post suggested its a matter of garbage collecting or global variables, yet it doesn’t apply here (I think) because I made sure to use an local auxiliar variable to show the image, and basically it works. similar post