【问题标题】:Tkinter-PhotoImage in a Tkinter.FrameTkinter.Frame 中的 Tkinter-PhotoImage
【发布时间】:2015-07-23 00:10:39
【问题描述】:

我正在尝试在我的一个框架小部件中添加一个 PhotoImage()。 不过似乎有问题,因为图片的图形不会显示。

代码如下:

        from urllib import urlopen
    import Tkinter
    from Tkinter import *

    class frames(object):

        pictures = {"path": "c:/users/xxxxx/desktop/source/pics/",
                    'google': 'google.gif',
                    'wiki': 'wikipedia.gif',
                    'tyda': 'tyda.gif',
                    "hitta": "hitta.gif"
                    }

        def __init__(self, parent):

            #Creation of main frames
            self.parent = parent
            self.logo_frame = Frame(parent)
            self.input_frame = Frame(parent)
            self.output_frame = Frame(parent)

        def configure(self):
            #root window
            self.parent.geometry('400x400')
            self.parent.title('w0rksuite')
            self.parent.configure(bg='grey')
            ################################################################
            #geometry logo_frame
            self.logo_frame.configure(bg='white', width=385, height=45)
            self.logo_frame.grid(column=0, row=0, padx=6, pady=4)
            ################################################################
            #geometry input_frame
            self.input_frame.configure(bg='white', width=385, height=45)
            self.input_frame.grid(column=0, row=1, padx=6, pady=4)  
            ################################################################
            #geometry output_frame
            self.output_frame.configure(bg='white', width=385, height=280)
            self.output_frame.grid(column=0, row=2, padx=6, pady=4)
            ################################################################

        def frame_objects(self):
            #INPUT ENTRY
            input_entry = Entry(self.input_frame, width=55)
            input_entry.grid(column=0, row=1)
            #INPUT TEXT
            input_text = Label(self.input_frame, text='Search:')
            input_text.grid(column=0, row=0, sticky=W)
            input_button = Button(self.input_frame, text='Find')
            #input_button.configure(height=)
            input_button.grid(column=2, row=1, padx=3, pady=0)
            #IMAGE OBJECTS
            hitta_pic = PhotoImage(file=frames.pictures['path']+frames.pictures['hitta'])
            hitta_button = Button(self.logo_frame, image=hitta_pic)
            hitta_button.grid(column=0, row=0, sticky=W)



#Initiate Windows#############
root = Tk()
master_frames = frames(root)
master_frames.configure()
master_frames.frame_objects()
root.mainloop()
##############################

我的粗体文本是我制作 PhotoImage 并将其连接到我的框架的方式。 按钮出现,但图片没有图形。

如果我在单独的 python 文件中执行此操作只是为了尝试该功能,无论按钮图像是否可以添加到框架中,它都可以工作并且图片显示。

关于为什么图形不显示的任何想法?

(有效的示例测试): 导入 Tkinter 从 Tkinter 导入 *

root = Tk()
frame = Frame(root)
image = PhotoImage(file='c:/users/xxxxxxx/desktop/source/pics/hitta.gif')
button = Button(frame, image=image)
button.pack()
frame.pack()
root.mainloop()

【问题讨论】:

  • 当你在一个类而不是全局命名空间中工作时,图片存储在一个局部变量中,当函数完成时,局部变量被垃圾收集,为了防止这种情况,你可以给你的类一个列表以存储图像,然后在创建每个图像后执行 frames.imagelist.append(image) 这将保留对您添加到列表中的每个图像的引用

标签: python tkinter


【解决方案1】:

实际上,您回答了自己,但将这段代码放在问题的末尾以向我们展示它的工作原理:

root = Tk()
frame = Frame(root)
image = PhotoImage(file='c:/users/xxxxxxx/desktop/source/pics/hitta.gif')
button = Button(frame, image=image)
button.pack()
frame.pack()
root.mainloop()

这里image 对象不会被销毁并保留在内存中,因此您可以始终引用它并仍然拥有其数据,但在您创建对象的frames(object)frame_objects(self) 方法中:

hitta_pic = PhotoImage(file=frames.pictures['path']+frames.pictures['hitta'])

每次你从frame_objects(self)返回时,这个对象都会被销毁,所以你什么都没有显示, 一种解决方法是使该对象成为您的类frames(object) 的属性,这样:

self.hitta_pic = PhotoImage(file=frames.pictures['path']+frames.pictures['hitta']) 

但是当你需要引用这个对象时,你必须这样做:

hitta_button = Button(self.logo_frame, image=self.hitta_pic)#here we associate to the name of object the self keyword.

【讨论】:

  • 是的 ofc!我是使用类的新手,我不会说我是它们如何工作和操作的大师。谢谢你,它给了我更好和更深入的洞察力了解课程的运作和工作方式。
  • 当然,只要记住这一点,每个方法和变量只有在使用“self”关键字引用时才是类属性,例如:“self.image”,如果您对答案感到满意,可能是您应该考虑投票..:)
猜你喜欢
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多