为什么我的python tkinter GUI在导入到另一个脚本时不显示图像?

时间:2016-10-11 17:50:02

标签: python user-interface tkinter module pillow

我正在为脚本创建GUI。我首先开发了一个空GUI(代码如下所示)。图像没有显示,但后来我用Google搜索并意识到对图像的引用被垃圾收集并根据此链接进行修复(http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm)。然后我将图像作为GUI脚本中的全局变量,并且也起作用。

此文件名为GUI.py:

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()

我现在的问题是,当我将这个GUI.py导入我的实际测试脚本时,我又丢失了图像。我已经尝试在该脚本中加载图像并将引用传递给GUI但是没有用。

import GUI

这是我从脚本中更改标签文本和进度条值的方法:

GUI.p2.state.set('Running')
GUI.p2.progress["value"] = 50
GUI.root.update()

如果我自己运行带有mainloop()的GUI,则图像显示正常。当我从第二个脚本中将其作为导入的模块运行时,它不显示图像。我做错了什么?

1 个答案:

答案 0 :(得分:-1)

你的图片文件在哪里?您确定将图像放在项目文件夹中吗?确保你的路径没有区别。