Tkinter.Frame 中的 Tkinter-PhotoImage

ske*_*ing 5 python tkinter

我正在尝试在我的框架小部件之一中添加 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()
##############################
Run Code Online (Sandbox Code Playgroud)

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

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

关于为什么grapgics不会显示的任何想法?

(它工作的示例测试):从 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()
Run Code Online (Sandbox Code Playgroud)

Iro*_*ist 4

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

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()
Run Code Online (Sandbox Code Playgroud)

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

hitta_pic = PhotoImage(file=frames.pictures['path']+frames.pictures['hitta'])
Run Code Online (Sandbox Code Playgroud)

每次从 中返回时,该对象都会被销毁frame_objects(self),因此您不会显示任何内容,一种解决方法是使该对象成为您的类的属性frames(object),如下所示:

self.hitta_pic = PhotoImage(file=frames.pictures['path']+frames.pictures['hitta']) 
Run Code Online (Sandbox Code Playgroud)

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

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