PyGobject(八十五)GObject介绍(自定义对象与信号)

本文详细介绍了如何在Python中使用GObject进行对象的自定义,包括继承、信号定义与触发、属性的创建与控制以及属性变化的监测等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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





代码下载地址:http://download.youkuaiyun.com/detail/a87b01c14/9594728

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值