Tuesday, May 15, 2012

GtkClutterEmbed the Pythonic Way!

The following is a simple clutter-gtk program in python that has an animatable widget. The script is for Gtk+3.0 not Gtk+2.0.

#!/usr/bin/python


from gi.repository import Gtk, Clutter, GtkClutter


storedWidth = 0
storedHeight = 0


def on_clicked(b, actor):
    global storedWidth, storedHeight
    if actor.get_width() != actor.get_stage().get_width():
        storedWidth = actor.get_width()
        storedHeight = actor.get_height()
        width = actor.get_stage().get_width()
        height = actor.get_stage().get_height()
    else:
        width = storedWidth
        height = storedHeight
        
    actor.animatev(Clutter.AnimationMode.EASE_OUT_BOUNCE,
                   500, ["width", "height"], [width, height])
    
GtkClutter.init([])


bttn = Gtk.Button("Foobar")
txtv = Gtk.TextView()
scwn = Gtk.ScrolledWindow()
scwn.add(txtv)
scwn.show_all()
bact = GtkClutter.Actor(contents = scwn)
embd = GtkClutter.Embed()
stge = embd.get_stage()
col = Clutter.Color()
col.from_string("#000")
stge.set_color(col)
vbox = Gtk.VBox()
wndw = Gtk.Window(width_request = 400, height_request = 300)


stge.add_actor(bact)
vbox.pack_start(embd, True, True, 0)
vbox.pack_start(bttn, False, True, 0)
wndw.add(vbox)


wndw.show_all()


bttn.connect("clicked", on_clicked, bact)
wndw.connect("destroy", lambda w: Gtk.main_quit())


Gtk.main()