python clutter example (clickable buttons)

532

Example Clutter program which will load a png and create a clickable button out of it.

import clutter

#create a clutter stage and set the display size stage = clutter.Stage()
stage.set_size(400, 400)

#load the image for the buttons
img=clutter.cogl.texture_new_from_file('button.png',clutter.cogl.TEXTURE_NO_SLICING, clutter.cogl.PIXEL_FORMAT_ANY)

#example create button from class start

class button(clutter.Texture):
def __init__(self,id,row=1,padding=10):
clutter.Texture.__init__(self)
self.row=row
self.id=id
self.set_size (100,100)
self.set_position (id*100,self.row*100)
self.set_cogl_texture(img)
self.set_reactive(True)
#call click method on button clicked
self.connect("button-press-event",self.clicked)
def clicked(self,stage, event):
print "class click="+str(self.id)+" row "+str(self.row)

#example class with collection of button class class buttons:
def __init__(self):
self.buttonlist=[]
self.count=0
for i in range(0,10):
self.buttonlist.append(button(self.count,row=2))
#stage.add(self.buttonlist[self.count])
self.count+=1
#iter method so we can step through all buttons in a for loop
def __iter__(self):
for i in self.buttonlist:
yield i
#append a new button
def append(self,btton):
self.buttonlist.append(self.count)
self.count+=1

#crate instance of buttons class
#append buttons class to stage
buttonlist2=buttons()
for b in buttonlist2:
stage.add(b)

#example of creating button with function calls end


#show everything in the stage
stage.show_all()
stage.connect("destroy",clutter.main_quit)
#main clutter loop
clutter.main()
 

Test Image i used if anyone needs it.