Why does my python tkinter GUI not show images when imported into another script?
<p>I am creating a GUI for a script. I first developed an empty GUI (code shown below). The images were not showing up but then I googled and realized references to the image were being garbage collected and fixed it according to this link (<a href="http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm" rel="nofollow">http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm</a>). I then made the image a global variable within the GUI script and that also worked.</p>

<p>This file is called GUI.py:</p>

<pre><code>import Tkinter as tk
import ttk
import tkMessageBox
import time
from PIL import ImageTk, Image

class StartPage(tk.Frame):
    def __init__(self, master, text, height, width, *args, **kwargs):
        global logo
        tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
        self.height = height
        self.width = width
        # path = "test.jpg"
        # self.img = ImageTk.PhotoImage(Image.open(path))
        logo = ImageTk.PhotoImage(Image.open('test.jpg'))
        self.picture = tk.Label(self, image=logo)
        #self.picture.image = img
        self.picture.pack(side = "bottom", fill = "both", expand = "yes")
        label = tk.Label(self, text='Waiting', font=("Helvetica bold", 24)).pack()
        label = tk.Label(self, text='Click START Button', font=("Helvetica", 16)).pack(expand=True)
        button = tk.Button(self, text=text, font=('Helvetica', 20),
                           command=lambda: self.callback())
        button.pack(side="top", expand=True)
        root.update()
    def onlift(self):
        root.geometry('{}x{}'.format(self.width, self.height))
        self.lift()

class TestPage(tk.Frame):
    def __init__(self, master, text, height, width, *args, **kwargs):
        tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
        self.height = height
        self.width = width
        self.state = tk.StringVar()
        self.label = tk.Label(self, textvariable=self.state, font=("Helvetica", 16)).pack()
        self.progress = ttk.Progressbar(self, orient='horizontal', length=1000, mode='determinate')
        self.progress.pack()
        path = 'connect.jpg'
        img = ImageTk.PhotoImage(Image.open(path))
        self.picture = tk.Label(self, text='test image', image=img)
        self.picture.image = img
        self.picture.pack()
        root.update()
    def onlift(self):
        global p1
        root.geometry('{}x{}'.format(self.width, self.height))
        self.lift()
        self.progress["value"] = 0
        self.state.set('Running...')
        root.update()
        time.sleep(1)
        self.progress["value"] = 50
        root.update()
        confirm = tkMessageBox.askyesno(message='ON?', icon='question', title='Confirmation')
        #print confirm
        if confirm:
            print 'Confirmed'
            self.progress["value"] = 100
            self.state.set('PASSED!')
            root.update()
            tkMessageBox.showinfo(title='Test Passed',message='PASS')
            self.label

        else:
            self.state.set('Test FAILED!')
            root.update()
            tkMessageBox.showinfo(title='Test Failed',message='FAIL',icon='warning')
        p1.onlift()


class App(tk.Frame):
    def __init__(self, *args, **kwargs):
        global p1
        tk.Frame.__init__(self, *args, **kwargs)

        p1 = StartPage(self, 'START', height=root.winfo_screenheight(), width=root.winfo_screenwidth())
        p2 = TestPage(self, 'blank', height=root.winfo_screenheight(), width=root.winfo_screenwidth())
        p1.callback = p2.onlift
        p2.callback = p1.onlift

        p1.place(x=0, y=0, relwidth=1, relheight=1)
        p2.place(x=0, y=0, relwidth=1, relheight=1)

        p1.onlift()

global p1
global p2
global logo
global connectimg

root = tk.Tk()
root.title('GUI')
app = App(root)
root.update()
</code></pre>

<p>My problem now is, when I import this GUI.py into my actual test script I lose the images again. I have tried loading the images in that script and passing the reference to the GUI but that has not worked.</p>

<pre><code>import GUI
</code></pre>

<p>This is how I change the label text and progress bar value from my script:</p>

<pre><code>GUI.p2.state.set('Running')
GUI.p2.progress["value"] = 50
GUI.root.update()
</code></pre>

<p>If I run the GUI with a mainloop() on its own the images show up fine. When I run it as an imported module from the second script it does not display the images. What am I doing wrong?</p>

<p>Where are your picture files? Did you make sure to put the images in your project folder? Make sure your path isn't different.</p>

python user-interface tkinter module pillow