Tkinter Label does not show Image

前端 未结 3 1085
心在旅途
心在旅途 2020-11-27 21:32

I\'m trying to learn some tkinter. I can\'t get tkinter to display an icon. I don\'t know where it goes wrong. It does not produce any error and it respects the size of the

3条回答
  •  离开以前
    2020-11-27 22:11

    For some reason (I don't understand exactly why) you must anchor the image object into the widget in order for it to display. Therefore try the following change at your code:

    from Tkinter import *
    from PIL import Image, ImageTk
    
    class GUI:
    
        def __init__(self, master):
    
            frame = Frame(master)
            frame.pack()
    
            #status bar
            self.bar = Frame(root, relief=RIDGE, borderwidth=5)
            self.bar.pack(side=TOP)
    
            self.iconPath = 'data/icons/size.png'
            self.icon = ImageTk.PhotoImage(Image.open(self.iconPath))
            self.icon_size = Label(self.bar)
            self.icon_size.image = self.icon  # <== this is were we anchor the img object
            self.icon_size.configure(image=self.icon)
            self.icon_size.pack(side=LEFT)
    
    root = Tk()
    
    
    app = GUI(root)
    
    root.mainloop()
    

    Good Luck!

提交回复
热议问题