Apologies, there were a few errors in my sample. Attached is a fixed
version.
If anyone has any tips about how to close the menu, I'd love to hear. I can
close it with the ESC key, but not by selecting an item.
import pygame, sys
from pygame.locals import *
import pygameMenu # This imports classes and other things
from pygameMenu.locals import * # Import constants (like actions)
'''
Testing pygame-menu. Keyboard 1 launches menu1, keyboard 2 launches menu2.
'''
# set up pygame
pygame.init()
clock = pygame.time.Clock()
surface = pygame.display.set_mode((800, 600) )
pygame.display.set_caption('test')
# background for menus
def menu_bg():
"""
Background color of the main menu, on this function user can plot
images, play sounds, etc.
"""
surface.fill((40, 0, 40))
# triggered when menu item selected
def item_selected(arg):
print "item selected %s" % arg
menu1.disable() # should close menu1? But isn't...
menu2.disable() # should close menu2? But isn't...
print menu2.is_enabled()
#menu2.reset(100)
#menu2.reset(1)
#create menu1
menu1 = pygameMenu.Menu(surface,
bgfun=menu_bg,
enabled=False,
font=pygameMenu.fonts.FONT_NEVIS,
menu_alpha=90,
#onclose=PYGAME_MENU_CLOSE,
onclose=PYGAME_MENU_RESET, # If this menu closes
(press ESC) back to main
title='Main Menu',
title_offsety=5,
window_height=800,
window_width=600
)
menu1.add_option('choice1', item_selected, "item1")
menu1.add_option('choice2', item_selected, "item2")
menu1.add_option('choice3', item_selected, "item3")
menu1.add_option('Exit', PYGAME_MENU_BACK)
#create menu2
menu2 = pygameMenu.Menu(surface,
bgfun=menu_bg,
enabled=False,
font=pygameMenu.fonts.FONT_NEVIS,
menu_alpha=90,
#onclose=PYGAME_MENU_CLOSE,
#onclose=PYGAME_MENU_RESET,
onclose=PYGAME_MENU_RESET, # If this menu closes
(press ESC) back to main
#onreturn=change_color_bg, # Action when pressing
return on a element
title='Menu 2',
title_offsety=5,
window_height=800,
window_width=600,
menu_height=400,
menu_width=600
)
menu2.add_option('menu2 choice1', item_selected, "itemmmm1")
menu2.add_option('menu2 pygame_menu_back', PYGAME_MENU_BACK)
menu2.add_option('menu2 pygame_menu_close', PYGAME_MENU_CLOSE)
menu2.add_option('menu2 pygame_menu_exit', PYGAME_MENU_EXIT)
# run the game loop
while True:
clock.tick(60)
# fill the bg and draw some text
surface.fill((255,255,255))
font1 = pygame.font.SysFont(None, 48)
text = font1.render('The Main Window', True, (0,255,0), (255,0,0))
textRect = text.get_rect()
textRect.centerx = surface.get_rect().centerx
textRect.centery = surface.get_rect().centery
surface.blit(text, textRect)
pygame.display.update()
events = pygame.event.get()
# Execute main from principal menu if is enabled
menu1.mainloop(events)
menu2.mainloop(events)
for event in events:
if event.type == KEYDOWN:
# open menu1 if they press the 1 key
if event.key == 49:
print "enabling menu1!"
menu1.enable()
# open menu2 if they press the 2 key
elif event.key == 50:
print "enabling menu2!"
menu2.enable()
print event.key
if event.type == QUIT:
pygame.quit()
sys.exit()