【问题标题】:How to load a image into a label without saving to hard drive python Tkinter如何将图像加载到标签中而不保存到硬盘 python Tkinter
【发布时间】:2015-07-27 19:16:38
【问题描述】:

我正在做一个项目,其中有一个列表,其中包含几个 StringIO。我想将它们加载到 Tkinter 标签中,但出现此错误:IOError: cannot identify image file。我知道这意味着它找不到图像文件。但是如何在不保存到硬盘驱动器的情况下显示图像?

这是我的代码:

from PIL import ImageTk, Image as im
from cStringIO import StringIO
from threading import Thread
from Tkinter import *
import socket
import os

class Application(Frame):
    def __init__(self, parent):
        Frame.__init__(self,parent)
        self.pack(fill=BOTH)

        self.images = []

        self.create_Browse()

    def create_Browse(self):
        for item in self.images:
            img = ImageTk.PhotoImage(im.open(item))

            label = Label(self, image=img)
            label.image = img
            label.pack(side=TOP)

root = Tk()
root.title("None")
root.resizable(0,0)
root.geometry("650x500")
root.iconbitmap("Files/System_Files/icon.ico")

app = Application(root)

root.mainloop()

这只是一个例子。 self.images 列表中将包含 StringIO

【问题讨论】:

    标签: python image tkinter label stringio


    【解决方案1】:

    您可能需要在创建图像之前重置您的 StringIO 对象:

    for item in self.images:
        item.seek(0) # set file position back to the start
        img = ImageTk.PhotoImage(Image.open(item))
    

    【讨论】:

      猜你喜欢
      • 2015-05-16
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 2017-08-24
      • 2012-11-17
      相关资源
      最近更新 更多