Should it be just one UIManager in pygame_gui?

Viewed 328

I'm writing a project using pygame-gui. I need a class Chart which is a box that would be filled with some text, drawing, etc. So I create a different surface for it. In its subclass ClassChart(Chart) I place an UITextEntryLine.

The problem is that using a new manager (relating to its surface) this entry line doesn't work. If I use the manager from the main class (where I create the window) everything works. So how to make my entry line work with the new manager and is it worth it? Is it ok to use the same manager for the whole project?

My main:

FPS = 60
windowWidth = 900
windowHeight = 900
clock = pygame.time.Clock()

pygame.init()

windowSurface = pygame.display.set_mode((windowWidth, windowHeight))
manager = gui.UIManager((windowWidth, windowHeight))
background = pygame.Surface((windowWidth, windowHeight))
background.fill(Colors.WHITE)

chart = ClassChart(windowSurface, manager, x=100, y=100, name="TEst")

running = True
while running:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    manager.process_events(event)

    windowSurface.blit(background, (0, 0))
    chart.draw()

    manager.update(FPS)
    manager.draw_ui(windowSurface)
    pygame.display.update()
    clock.tick(FPS)

My ClassChart (commented line shows how to work with the manager from main)

class ClassChart(Chart):
    def __init__(self,
                 parentSurface,
                 manager,
                 width=160,
                 height=200,
                 boundColor=Colors.BLACK,
                 x=0, y=0, thickness=2,
                 name="Class",
                 backgroundColor=Colors.LIGHT_LIGHT_GREY, ):
        super().__init__(parentSurface, width, height, boundColor, x, y, thickness)
        self.manager = gui.UIManager((self.width, self.height))
        self.name = gui.elements.UITextEntryLine(pygame.Rect((0, 0), (self.width, self.height)), self.manager)
        # self.name = gui.elements.UITextEntryLine(pygame.Rect((self.x, self.y), (self.width, self.height)), manager)
        self.nameSize = self.height // 10
        self.fieldSize = (self.height - self.nameSize) // 2
        self.name.set_text(name)

    def draw(self):
        super(ClassChart, self).draw()
        pygame.draw.line(self.surface, self.boundColor, (0, self.nameSize),
                         (self.width, self.nameSize), self.thickness)
        pygame.draw.line(self.surface, self.boundColor, (0, self.nameSize + self.fieldSize),
                         (self.width, self.nameSize + self.fieldSize), self.thickness)
        self.manager.update(60)
        self.manager.draw_ui(self.surface)
        self.blit()
1 Answers

I used UIManager in the main pygame python script:

import NewGUI
...
class Testing:
    def __init__(self):
        pygame.init()
        ...
        self.manager = pygame_gui.UIManager(size, "quick_theme.json")
        self.textBox = NewGUI.TextBox(manager=self.manager)
        ...
    def run(self):
        while self.is_running:
            time_delta = self.clock.tick(60) / 1000.0
            ... # the part with keyboard input and use like self.textBox= NewGUI.TextBox(manager=self.manager)

if __name__ == '__main__':
    app = Testing()
    app.run() 

I used another python script with the class, for example, NewGUI.py:

from asyncio.windows_events import NULL
import pygame
import pygame_gui
from pygame_gui import *

from pygame_gui import UIManager, PackageResource

from pygame_gui.elements import UIWindow
from pygame_gui.elements import UIButton
from pygame_gui.elements import UIHorizontalSlider
from pygame_gui.elements import UITextEntryLine
from pygame_gui.elements import UIDropDownMenu
from pygame_gui.elements import UIScreenSpaceHealthBar
from pygame_gui.elements import UILabel
from pygame_gui.elements import UIImage
from pygame_gui.elements import UIPanel
from pygame_gui.elements import UISelectionList

from pygame_gui.windows import UIMessageWindow

one example of class in this script:

class TextBox(pygame_gui.elements.UIWindow):
    def __init__(self, manager):
        super().__init__(pygame.Rect((600, 350), (640, 480)),
                            manager,
                            window_display_title='Textbox Info',
                            object_id="#obj_id_for_json_sudtom_theme")

        self.text_box = pygame_gui.elements.UITextBox(
                '... ',
                pygame.Rect((0, 0), (610, 440)),
                manager=manager,
                container=self,
                parent_element=self)
        self.text_box.set_active_effect(pygame_gui.TEXT_EFFECT_TYPING_APPEAR)
        self.text_box.rebuild()

    def process_event(self, event):
        handled = super().process_event(event)
Related