[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Re: New GUI
Marcus wanted an example of how events could be handled better.
I've worked up an example of how the mainline code could look, if the
event handling were done using a more VB-like structure.
import kamgui
# This example creates a screen with 2 buttons and 3 labels.
# -------------------------------------------------------------
# App level features handled with this code:
# -------------------------------------------------------------
# Clicking the buttons updates the status label.
# Moving the mouse updates the mousepos label.
# The fps label is updated when the global 'FPS' event is fired.
# --------------------------------------------------------------
# Engine level 'automatic' features:
# --------------------------------------------------------------
# The buttons automatically hilight when you hover the
# mouse over them, and unhilight when you leave.
# Clicking the button sets the focus to the button,
# and draws it with a border around it and darker text.
# Events that are targeted to a specific control, propagate
# up the ownership hierarchy until the event is handled.
# If no control is listening, the event is silently ignored.
# Events that are sent to no specific control,
# are broadcast to all 'listening' controls.
# To make a control listen, simply add the event name to the
# 'events' listbox for that control. In this example,
# the app is listening for 'MousePos' and 'FPS.'
# All these engine level features are handled behind the scenes.
class MyApp(kamgui.App):
events = ['MousePos', 'FPS']
def On_Initialize(self, e):
' Create the controls '
self.mousepos = kamgui.Label(parent = self,
fontcolor = kamgui.constants.YELLOW)
button = kamgui.Button(parent = self, pos = [100, 100],
name = 'DoNothing', caption = 'Do Nothing')
button = kamgui.Button(parent = self, pos = [150, 210],
name = 'DoSomething', caption = 'Do Something',
size = [200, 50])
self.fps = kamgui.Label(parent = self, text = 'FPS',
pos =(self.size[0], 0), align = 'r',
fontcolor = kamgui.constants.DKGREEN)
self.status = kamgui.Label(parent = self,
text = 'Welcome to KamGUI!',
pos = (0, self.size[1]), align = 'lb')
def On_Terminate(self, e):
' Quit the app'
print 'Quitting MyApp'
def On_FPS(self, e):
' Display FPS'
self.fps.SetText('%d FPS' % e.value)
def On_MousePos(self, e):
' Display current mouse position'
self.mousepos.SetText(e.pos)
def DoSomething_Click(self, e):
' Do something when button is clicked.'
self.status.SetText('Did something.')
def DoNothing_Click(self, e):
' Do nothing.'
self.status.SetText('Did nothing.')
MyApp().Run()