Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

python - is there any way of embedding html INTO pygamee

so, I am trying to create a virtual assistant in pygame, but I could not find any way of adding my search results into the window, I was planning to web scrape all the html with beatifulsoup and put that html into pygame, but the problem is that I don't know any way of how one can embed html inside pygame, and to be clear, I am not talking about embedding pygame to html, but putting html inside a pygame window


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There's no way to do this directly in pygame. But you can use an external tool like wkhtmltoimage to render your HTML to an image and use that in pygame.

Here's a simple example using imgkit (a python wrapper for wkhtmltoimage):

import pygame
import imgkit
from io import BytesIO

def main():
    config = imgkit.config(wkhtmltoimage=r'C:Program Fileswkhtmltopdfinwkhtmltoimage.exe')

    pygame.init()
    screen = pygame.display.set_mode((600, 480))

    html = "<style type = 'text/css'> body { font-family: 'Arial' } </style><body><h1>Html rendering</h1><div><ul><li><em>using pygame</em></li><li><strong>using imgkit</strong></li></ul></div></body>"
    img = imgkit.from_string(html, False, config=config)
    surface = pygame.image.load(BytesIO(img)).subsurface((0,0,280,123))
    r = 0
    center = screen.get_rect().center
    clock = pygame.time.Clock()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                return

        screen.fill('white')
        tmp = pygame.transform.rotozoom(surface, r, 1)
        tmp_r = tmp.get_rect(center=center)
        screen.blit(tmp, tmp_r)
        r += 1
        pygame.display.flip()
        clock.tick(60)

if __name__ == '__main__':
    main()    

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...