-3

Alright, so I want to make a gif display when a function has been called, but the gif will go invisible and not show up. I searched for possible answers but all of them mention "create a reference to (insert code here)" and I don't really get it because: 1. 99% of them use objects and classes in which I have 0 experience 2. Some say to make a reference with "self.img = PhotoImage(...)" which I'm pretty sure its connected to objects and classes. 3. Others only say to create a reference.

Sorry for being somewhat rude. I'm just really fed up, I searched for answers for 2 hours now.

I tried to assign the variable to global, place the variable in the function and tried to remake the gif and rename the file

This is what I tried to do

def red_flicker():
    global root
    red_btn_flicker = tk.PhotoImage(file='test.gif')
    label_red = tk.Label(image=red_btn_flicker)
    label_red.place(x=red_btn_place_x, y=red_btn_place_y)

the gif is invisible.

Please be noob friendly. Any stuff about python 2.7 and using objects/classes will be ignored

Scarm
  • 1
  • 1
  • read note abut bug in PhotoImage which removes image: [PhotoImage](http://effbot.org/tkinterbook/photoimage.htm) – furas Jul 24 '19 at 17:23
  • use `global red_btn_flicker` in function `red_flicker()` to make this variable global and it should resolve problem. Or try `label_red.img = red_btn_flicker` to assing PhotoImage to class instance. – furas Jul 24 '19 at 17:24
  • This is a very common question. Your problem is that the reference to the image is garbage collected as soon as the function ends. You need to save a reference to the image in the global namespace. – Mike - SMT Jul 24 '19 at 17:38
  • @furas tried `global red_btn_flicker`. gif appeared but won't work. I'm very tired so ill give you an image instead. i dont know how to use classes and objects. https://imgur.com/0kjglMR – Scarm Jul 24 '19 at 17:39
  • @Mike-SMT my main problem is that i dont know how to save a reference. I tried to do the thing furas said, but the gif wont work. I replied to him with an imgur link to an image about the result i got – Scarm Jul 24 '19 at 17:43
  • Well there are countless post about this exact problem here on stack overflow. You can do a quick google search for "tkinter image not displayed in label" or anything close to that and get many results all about the same issue. – Mike - SMT Jul 24 '19 at 17:45
  • You said `"gif appeared but won't work."` What means "won't work" ? What do you do with this GIF ? You didn't say anithing about it. Or maybe you have animaged gif ? `Tkinter` can't display animation. You would have to load every frame separatelly and replace them in `Label`. – furas Jul 24 '19 at 18:19

2 Answers2

0

You must save a reference, as mentioned in the answer to this question: Why does Tkinter image not show up if created in a function?

Since you aren't using classes, you can use a global variable. For example:

def red_flicker():
    global red_btn_flicker

    red_btn_flicker = tk.PhotoImage(file='test.gif')
    label_red = tk.Label(image=red_btn_flicker)
    label_red.place(x=red_btn_place_x, y=red_btn_place_y)

Another simple technique is to attach the image as an attribute of the label itself. This works because python lets you create custom attributes on an object. However, you must make sure that the reference to the label itself isn't lost

def red_flicker():
    global label_red

    red_btn_flicker = tk.PhotoImage(file='test.gif')
    label_red = tk.Label(image=red_btn_flicker)
    label_red.place(x=red_btn_place_x, y=red_btn_place_y)

    label_red.image = red_btn_flicker
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

Ok so first things first.

Your function is adding a new label every time it is call so you probably should generate the label in the global namespace once and then just apply the image to the label in the function. This way you can call the function all you want without adding more labels.

I would also move your PhotoImage to the global so you do not need to keep reopening the image each time you load the function.

By making this change we do not even need to use global as the widget creating and image loading happens in the global already.

Make sure to save the reference to the image so its not garbage collected.

import tkinter as tk


root = tk.Tk()

red_btn_flicker = tk.PhotoImage(file='test.gif')
label_red = tk.Label(root)
label_red.pack()


def red_flicker():
    label_red.config(image=red_btn_flicker)
    label_red.image = red_btn_flicker # saving reference to image.


red_flicker()

root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79