簡體   English   中英

如何在不保存到硬盤的情況下將圖像加載到標簽中python Tkinter

[英]How to load a image into a label without saving to hard drive python Tkinter

我正在一個項目中,其中有一個包含幾個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

在創建映像之前,您可能需要重置StringIO對象:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM