pythontextboxthemespygame-gui

how to change the text color of a pygame gui textbox


I have this python code which displays a screen with a textbox in the middle that print its text when pressed enter:

import pygame_gui, pygame

pygame.init()
screen = pygame.display.set_mode((500,500))

clock = pygame.time.Clock()
manager = pygame_gui.UIManager((500,500))
manager.set_window_resolution((500,500))

rect_help = pygame.Rect(200, 200, 100, 50)
text_box = pygame_gui.elements.UITextEntryLine(relative_rect=rect_help, manager=manager, object_id='#text')

run=True
while run:
    UI_refresh = clock.tick(30)/1000

    for event in pygame.event.get():
        if event.type==pygame.QUIT: run=False
        elif event.type==pygame_gui.UI_TEXT_ENTRY_FINISHED and event.ui_object_id == '#text':
            print(text_box.text)

        manager.process_events(event)
    manager.update(UI_refresh)

    screen.fill('white')
    manager.draw_ui(screen)

    pygame.display.update()

The code uses pygame and pygame_gui. The textbox has a gray background and the color of the text is white, I want to change these colors to something else - how can I do it?


Solution

  • import pygame_gui, pygame
    
    pygame.init()
    screen = pygame.display.set_mode((500,500))
    
    clock = pygame.time.Clock()
    bg_hex = "#000000"
    text_hex = "#FFFFFF"
    theme = {"text_entry_box":{"colours":
             {
                 "dark_bg":bg_hex,
                 "normal_text":text_hex
             }
            }}
    manager = pygame_gui.UIManager((500,500),theme)
    manager.set_window_resolution((500,500))
    
    rect_help = pygame.Rect(200, 200, 100, 50)
    text_box = pygame_gui.elements.UITextEntryLine(relative_rect=rect_help, manager=manager, object_id='#text')
    
    run=True
    while run:
        UI_refresh = clock.tick(30)/1000
    
        for event in pygame.event.get():
            if event.type==pygame.QUIT: run=False
            elif event.type==pygame_gui.UI_TEXT_ENTRY_FINISHED and event.ui_object_id == '#text':
                print(text_box.text)
    
            manager.process_events(event)
        manager.update(UI_refresh)
    
        screen.fill('white')
        manager.draw_ui(screen)
    
        pygame.display.update()
    

    I got the text entry box from UITextEntryBox Theming Parameters and how to apply the theme in you code from Pygame Gui Tutorial – Complete Guide (Thanks to @PM 77-1)