[英]A buffer which shows in pygame does not display in tkinter
The code below illustrates a buffer which displays with pygame.image.frombuffer but not with tkinter PhotoImage下面的代码说明了一个缓冲区,它显示为 pygame.image.frombuffer 但不显示 tkinter PhotoImage
import chess, chess.svg, pylunasvg as pl from tkinter import Tk, PhotoImage # This is where I create the buffer board = chess.Board(chess.STARTING_FEN) svg_text = chess.svg.board(board, size=400) document = pl.Document.loadFromData(svg_text) byts = bytes(document.renderToBitmap()) #Now that I have this raster buffer 'byts' surf = pygame.image.frombuffer(byt, (400, 400), 'RGBA') #works root = Tk() PhotoImage(data=byt) #raises an error about the image
I know this is as a result of something I don't know about tkinter.我知道这是因为我不了解 tkinter。 I would really appreciate that you point out what is wrong and what I can do.我真的很感激你指出什么是错的以及我能做什么。 Thanks in advance提前致谢
PhotoImage
's argument must be a file path. PhotoImage
的参数必须是文件路径。 However, there is a solution using a PLI image.但是,有一个使用 PLI 图像的解决方案。 See Tkinter PhotoImage .请参阅Tkinter PhotoImage 。 Make a PLI image form the buffer and use that image to create an ImageTk.PhotoImage
object:从缓冲区制作 PLI 图像并使用该图像创建ImageTk.PhotoImage
object:
from PIL import Image, ImageTk
image = Image.frombytes('RGBA', (400, 400), byt, 'raw') root = Tk() photo_image = ImageTk.PhotoImage(image)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.