1

I have a Pygame programme and I found on internet a website with a live html object, that I whant in my pygame window. I'v tried lot's of moduls, but I have not found what I wanted.

Is it possible to have the entire window/onglet of Firefox or Chrome, or just a (div) html object.

Here is my pygame code :

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()
Rafo 554
  • 99
  • 5

2 Answers2

0

You could likely use the beautifulsoup module to scrape the website and pull out the image and then display it in your game. There may be copyright issues about using an image from a website like that though.

The answer here shows using beautifulsoup to pull the images from a website and save them as files in a directory. You do not need to save them to disk though you can just keep them in your game and use them/it as needed. I have not tested the code in the answer, so cannot attest that it works. I found it by googling 'get image from website with beautifulsoup'.

Glenn Mackintosh
  • 2,765
  • 1
  • 10
  • 18
0

You can use cef4pygame library to embed CEF into PyGame. Example:

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()

Install command: pip install CEF4pygame