[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Tutorial... GUI



On, Wed Sep 26, 2007, RR4CLB wrote:

[Example request]

I attached a small OcempGUI example script, which will create two
buttons on a pygame window and print their text to the console whenever
they receive focus. The code aligns them beneath each other using a
spacing of 10 pixels between them.
The TAB key cycles the input focus between the buttons.

Regards
Marcus
# Import the necessary ocempgui parts.
from ocempgui.widgets import *
from ocempgui.widgets.Constants import *

# object_focused will print a widget's text to the console.
def object_focused (gui_widget):
    print "Element %s is focused" % gui_widget.text

# Create the first button with the text "Button number one" and
# place it at screen offset 10, 10.
button_one = Button ("Button number one")
button_one.topleft = 10, 10

# Connect the focus event (for tab).
button_one.connect_signal (SIG_FOCUSED, object_focused, button_one)

# Connect the mouse enter event to the button to have some "hover"
# effect.
button_one.connect_signal (SIG_ENTER, object_focused, button_one)

# Create the second button with the text "Button number two" and
# place it 10 pixels beneath the first one.
button_two = Button ("Button number two")
button_two.topleft = button_one.left, button_one.bottom + 10

# Connect the focus event (for tab or mouse "hover") of the second button.
button_two.connect_signal (SIG_FOCUSED, object_focused, button_two)

# Connect the mouse movement event to the button to have some "hover"
# effect.
button_two.connect_signal (SIG_ENTER, object_focused, button_two)

# Create the renderer object.
re = Renderer ()

# Let the renderer create a pygame screen with 200 pixels width and 200
# pixels height.
re.create_screen (200, 200)

# Set the window title to "Two button demo".
re.title = "Two button demo"

# Set the screen background color to a grey RGB value.
re.color = (200, 200, 200)

# Add the both buttons.
re.add_widget (button_one, button_two)

# Start the main loop for drawing and event processing.
re.start ()

Attachment: pgp3Nu7tlQeKO.pgp
Description: PGP signature