-2

I want to create an png format image in Tkinter text widget. when I run my code, it works but just show a white image. what can I do about that?

elif int(w4_toc1.selection()[0]) == 21:
w4_tex_title.insert('end', doc.paragraphs[57].text)
w4_tex_title.insert('end', '\n'+'\n'+'\n'+doc.paragraphs[58].text)
for i in range(59,62):
    w4_testrong textx_title.insert('end', '\n'+'\n'+doc.paragraphs[i].text)`enter code here`
from PIL import Image,ImageTk
dimension_table = Image.open('icons\coordinate.png')
dt = ImageTk.PhotoImage(dimension_table)
w4_tex_title.image_create('end+2 lines',align=CENTER, image=dt)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

0

You need to keep reference to the image, or pythons garbage collector sweeps the image.

dt = ImageTk.PhotoImage(dimension_table)
w4_tex_title.image = dt #Keeping Reference to the Image
w4_tex_title.image_create('end+2 lines',align=CENTER, image=dt)
coderoftheday
  • 1,987
  • 4
  • 7
  • 21