可以用Python在pygame窗口中运行Firefox或Chrome吗?

vecaoik1  于 2023-08-01  发布在  Go
关注(0)|答案(2)|浏览(118)

我有一个Pygame程序,我在互联网上发现了一个带有实时HTML对象的网站,我在我的Pygame窗口中看到了它。我试了很多模块,但我没有找到我想要的。
有没有可能拥有Firefox或Chrome的整个window/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()

字符串

szqfcxe2

szqfcxe21#

您可能会使用beautifulsoup模块来抓取网站并提取图像,然后将其显示在游戏中。使用这样的网站上的图片可能会有版权问题。
答案here展示了如何使用beautifulsoup从网站中提取图像并将其保存为目录中的文件。您不需要将它们保存到磁盘,尽管您可以将它们保存在游戏中并根据需要使用它们/它。我没有测试过答案中的代码,所以不能证明它可以工作。我是在谷歌上搜索“从网站上用漂亮的汤获取图片”找到的。

new9mtju

new9mtju2#

您可以使用cef4pygame库将CEF嵌入PyGame。范例:

from CEF4pygame import CEFpygame,pygame
browser=CEFpygame(
    URL='YOUR_URL',
    VIEWPORT_SIZE=(800,500)
)

pygame.init()
display_width = 900
display_height = 600
display = pygame.display.set_mode((display_width,display_height))
run=1
while run:
    events=pygame.event.get()
    pygame.key.set_repeat(500,100)
    keys=pygame.key.get_pressed()
    alt_pressed=keys[1073742050]
    ctrl_pressed=keys[1073742048]
    shift_pressed=keys[pygame.K_LSHIFT]
    for event in events:
        if event.type == pygame.QUIT:
            run=0
        if event.type == pygame.MOUSEMOTION:
            browser.motion_at(event.pos[0]-50,event.pos[1]-70,alt=alt_pressed,shift=shift_pressed,control=ctrl_pressed)
        if event.type == pygame.MOUSEBUTTONDOWN:
            browser.mousedown_at(event.pos[0]-50,event.pos[1]-70,event.button,alt=alt_pressed,shift=shift_pressed,control=ctrl_pressed)
        if event.type == pygame.MOUSEBUTTONUP:
            browser.mouseup_at(event.pos[0]-50,event.pos[1]-70,event.button,alt=alt_pressed,shift=shift_pressed,control=ctrl_pressed)
        if event.type == pygame.KEYDOWN:
            browser.keydown(event.key,alt=alt_pressed,shift=shift_pressed,control=ctrl_pressed)
        if event.type == pygame.KEYUP:
            browser.keyup(event.key,alt=alt_pressed,shift=shift_pressed,control=ctrl_pressed)
    display.blit(browser.image, (50,70))
    pygame.display.update()

字符串
安装命令:pip install CEF4pygame

相关问题