Skip to content

Instantly share code, notes, and snippets.

@misaka4e21
Created January 5, 2013 18:34
Show Gist options
  • Save misaka4e21/4462991 to your computer and use it in GitHub Desktop.
Save misaka4e21/4462991 to your computer and use it in GitHub Desktop.
剛發現Python的with語句,很適合寫Clutter
#!/usr/bin/env python3
#coding=utf-8
import sys
from contextlib import contextmanager
from gi.repository import Clutter
@contextmanager
def mk(class_name,*args,**kwargs):
klass=getattr(Clutter,class_name)
if 'constructor' in kwargs:
klass=getattr(klass,kwargs['constructor'])
del kwargs['constructor']
obj=klass(*args,**kwargs)
yield obj
Clutter.init(sys.argv)
with mk('Stage',constructor='get_default') as stage:
with mk('Text') as label:
label.set_text('LZSB')
stage.add_actor(label)
stage.show()
Clutter.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment