【问题标题】:Is it possible to run Firefox or Chrome in a pygame window with Python?是否可以使用 Python 在 pygame 窗口中运行 Firefox 或 Chrome?
【发布时间】:2020-09-14 09:44:35
【问题描述】:

我有一个 Pygame 程序,我在互联网上找到了一个带有实时 html 对象的网站,我想要在我的 pygame 窗口中使用它。我尝试了很多模块,但我没有找到我想要的。

是否有可能拥有 Firefox 或 Chrome 的整个窗口/onglet,或者只是一个 (div) html 对象。

这是我的 pygame 代码:

import pygame
import pyperclip
import autoit
import math
import time
import tkinter as tk
from tkinter import filedialog
import os
import ftplib
from shutil import copyfile
from datetime import datetime
import pytz
from random import seed
from random import randint
import time
file1 = open('nvpn.txt', 'r')
Lines = file1.readlines()
# Start pygame
pygame.init()
clock = pygame.time.Clock()
# set the pygame window name
pygame.display.set_caption('Agents of shield')
# define the RGB value for white,
#  green, blue colour .
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
class CustText:
    def __init__(self,content, fontSize, color1, color2, posX, posY):
        self.text = pygame.font.Font('freesansbold.ttf', fontSize).render(content, True, color1, color2)
        x = self.text.get_rect()[0]/2
        y = self.text.get_rect()[1]/2
        self.textRect = (int(x) + posX, int(y) + posY)


def main():
    loop = True
    fenetre = pygame.display.set_mode((640, 480))
    # Desclarations
    a1 = Lines[randint(0, 2)]
    Text2 = CustText(a1, 18, (0, 0, 0), (255, 255, 255), 10, 100)
    print(a1)
    while loop:
        fenetre.fill(white)
        a = datetime.now(pytz.timezone('America/New_York')).strftime("%H:%M:%S")

        heures = a[0] + a[1]
        minutes = a[3] + a[4]
        seconde = a[6] + a[7]

        difSec = 60 - int(seconde)
        difMin = 59 - int(minutes)
        if datetime.now().day == 26:
            difHeures = 43 - int(heures)
        else:
            difHeures = 43 - int(heures)

        a2 = str(difHeures) + "H " + str(difMin) + "M " + str(difSec) + "S "
        timeInEast2 = a2
        timeInEast = str(a)

        Text1 = CustText(timeInEast, 30, (0, 0, 0), (255, 255, 255), 10, 10)
        Text3 = CustText(timeInEast2, 30, (0, 0, 0), (255, 255, 255), 10, 50)

        fenetre.blit(Text1.text, Text1.textRect)
        fenetre.blit(Text2.text, Text2.textRect)
        fenetre.blit(Text3.text, Text3.textRect)

        # Si quitter alors quitter
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                while True:
                    quit()
        if pygame.mouse.get_pressed()[2]:
            pyperclip.copy(a1.split(":")[0])
        if pygame.mouse.get_pressed()[0]:
            pyperclip.copy(a1.split(":")[1])

        # Actualisation de l'affichage
        pygame.display.flip()
        clock.tick(30)
if __name__ == '__main__':
    main()

【问题讨论】:

  • 是的,但是 url 在 Firefox 中打开。我想在我的 pygame 实例上有 url。模拟 Firefox,或者在 pygame 中使用“自定义”网络浏览器。
  • 在编程中,几乎没有什么事情是不可能的,但我认为你必须付出很多努力才能做到这一点。我认为没有简单的解决方案。
  • 好的,谢谢,我找到了替代解决方案。

标签: python google-chrome firefox pygame


【解决方案1】:

您可能会使用beautifulsoup 模块来抓取网站并提取图像,然后将其显示在您的游戏中。不过,使用此类网站上的图片可能存在版权问题。

答案here 显示使用beautifulsoup 从网站拉取图像并将它们保存为目录中的文件。尽管您可以将它们保存在游戏中并根据需要使用它们,但您不需要将它们保存到磁盘。我没有测试答案中的代码,所以不能证明它有效。我通过谷歌搜索“使用 beautifulsoup 从网站获取图片”找到了它。

【讨论】:

    猜你喜欢
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多