tkinter无法在不包含mainloop的类中添加照片 [英] tkinter can't add photo in class that not include mainloop

查看:378
本文介绍了tkinter无法在不包含mainloop的类中添加照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉python,特别是GUI问题。
我正在尝试从其他类添加图像,我找到了添加其他对象但不添加图像的方法。
此代码工作正常:

I'm not familiar with python and specially with GUI issues. I'm trying to add image from other class, I found a way to add other object but not images. This code work fine:

from Tkinter import *
from PIL import Image, ImageTk

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.parent = master
        self.initUI()

    def initUI(self):
        self.outputBox = Text(self.parent, bg='black', height= 10, fg='green', relief=SUNKEN, yscrollcommand='TRUE')
        self.outputBox.pack(fill='both', expand=True)



def main():
    root = Tk()
    app = Application(root)
    app.parent.geometry('800x500')
    app.parent.configure(background = 'red')
    path = "../img/Stalin.jpeg"
    img = ImageTk.PhotoImage(Image.open(path))
    panel = Label(app.parent, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")  
    app.mainloop()

main()

但是ñ我尝试从课堂上添加图片它打开窗口但没有图片:

But when I try to add image from class it's open the window but without the image:

from Tkinter import *
from PIL import Image, ImageTk

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.parent = master
        self.initUI()

    def initUI(self):
        self.outputBox = Text(self.parent, bg='black', height= 10, fg='green', relief=SUNKEN, yscrollcommand='TRUE')
        self.outputBox.pack(fill='both', expand=True)
        path = "../img/Stalin.jpeg"
        img = ImageTk.PhotoImage(Image.open(path))
        panel = Label(self.parent, image = img)
        panel.pack(side = "bottom", fill = "both", expand = "yes")  

def main():
    root = Tk()
    app = Application(root)
    app.parent.geometry('800x500')
    app.parent.configure(background = 'red')
    app.mainloop()

main()

在eclipse上使用python 2.7。

working with python 2.7 on eclipse.

推荐答案

这里 -


您可以使用标签显示PhotoImage和BitmapImage对象。 执行此操作时,请确保保留对图像对象的引用,以防止它被Python的内存分配器垃圾收集。您可以使用全局变量或实例属性。

You can use the label to display PhotoImage and BitmapImage objects. When doing this, make sure you keep a reference to the image object, to prevent it from being garbage collected by Python’s memory allocator. You can use a global variable or an instance attribute.

(强调我的)

但是在你的第二个例子中,图像对象 img initUI()方法中的局部变量,因此当方法结束时,图像被垃圾收集(特别是在CPython中,因为它使用引用计数)。您应该将图像存储为实例属性 -

But in your second example, the image object img is a local variable in initUI() method, hence when the method ends the image gets garbage collected (especially in CPython, since it uses reference counting) . You should store the image as an instance attribute -

def initUI(self):
    self.outputBox = Text(self.parent, bg='black', height= 10, fg='green', relief=SUNKEN, yscrollcommand='TRUE')
    self.outputBox.pack(fill='both', expand=True)
    path = "../img/Stalin.jpeg"
    self.img = ImageTk.PhotoImage(Image.open(path))
    panel = Label(self.parent, image = self.img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")  






它适用于你的第一种情况,因为你从 mainloop > main()方法,因此 main()方法直到 mainloop()结束,因此在应用程序退出之前,图像不会被垃圾收集。


It works in your first case, because you enter the mainloop from main() method, hence the main() method does not end until the mainloop() ends, and hence the image does not get garbage collected until the application quits.

这篇关于tkinter无法在不包含mainloop的类中添加照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆