简体   繁体   中英

Tkinter image not adding to text widget

I am trying to make text widget that get images from reddit then displaying it on a text widget, but for some reason, with no error, the image is not displaying on the widget.

This is the part of my code that get the image from reddit and adding it on the widget.

subreddit = reddit.subreddit("MinecraftMemes")
filtered = [x for x in subreddit.top() if not x.stickied and not x.is_self]
url = filtered[random.randint(0, len(filtered) - 1)].url
url_type = self.get_url_type(url)
urllib.request.urlretrieve(url,f"minecraft{url_type}")
image = Image.open(f"minecraft{url_type}")
image.save('minecraft.png')
img = tkinter.PhotoImage(file=r"minecraft.png")
self.text_widget.image_create(END,image=img)
print("activated")

The activated is printed, the minecraft.png is seen on file explorer, so it is the part of the code with tkinter that is not working

I believe the issue is that the image is an object and it gets discarded when the code executes, so you might want to do self.img .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM