python - cannot associate image to tkinter label -


i trying display image tkinter gui using tkinter.label() widget. procedure seems simple , straightforward, code doesn't work!

code:

import tkinter tk import image, imagetk, sys  filename = 'ap_icon.gif' im = image.open(filename) # image loaded, because im.show() works  tkim = imagetk.photoimage(im)  root = tk.tk()  label = tk.label(root, image = tkim) # here core problem (see text explanation) label.image = tkim # should keep reference, right? label.grid (row = 0, column = 0)  tk.button(root, text = 'quit', command = lambda: sys.exit()).grid(row = 1, column = 1) root.mainloop() 

when execute code, doesn't compile, giving error:

tclerror: image "pyimage9" doesn't exist 

when define label without parent root, no compilation error occurs, gui not display image!

can identify issue?

this problem happens when attempt run above code in ipython. , can solved changing line

root = tk.tk()  root = tk.toplevel() 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -