Python GTK+:创建自定义信号?

发布于 2024-08-17 21:35:02 字数 53 浏览 7 评论 0原文

是否可以在 Python GTK+ 中创建新信号?

我想要一个框架代码示例。

Is it possible to create new signals in Python GTK+ ?

I'd like a skeleton code example, please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄凉少年不暖心 2024-08-24 21:35:02

摘录:

创建您自己的信号

您可能想要的另一件事
定义子类化 GObject 时使用
自定义信号。您可以创建您的
可以发出自己的信号
您班级的用户可以连接到
他们。

当发出信号时,一组
将执行关闭操作。一个封闭
是回调的抽象
概念。闭包就是回调
本身(函数指针)、用户
数据(这将是最后一个参数
回调)和另一个函数
清理问题,这不会
本文档中进行了讨论。

就本文而言,您不需要
真的需要知道其中的区别
在回调和闭包之间所以
这两个术语都会被使用。但要
建议这并不完全
正确。

正如我们之前所说,当信号出现时
发出,一组闭包将被
被执行。其中一张是同一个
对于该类的所有实例
因此它的名字是:类闭包,
其他的是自定义用户
回调。请注意,并非所有
信号需要有一个类闭包
因为它是可选的。

来自,http://www.pygtk.org /articles/subclassing-gobject/sub-classing-gobject-in-python.htm,希望有所帮助。网站上有示例代码,此处,一个片段:

import pygtk
pygtk.require('2.0')
import gobject

class Car(gobject.GObject):
    __gproperties__ = {
        'fuel' : (gobject.TYPE_FLOAT, 'fuel of the car',
                  'amount of fuel that remains in the tank',
                  0, 60, 50, gobject.PARAM_READWRITE)
        }

    __gsignals__ = {
        'engine-started' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                            (gobject.TYPE_FLOAT,))
        }

    def __init__(self):
        gobject.GObject.__init__(self)
        self.fuel = 50

    def do_get_property(self, property):
        if property.name == 'fuel':
            return self.fuel
        else:
            raise AttributeError, 'unknown property %s' % property.name

    def do_set_property(self, property, value):
        if property.name == 'fuel':
            self.fuel = value
        else:
            raise AttributeError, 'unknown property %s' % property.name

    def do_engine_started(self, remaining_fuel):
        print '***** Beginning of class closure *****'
        print 'The engine is ready and we still have %f of fuel' % self.fuel
        print '***** End of class closure *****'

    def start(self):
        self.emit('engine-started', self.get_property('fuel'))

gobject.type_register(Car)

An excerpt:

Creating your own signals

The other thing you probably want to
use when subclassing GObject is define
custom signals. You can create your
own signals that can be emitted so
users of your class can connect to
them.

When a signal is emitted a set of
closures will be executed. A closure
is an abstraction of the callback
concept. A closure is the callback
itself (a function pointer), the user
data (it will be the last parameter to
the callback) and another function for
cleanup issues, which will not be
discussed in this document.

For the sake of this article you don't
really need to know the difference
between a callback and a closure so
both terms will be used. But be
advised that this is not totally
correct.

As we said before, when a signal is
emitted, a set of closures will be
executed. One of them is the same one
for all the instances of this class
and hence its name: the class closure,
and the other ones are custom user
callbacks. Note that not all the
signals need to have a class closure
because it is optional.

From, http://www.pygtk.org/articles/subclassing-gobject/sub-classing-gobject-in-python.htm, hope that helps. There is example code on the site and here , a snippet:

import pygtk
pygtk.require('2.0')
import gobject

class Car(gobject.GObject):
    __gproperties__ = {
        'fuel' : (gobject.TYPE_FLOAT, 'fuel of the car',
                  'amount of fuel that remains in the tank',
                  0, 60, 50, gobject.PARAM_READWRITE)
        }

    __gsignals__ = {
        'engine-started' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                            (gobject.TYPE_FLOAT,))
        }

    def __init__(self):
        gobject.GObject.__init__(self)
        self.fuel = 50

    def do_get_property(self, property):
        if property.name == 'fuel':
            return self.fuel
        else:
            raise AttributeError, 'unknown property %s' % property.name

    def do_set_property(self, property, value):
        if property.name == 'fuel':
            self.fuel = value
        else:
            raise AttributeError, 'unknown property %s' % property.name

    def do_engine_started(self, remaining_fuel):
        print '***** Beginning of class closure *****'
        print 'The engine is ready and we still have %f of fuel' % self.fuel
        print '***** End of class closure *****'

    def start(self):
        self.emit('engine-started', self.get_property('fuel'))

gobject.type_register(Car)
随风而去 2024-08-24 21:35:02

答案当然是正确的,但使用 不太容易出现问题:

import gobject

from pygtkhelpers.utils import gsignal

class Car(gobject.GObject):

    gsignal('engine-started', float) # yeah baby

The answers are correct of course, but using libraries is less prone to pains:

import gobject

from pygtkhelpers.utils import gsignal

class Car(gobject.GObject):

    gsignal('engine-started', float) # yeah baby
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文