#๐Ÿ”’ cant for the life of me update the theming of a pygame_gui panel in the event loop

5 messages ยท Page 1 of 1 (latest)

jolly pawn
#

in screen 1, the change of color works, it's before the main loop starts.
in screen 2, it just dont

the code : ```py
from typing import Tuple

import pygame
import pygame_gui
import random
import glob

if seed := input("entrez le seed : "): # seed
random.seed(seed)
isSpy = {"Y": True, "N": False}[input("รŠtes vous espion [Y/N]").strip().upper()] # spy or agent

pygame initialization

pygame.init()
size = [1000, 1000]
screen = pygame.display.set_mode(size, flags=pygame.SRCALPHA)
clk = pygame.time.Clock()

pygame gui setup

manager = pygame_gui.UIManager(size, "theme.json")
cell_padding = cpd = 5

random codenames grid

colors = {"@red": "#663333",
"@blue": "#333366",
"@gray": "#555555",
"@black": "#151515"}
grid_size = [5, 5]
cell_size = [i / j for i, j in zip(size, grid_size)]
grid = ["@red"] * 9 + ["@blue"] * 9 + ["@gray"] * 6 + ["@black"]
random.shuffle(grid)

random pokemon grid

poke_board = pygame.Surface(size, pygame.SRCALPHA)
mons = glob.glob("pokemonImages/*.png")
chosen_mons = random.choices(mons, k=25)

create cells

for n, c in enumerate(zip(grid, chosen_mons)):
n: int; c: Tuple[str, str] # type hint
cell = pygame_gui.elements.UIPanel(pygame.Rect(cell_size[0] * (n % 5) + cpd, cell_size[1] * (n // 5) + cpd, cell_size[0] - 2 * cpd, cell_size[1] - 2 * cpd), manager=manager, object_id=pygame_gui.core.ObjectID(object_id="#cell", class_id=c[0]))
if isSpy:
cell.update_theming("{"colours" : {"dark_bg": "%s"}}" % colors[c[0]])
button_red = pygame_gui.elements.UIButton(pygame.Rect(cpd, cpd, 0.6 * cell_size[0], 0.3 * cell_size[1] - cpd * 2), "Rouge", manager, container=cell, tool_tip_text="Deviner en tant qu'agent rouge.", object_id="buttonRed")
button_blue = pygame_gui.elements.UIButton(pygame.Rect(0.6 * cell_size[0] + cpd * 2, 0.3 * cell_size[1], 0.3 * cell_size[0] - cpd, 0.6 * cell_size[1] + cpd), "Bleu", manager, container=cell, tool_tip_text="Deviner en tant qu'agent bleu.", object_id="buttonBlue")
pokemon_image = pygame_gui.elements.UIImage(pygame.Rect(cpd, 0.3 * cell_size[1] + cpd * 2, 0.6 * cell_size[0] - cpd * 2, 0.6 * cell_size[1] - cpd * 2), pygame.transform.scale(pygame.image.load(c[1]), [0.6 * p - cpd * 2 for p in cell_size]), manager, container=cell)
pokemon_name = pygame_gui.elements.UILabel(pygame.Rect(0, 5 * cpd, 0.6 * cell_size[0], 0.4 * cell_size[1]), c[1].replace("pokemonImages\", "").replace(".png", ""), manager, container=cell, anchors={"top": "top", "centerx": "centerx", "centerx_target": button_red}, object_id="pokemonName")

main loop

while True:

# events and frame setup
for e in pygame.event.get():
    if e.type == pygame.QUIT:
        exit()
    if e.type == pygame_gui.UI_BUTTON_PRESSED:
        button: pygame_gui.elements.UIButton = e.ui_element
        cell: pygame_gui.elements.UIPanel = e.ui_element.ui_container.get_container()
        cell.update_theming("{\"colours\" : {\"dark_bg\": \"%s\"}}" % colors[cell.class_ids[0]])
        button.pressed = True
        button.disable()

    manager.process_events(e)

dt = clk.tick(60)
manager.update(dt)
screen.fill([35, 35, 35, 255])

manager.draw_ui(screen)

pygame.display.update()  # print the frame
empty cloakBOT
#

@jolly pawn

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

jolly pawn
#

oh yeah here's the theme.json as well

{
  "#cell": {
    "colours": {
      "dark_bg": "#333333",
      "normal_border": "#DDDDDD"
    }
  },
  "buttonBlue": {
    "colours": {
      "hovered_bg": "#2222AA",
      "hovered_border": "#DDDDDD",
      "normal_bg": "#222266",
      "normal_border": "#DDDDDD"
    }
  },
  "buttonRed": {
    "colours": {
      "hovered_bg": "#991515",
      "hovered_border": "#DDDDDD",
      "normal_bg": "#662222",
      "normal_border": "#DDDDDD"
    }
  },
  "pokemonName": {
    "font": {
      "size": 16
    }
  }
}
empty cloakBOT
#

@jolly pawn

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.