1

I have the following code:

from tkinter import *
import os
from PIL import ImageTk, Image
#Python3 version of PIL is Pillow

class Scope:
    def __init__(self, master):
        self.master = master        

        f = Frame(root)

        self.greet_button = Button(f, text="Back", command=self.back)
        self.greet_button.pack(side= LEFT)

        self.greet_button = Button(f, text="Detect", command=self.detect)
        self.greet_button.pack(side= LEFT)

        self.close_button = Button(f, text="Continue", command=master.quit)
        self.close_button.pack(side=LEFT)

        photo = PhotoImage(file='demo.gif')
        cv = Label(master, image=photo)
        cv.pack(side= BOTTOM)
        f.pack()

    def greet(self):
        print("Previous image...")

root = Tk()
my_gui = Scope(root)
root.mainloop()

My first problem is that when I run this, all the buttons and the window show up, but there is no image. There is a square place holder indicating that the image should be in that box, but no image actually shows. I'm able to display the image if I just type the following:

root = Tk()

photo = PhotoImage(file='demo.gif')
label = Label(root, image=photo)
label.pack()

root.mainloop()

So, I know it's possible. But I don't know what I'm doing wrong with my GUI code. I've tried debugging this quite a bit and nothing seemed to work.

A second problem is, I am completely unable to display a jpg file in a GUI. I've tried using every tutorial and nothing quite does the trick. Ideally, I'd like to just be able to display a jpg image, if that's not possible, I'll settle for displaying a gif.

2
  • Oops, seems like I wasn't looking for the right thing @BryanOakley. What should I do in this situation? Just delete it? It does have a good answer. I also asked a second part to this question. Should I just reformat the question to just include that? Commented Jul 19, 2017 at 18:14
  • Sorry, I was not aware of the duplicate. However, there is also an answer to your second part on stackoverflow, see here. Conclusion is: Always google first. Learn how to google efficiently. Commented Jul 20, 2017 at 7:55

1 Answer 1

3

Your reference to photo gets destroyed / carbage collected by Python after the class is called, so, there is nothing the label could show.

In order to avoid this, you have to maintain a steady reference to it, i.e. by naming it self.photo:

from tkinter import *
import os
from PIL import ImageTk, Image
#Python3 version of PIL is Pillow

class Scope:
    def __init__(self, master):
        self.master = master    
        f = Frame(root)

        self.greet_button = Button(f, text="Back")  #, command=self.back)
        self.greet_button.pack(side=LEFT)

        self.greet_button = Button(f, text="Detect")  #, command=self.detect)
        self.greet_button.pack(side=LEFT)

        self.close_button = Button(f, text="Continue", command=master.quit)
        self.close_button.pack(side=LEFT)

        self.photo = PhotoImage(file='demo.gif')
        cv = Label(master, image=self.photo)
        cv.pack(side=BOTTOM)
        f.pack()

    def greet(self):
        print("Previous image...")

root = Tk()
my_gui = Scope(root)
root.mainloop()

PS: Your code snippet was not running properly because two functions were missing.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.