I'm trying to use pygame_gui to ask some info to my app users, I'm able to display the entry box, the label and the button correctly but I can't click in the entry box to write and I don't know how to get the inserted input when pressing the "start" button. This is how I've structured mygame: I have a game and a main file that I use to launch the code and then my states. Like this:
game.py
class Game(object):
def __init__(self, screen, states, start_state):
self.done = False
self.screen = screen
self.clock = pygame.time.Clock()
self.fps = 60
self.states = states
self.state_name = start_state
self.state = self.states[self.state_name]
def event_loop(self):
for event in pygame.event.get():
self.state.get_event(event)
def flip_state(self):
current_state = self.state_name
next_state = self.state.next_state
self.state.done = False
self.state_name = next_state
persistent = self.state.persist
self.state = self.states[self.state_name]
self.state.startup(persistent)
def update(self, dt):
if self.state.quit:
self.done = True
elif self.state.done:
self.flip_state()
self.state.update(dt)
def draw(self):
self.state.draw(self.screen)
def run(self):
while not self.done:
dt = self.clock.tick(self.fps)
self.event_loop()
self.update(dt)
self.draw()
pygame.display.update()
main.py
import pygame
import sys
from data.states.info import Info
from game import Game
pygame.init()
pygame.mouse.set_visible(False)
screen = pygame.display.set_mode((1920, 1080))
states = {
"INFO": Info(),
}
game = Game(screen, states, "INFO")
game.run()
pygame.quit()
sys.exit()
info.py
import pygame
from .base import BaseState
import pygame_gui
from pygame_gui import UIManager, PackageResource
from pygame_gui.elements import UIWindow
from pygame_gui.elements import UIButton
from pygame_gui.elements import UITextEntryLine
from pygame_gui.elements import UIDropDownMenu
from pygame_gui.elements import UILabel
from pygame_gui.windows import UIMessageWindow
class Info(BaseState):
def __init__(self):
super(Info, self).__init__()
self.ui_manager = UIManager((self.screen_rect.width, self.screen_rect.height))
pygame.mouse.set_visible(True)
self.entry = UITextEntryLine(pygame.Rect(50, 100, 200, 50), self.ui_manager)
self.label = UILabel(
pygame.Rect(50, 50, 50), "Enter your name:", self.ui_manager
)
self.start_button = UIButton(
pygame.Rect((50, 450), (400, 35)),
"Start",
self.ui_manager,
object_id="#start_button",
)
def get_event(self, event):
if event.type == pygame.QUIT:
self.quit = True
def draw(self, surface):
surface.fill(pygame.Color("black"))
self.ui_manager.draw_ui(surface)
How can I correctly use pygame_gui with this structure?