如何在网站上访问 pictures/text 并使用 Tkinter 显示它们?
How can I access pictures/text on a website and display them with Tkinter?
我正在尝试编写一个 Python 3.x 程序来从网站访问数据(图片、文本等)。我还想在 Tkinter GUI 中显示该数据。
顺便说一句,我在 Windows 上使用 Python 3.4。
https://pypi.python.org/pypi/wget
试试 wget!这是一个简单的网络抓取工具,应该可以满足您的需求。
尝试使用 urllib
下载并使用 PIL
的 Pillow
分支来处理图像:
from Tkinter import *
from PIL import ImageTk
import urllib
# initialize window
root = Tk()
root.geometry('640x480')
# retrieve and download image
location = 'https://www.python.org/static/community_logos/python-logo.png'
image = open('image.png', 'wb')
image.write(urllib.urlopen(location).read())
image.close()
# create canvas for drawing
canvas = Canvas(root, width = 640, height = 480)
canvas.place_configure(x = 0, y = 0, width = 640, height = 480)
# load image with PIL and draw to canvas
image = ImageTk.PhotoImage(file = 'image.png')
canvas.create_image(10, 10, image = image, anchor = NW)
# start program
root.mainloop()
我正在尝试编写一个 Python 3.x 程序来从网站访问数据(图片、文本等)。我还想在 Tkinter GUI 中显示该数据。
顺便说一句,我在 Windows 上使用 Python 3.4。
https://pypi.python.org/pypi/wget
试试 wget!这是一个简单的网络抓取工具,应该可以满足您的需求。
尝试使用 urllib
下载并使用 PIL
的 Pillow
分支来处理图像:
from Tkinter import *
from PIL import ImageTk
import urllib
# initialize window
root = Tk()
root.geometry('640x480')
# retrieve and download image
location = 'https://www.python.org/static/community_logos/python-logo.png'
image = open('image.png', 'wb')
image.write(urllib.urlopen(location).read())
image.close()
# create canvas for drawing
canvas = Canvas(root, width = 640, height = 480)
canvas.place_configure(x = 0, y = 0, width = 640, height = 480)
# load image with PIL and draw to canvas
image = ImageTk.PhotoImage(file = 'image.png')
canvas.create_image(10, 10, image = image, anchor = NW)
# start program
root.mainloop()