Why is this pygame-menu dropdown only displaying the first character?

85 Views Asked by At

I'm trying to add a dropdown to pygame_menu that displays the days of the month '1'-'31' (or '01'-'31') however when I select the dropdown while running, it displays just the leftmost character. The most concise code that will run is below

import pygame
import pygame_menu as pm
import sys

from pygame.locals import *

WIDTH, HEIGHT = 1100, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.init()


my_theme = pm.themes.THEME_DARK.copy()
my_theme.title = False
my_theme.background_color = color_opaque = (50, 100, 200)


setup_menu = pm.Menu('', WIDTH, HEIGHT)
days = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']


####
setup_menu.add.dropselect('Days',items=days, placeholder_add_to_selection_box=False)
####

def main():
    run = True
    clock = pygame.time.Clock()

    while run:
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == QUIT:
                run = False
                pygame.display.quit()
                pygame.quit
                sys.exit()
                break

            if setup_menu.is_enabled():
                setup_menu.update(pygame.event.get())
                setup_menu.mainloop(WIN)
        

            pygame.display.update()

if __name__ == "__main__":
    main() 

The above code produces the following

enter image description here

enter image description here

Any advice would be greatly appreciated. Thank you

1

There are 1 best solutions below

3
Joe On BEST ANSWER

The problem is with your days list. According to the documentation, you need to create a list of tuples:

The items of the DropSelect are:

items = [('Item1', a, b, c...), ('Item2', d, e, f...)]

  • The first element of the tuple is the display text that will be shown in the dropdown menu to represent the option.
  • The rest of the elements in the tuple can hold additional data associated with the option.

Also, note that if you create tuples with a single item, only the first character of each item will be displayed. For example:

days = [('Mon'), ('Tue'), ('Wed')]

Would display 'M', 'T' and 'W' on the dropdwon menu. So if you want to display the full text, add empty strings to each tuple:

days = [('Mon', ''), ('Tue', ''), ('Wed', '')]

Here is a working version of your code:

import pygame
import pygame_menu as pm
import sys

from pygame.locals import *

WIDTH, HEIGHT = 1100, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.init()


my_theme = pm.themes.THEME_DARK.copy()
my_theme.title = False
my_theme.background_color = color_opaque = (50, 100, 200)


setup_menu = pm.Menu('', WIDTH, HEIGHT)

# use a list of tuples instead
characters = [
    ('Kaboom Bot', 'Mech'), ('Impulsive Trickster', 'Demon'), ('Low-Flier', 'Dragon'), 
    ('Menagerie Mug', 'None'), ('Murcules', 'Murloc'), ('Tad', 'Murloc')]

####
setup_menu.add.dropselect('Characters',items=characters, placeholder_add_to_selection_box=False)
####

def main():
    run = True
    clock = pygame.time.Clock()

    while run:
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == QUIT:
                run = False
                pygame.display.quit()
                pygame.quit
                sys.exit()
                break

            if setup_menu.is_enabled():
                setup_menu.update(pygame.event.get())
                setup_menu.mainloop(WIN)
        

            pygame.display.update()

if __name__ == "__main__":
    main()