0

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

  • is this code inside a function? Does this answer the question? https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function – Bryan Oakley Oct 17 '21 at 15:28
  • I believe it is not working because the file is a png. Try converting it in to a GIF file and see if it works. If it is not working then I would recommend you use the [Pillow module](https://pillow.readthedocs.io/en/stable/) – ss3387 Oct 03 '22 at 11:04

1 Answers1

0

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.

AdomasOG
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '22 at 20:03