Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何仅使用标准Python库向Python 2.7中的Tkinter添加URL图像?_Image_Python 2.7_Tkinter - Fatal编程技术网

Image 如何仅使用标准Python库向Python 2.7中的Tkinter添加URL图像?

Image 如何仅使用标准Python库向Python 2.7中的Tkinter添加URL图像?,image,python-2.7,tkinter,Image,Python 2.7,Tkinter,我见过很多关于如何使用图像URL在Tkinter中显示图像的示例,但这些示例都不适合我,例如 import urllib from Tkinter import * import io from PIL import Image, ImageTk app = Tk() app.geometry("1000x800") im = None #<-- im is global def search(): global im #<-- declar im as glo

我见过很多关于如何使用图像URL在Tkinter中显示图像的示例,但这些示例都不适合我,例如

import urllib 
from Tkinter import *
import io
from PIL import Image, ImageTk


app = Tk()
app.geometry("1000x800")

im = None  #<-- im is global

def search():
    global im  #<-- declar im as global, so that you can write to it
               # not needed if you only want to read from global variable.
    tx1get = tx1.get()
    Label(app, text="You Entered: \"" + tx1get + "\"").grid(row=1, column=0)
    fd = urllib.urlopen("http://ia.media-imdb.com/images/M/MV5BMTc2MTU4ODI5MF5BMl5BanBnXkFtZTcwODI2MzAyOA@@._V1_SY317_CR7,0,214,317_AL_.jpg")
    imgFile = io.BytesIO(fd.read())
    im = ImageTk.PhotoImage(Image.open(imgFile))
    image = Label(app, image = im, bg = "blue")
    image.grid(row=2, column=0)

tx1=StringVar()
tf = Entry(app, textvariable=tx1, width="100")
b1 = Button(app, text="Search", command=search, width="10")
tf.grid(row=0, column=0)
b1.grid(row=0, column=1)

app.mainloop()
我得到了“无模块名请求”,几乎所有的示例都使用了PIL模块,但我无法让它们工作,因为Python 2.7无法识别其中的许多模块。我需要显示一个图像作为评估的一部分,虽然我们可以导入诸如Tkinter之类的东西,但该文件需要运行,而无需从标准Python库之外添加模块

同样值得注意的是,我甚至不能导入“tkinter”。它会说没有名为“tkinter”的模块,因为它需要以大写字母“T”开头

因此,我的问题是:

  • PIL是否需要我安装其他软件/库

  • 输入没有大写字母“T”的“tkinter”是否因为我使用的是Python 2.7而不起作用

  • 使用Python2.7如何从URL在Tkinter窗口中显示图像


  • 这在windows上使用python 2.7时有效:

    from io import BytesIO
    import Tkinter as tk
    import urllib  # not urllib.request
    from PIL import Image, ImageTk
    
    root = tk.Tk()
    url = "http://imgs.xkcd.com/comics/python.png"
    
    u = urllib.urlopen(url)
    raw_data = u.read()
    u.close()
    
    im = Image.open(BytesIO(raw_data))
    image = ImageTk.PhotoImage(im)
    label = tk.Label(image=image)
    label.pack()
    root.mainloop()
    
    回答您的问题:

  • 您需要安装PIL(它不是Python 2.7的标准配置)
  • 是的,您需要在Python 2.7中导入
    Tkinter
    tkinter
    适用于Python 3.x
  • 您可以使用上面的代码(只要您安装了PIL)
  • 而且

  • 在Python2.7中,您需要
    urllib
    而不是
    urllib.request
  • 似乎您不能在urllib中将
    与…open(x)作为fname一起使用,因此您需要显式地打开和关闭文件
    
    好的,谢谢你的反馈。我将不得不这样做,没有图像,因为这是一个任务,我不允许安装库。
    from io import BytesIO
    import Tkinter as tk
    import urllib  # not urllib.request
    from PIL import Image, ImageTk
    
    root = tk.Tk()
    url = "http://imgs.xkcd.com/comics/python.png"
    
    u = urllib.urlopen(url)
    raw_data = u.read()
    u.close()
    
    im = Image.open(BytesIO(raw_data))
    image = ImageTk.PhotoImage(im)
    label = tk.Label(image=image)
    label.pack()
    root.mainloop()