简体   繁体   中英

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

 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. 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. However, there is a solution using a PLI image. See Tkinter PhotoImage . Make a PLI image form the buffer and use that image to create an ImageTk.PhotoImage object:

 from PIL import Image, ImageTk
 image = Image.frombytes('RGBA', (400, 400), byt, 'raw') root = Tk() photo_image = ImageTk.PhotoImage(image)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM