作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我正在尝试在 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()
关于python - 有没有办法将html嵌入到pygame中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65680711/
我是一名优秀的程序员,十分优秀!