Home > Software design >  A buffer which shows in pygame does not display in tkinter
A buffer which shows in pygame does not display in tkinter

Time:07-25

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

CodePudding user response:

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)
  • Related