Python Tkinter PhotoImage无法正确显示gif

时间:2019-04-06 23:03:08

标签: python image tkinter

我正在尝试将图像放入“ Python Tkinter”窗口的“框架”中,但一直显示的只是一个与图像大小相符的白盒。我正在使用Gif和Tkinter PhotoImage。

        tkinter.Tk.__init__(self,master)
        self.title("Apex Companion")
        self.resizable(width=False, height=False)
        self.geometry("250x400")
        self.overrideredirect(True)
        self.config(bg="#393939")
        #Tob Bar Frame
        tb = tkinter.Frame(self,height=20,width=250,bg="#232323")
        tb.pack_propagate(False)
        #Top Bar Text
        tb_text = tkinter.Label(tb, text="Apex Companion",bg="#232323",fg="#737373")
        #Top Bar Close
        tb_close = tkinter.Button(tb, height=2, width=3,
                                  text="✕", bg="#232323", fg="#ffffff",
                                  activebackground="#c94343",activeforeground="#ffffff",
                                  command=self.destroy, bd=0)
        #Top Bar Minimize
        tb_min = tkinter.Button(tb, height=2, width=2,
                                text="—", bg="#232323",fg="#ffffff"
                                ,bd=0)
        #Top Bar Logo
        tb_img = tkinter.PhotoImage(file="logo_apc.gif")
        tb_logo = tkinter.Label(tb,image=tb_img)

        tb.pack()
        tb_close.pack(side=tkinter.RIGHT)
        tb_min.pack(side=tkinter.RIGHT)
        tb_logo.pack(side=tkinter.LEFT)
        tb_text.pack()

1 个答案:

答案 0 :(得分:0)

当您将PhotoImage分配给局部变量时,它可能是从内存中删除的错误。

尝试将其分配给类变量self.tb.img = tkinter.PhotoImage(...)

页面底部的更多信息:effbot.org/tkinterbook/photoimage.htm