How to display a png file from a webpage on a tk label in Python?

前端 未结 1 1726
[愿得一人]
[愿得一人] 2021-01-25 18:19

I\'m new to python and I\'m on a windows 7 64 bit with python 3.3. I can display a gif image with the following code. However I can\'t make it work with png files. How to do tha

相关标签:
1条回答
  • 2021-01-25 18:46

    You should use PIL (or pillow). You can find pillow windows binary here.

    Try following example after you install pillow:

    from io import BytesIO
    import urllib
    import urllib.request
    import tkinter as tk
    from PIL import Image, ImageTk
    root = tk.Tk()
    url = "http://imgs.xkcd.com/comics/python.png"
    with urllib.request.urlopen(url) as u:
        raw_data = u.read()
    im = Image.open(BytesIO(raw_data))
    image = ImageTk.PhotoImage(im)
    label = tk.Label(image=image)
    label.pack()
    root.mainloop()
    
    0 讨论(0)
提交回复
热议问题