pygame is
Simple DirectMedia Layer is
Site Swing

MenuClass - 1.0.0

Scott Barlow (spectre5)



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 provided and then see the more powerful ''.

In '', 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.


Initial Release


Home Page:


click to view original size


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

If you wish to leave a comment with your 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/
> # @param initChoice Optional initial choice, defaults to first in list.
< buttonList):
> buttonList, initChoice=0):
> # Remember the initial menu choice, defaults to first in list
> self.init = initChoice
< self.selection = 0
> self.selection = self.init
$ diff MenuClass_V1.0.3.orig/
< ('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 != '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, runs on OS X, but 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.

our projects welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.
recent releases
Feb 21, 2017

Jan 31, 2017

Jan 24, 2017

Jan 18, 2017

Jan 7, 2017

Dec 30, 2016

Dec 8, 2016

Nov 28, 2016

Nov 27, 2016

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