GObject是最基础类型,提供了GTK+和Pango和其他库中的所有对象类型的公共属性和方法。GObject.Object类提供了对象的构造和销毁,属性访问方法,以及信号支持的方法。
自定义对象(继承自GObject.GObject)
from gi.repository import GObject
class MyObject(GObject.GObject):
def __init__(self):
GObject.GObject.__init__(self)
信号
定义信号
放在__gsignals__字典中,key为信号名
然后定义虚方法,方法名为do_+信号名。当信号被激活时,执行此方法
class MyObject(GObject.GObject):
__gsignals__ = {
'my_signal': (GObject.SIGNAL_RUN_FIRST, None,
(int,))
}
def do_my_signal(self, arg):
print("class method for 'my_signal' called with argument", arg)
触发信号
my_obj = MyObject()
my_obj.emit("my_signal", 42)
# emit the signal "my_signal", with the argument 42
属性
创建新属性
from gi.repository import GObject
class MyObject(GObject.GObject):
foo = GObject.property(type=str, default='bar')
property_float = GObject.property(type=float)
def __init__(self):
GObject.GObject.__init__(self)
控制读写属性
foo = GObject.property(type=str, flags = GObject.PARAM_READABLE) # not writable
bar = GObject.property(type=str, flags = GObject.PARAM_WRITABLE) # not readable
使用装饰器创建只读属性
from gi.repository import GObject
class MyObject(GObject.GObject):
def __init__(self):
GObject.GObject.__init__(self)
@GObject.property
def readonly(self):
return 'This is read-only.'
my_object = MyObject()
print(my_object.readonly)
print(my_object.get_property("readonly"))
定义最小和最大值
from gi.repository import GObject
class MyObject(GObject.GObject):
__gproperties__ = {
"int-prop": (int, # type
"integer prop", # nick
"A property that contains an integer", # blurb
1, # min
5, # max
2, # default
GObject.ParamFlags.READWRITE # flags
),
}
def __init__(self):
GObject.GObject.__init__(self)
self.int_prop = 2
def do_get_property(self, prop):
if prop.name == 'int-prop':
return self.int_prop
else:
raise AttributeError('unknown property %s' % prop.name)
def do_set_property(self, prop, value):
if prop.name == 'int-prop':
self.int_prop = value
else:
raise AttributeError('unknown property %s' % prop.name)
obj = MyObject()
print(obj.int_prop)
监测属性变化
当属性有变化,“notify::property_name”信号将被发送
#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/16
# section 133
TITLE = "GObject"
DESCRIPTION = ""
from gi.repository import GObject
class MyObject(GObject.GObject):
__gproperties__ = {
"int-prop": (int, # type
"integer prop", # nick
"A property that contains an integer", # blurb
1, # min
5, # max
2, # default
GObject.ParamFlags.READWRITE # flags
),
}
def __init__(self):
GObject.GObject.__init__(self)
self.int_prop = 2
self.connect("notify::int-prop", self.on_notify)
def do_get_property(self, prop):
if prop.name == 'int-prop':
return self.int_prop
else:
raise AttributeError('unknown property %s' % prop.name)
def do_set_property(self, prop, value):
if prop.name == 'int-prop':
self.int_prop = value
else:
raise AttributeError('unknown property %s' % prop.name)
@staticmethod
def on_notify(obj, gparamstring):
print("new value=%d" % obj.get_property(gparamstring.name))
def main():
my_object = MyObject()
print(my_object.int_prop)
my_object.set_property("int-prop", 3) # on_notify will be called
if __name__ == '__main__':
main()
输出
2
new value=3