pygame is
Python
Simple DirectMedia Layer
 
 
pygame.org is
Site Swing
MenuClass

MenuClass - 1.0.3

Scott Barlow (spectre5)

Tags:

Description

This is a menu class written for pygame/Python. The menu is designed to work with a program using a finite state machine (but it could also be easily modified to have the 'buttons' return functions). The menu 'buttons' contain a 'state' (a state could really be anything you want) and this 'state' is what is returned when the user selects/presses the button. The program controlling the menu can then act on this returned state as required. This helps to write non-blocking code.

The menu can have text buttons, image buttons (that get highlighted on all sides to detect which is selected), or any combination of the two.

The menu is flexible and can be dynamically changed. The 'buttons' will auto-magically update themselves the next time they are drawn to the screen (via the update method, which calls the draw method). The draw method should not be called itself. 'Buttons' can be added or removed at any time.

The menu can be positioned by the top left corner (a rectangle containing all buttons is what gets moved). It can be changed to center the entire menu (i.e. center that containing rectangle) on that same position coordinate. Or the user can center the entire menu on the self.draw_surface. Note that if the pygame screen is given to the menu, then the entire window will be available to be drawn to. But if the user gives the menu another pygame surface, then that surface itself will need to be blitted to the pygame screen at some point. Furthermore, the user can align the buttons to align on the left, to be centerd, or to align themselves on the right. Also, they can be aligned vertically on the top, center, or bottom.

The user can dynamically change the colors of the font/highlights, the padding between buttons (left/right and top/bottom), the thickness of the highlight around image buttons, and the orientation of the menu (if the 'buttons' will be stacked top to bottom ('vertical') or left to right ('horizontal').

The best way to figure out the menu is to tinker around with it. Check out the basic menu with the 'example_simple.py' example provided and then see the more powerful 'example.py'.

In 'example.py', use 'r' to remove any button you select. Dynamically add buttons on 'Menu 3' via the 'Add' button and see the flexibility of the menu via the 'Rand Colors' and 'Rand Config' buttons which will randomly change the colors and other parameters of the menu (run the example from the console to see the output - the newly assigned parameters).

Lastly, all files are heavily commented. Read the top of each file for the general information about the file and how to use it.

Note: A doxygen config file is also included. To get the doxygen output, simply type 'doxygen doxy' from the folders location.

Last Note: I believe that this code would work with Windows/Mac, but I only have Linux and I have successfully run it on several different Linux distros.

Changes

Very minor updates and a bug fix. Changed to a zip file for windows users unfamiliar with .tar.gz files.

Links

Home Page: http://code.google.com/p/python-pygame-menu-class/
Source: http://python-pygame-menu-class.googlecode.com/files/MenuClass_V1.0.3.zip

Screenshot


click to view original size

Releases

MenuClass - 1.0.3 - Aug 24, 2009
MenuClass - 1.0.0 - Aug 22, 2009

Pygame.org account Comments

If you wish to leave a comment with your pygame.org account, please sign in first.

November 29, 2010 5:26pm - Robert Leachman - nickname: (quazar)
Got it to work with the mouse, yeah baby! I don't know if it's a good patch or not, fresh hack and seems ugly.
November 29, 2010 3:45pm - Robert Leachman - nickname: (quazar)
Works well for me on OS X, and thanks!

I want to offer Easy-Normal-Hard and default to Normal, so needed to set the starting menu choice. I hope this diff posts correctly and helps somebody:

$ diff MenuClass_V1.0.3.orig/menu.py menu.py
135a136
> # @param initChoice Optional initial choice, defaults to first in list.
140c141
< buttonList):
---
> buttonList, initChoice=0):
179a181,183
>
> # Remember the initial menu choice, defaults to first in list
> self.init = initChoice
638c642
< self.selection = 0
---
> self.selection = self.init
$ diff example_simple.py MenuClass_V1.0.3.orig/example_simple.py
69c69
< ('Exit', 4, None)],1)
---
> ('Exit', 4, None)])
July 30, 2010 11:10am - Eugenia Gabrielova - nickname: (yayscience) - 5/5
Thanks, it looks and works great. Running on Linux so no problems.

- Genia
August 22, 2009 4:14pm - Scott Barlow - nickname: (spectre5)
Err...obviously the line:
os.environ['SDL_VIDEO_WINDOW_POS'] = str(x_) + "," + str(y_)

in my previous post is supposed to be indented, I guess these comments don't keep leading spaces?
August 22, 2009 4:13pm - Scott Barlow - nickname: (spectre5)
Thanks! And thanks for checking it out on OS X. Instead of commenting line 69 out, I believe you could change the line to be:

if os.name != 'mac':
os.environ['SDL_VIDEO_WINDOW_POS'] = str(x_) + "," + str(y_)

However, many users would probably prefer to just comment-out or delete that line since I assume that the line 63 (os.environ['SDL_VIDEO_CENTERED'] = '1') did work for you? Line 63 would center the window on the screen which is probably better for most users anyways.

Has anyone had any problems with windows?
August 22, 2009 2:19pm - matthew smith - nickname: (durandal) - 4/5
Very nice demo. By the way, example_simple.py runs on OS X, but example.py will not work unless you comment out line 69, 'os.environ['SDL_VIDEO_WINDOW_POS'] = str(x_) + "," + str(y_)'. Otherwise you get a bus error.
spotlight

 
our projects
pygame.org welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.
 
recent releases
Sep 17, 2014

Sep 9, 2014

Sep 8, 2014

Sep 7, 2014


Sep 5, 2014

Aug 26, 2014

Aug 22, 2014

Aug 21, 2014


Aug 18, 2014

Aug 2, 2014

... more!
 
for pygame related questions, comments, and suggestions, please see help (lists, irc)