gpt4 book ai didi

python - 有没有办法将html嵌入到pygame中

转载 作者:行者123 更新时间:2023-12-04 08:16:24 26 4
gpt4 key购买 nike

所以,我正在尝试在 pygame 中创建一个虚拟助手,但我找不到任何方法将我的搜索结果添加到窗口中,我正计划用 beatifulsoup 并将该 html 放入 pygame,但问题是我不知道如何将 html 嵌入到 pygame 中,并且需要明确的是,我不是在谈论将 pygame 嵌入到 html 中,而是将 html 放入 pygame 窗口中

最佳答案

无法在 pygame 中直接执行此操作。但是您可以使用外部工具,例如 wkhtmltoimage将您的 HTML 呈现为图像并在 pygame 中使用它。

这是一个使用 imgkit 的简单示例(wkhtmltoimage 的 python 包装器):

import pygame
import imgkit
from io import BytesIO

def main():
config = imgkit.config(wkhtmltoimage=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage.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

关于python - 有没有办法将html嵌入到pygame中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65680711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com