How to open PIL Image in Tkinter on Canvas

后端 未结 4 1146
面向向阳花
面向向阳花 2021-02-14 18:08

I can\'t seem to get my PIL Image to work on canvas. Code:

from Tkinter import*
import Image, ImageTk
root = Tk()
root.geometry(\'1000x1000\')
canvas = Canvas(ro         


        
4条回答
  •  情歌与酒
    2021-02-14 18:40

    (An old question, but the answers so far are only half-complete.)

    Read the docs:

    class PIL.ImageTk.PhotoImage(image=None, size=None, **kw)
    
    • image – Either a PIL image, or a mode string. [...]
    • file – A filename to load the image from (using Image.open(file)).

    So in your example, use

    image = ImageTk.PhotoImage(file="ball.gif")
    

    or explicitly

    image = ImageTk.PhotoImage(Image("ball.gif"))
    

    (And remember – as you did correctly: Keep a reference to the image object in your Python program, otherwise it is garbage-collected before you seee it.)

提交回复
热议问题