小部件之间的自定义信号

发布于 2024-12-09 05:07:05 字数 3538 浏览 0 评论 0原文

尝试将信号从一个 gtk.EventBox 子级发送到另一个。

在 init HeadMode(第 75 行)上出现错误:

类型错误:未知信号名称:消息发送

为什么?

#!/usr/bin/env python
# -*- coding: utf8 -*-

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


def Destroy(widget):
    gtk.main_quit()


class CustomEventBox(gtk.EventBox):
    def __init__(self):
        super(CustomEventBox, self).__init__()
        self.press_hid = self.connect("button_press_event", self.on_press)
        self.release_hid = self.connect("button_release_event", self.on_release)
        self.set_custom_color()
        self.show()

    def on_press(self, widget, event):
        print "v: " + widget.label.get_text()
        widget.pressed()

    def on_release(self, widget, event):
        print "^: " + widget.label.get_text()
        widget.released()

    def pressed(self):
        pass

    def released(self):
        pass

    def set_custom_color(self):
        pass


class OrderBox(CustomEventBox):
    def __init__(self, label_text):
        super(OrderBox, self).__init__()
        self.label = gtk.Label(label_text)
        self.add(self.label)
        self.label.show()

    def set_custom_color(self):
        bg = self.get_colormap().alloc_color('white')
        self.modify_bg(gtk.STATE_NORMAL, bg)
        self.emit('message-send', 'normal')

    released = set_custom_color

    def pressed(self):
        bg = self.get_colormap().alloc_color('green')
        self.modify_bg(gtk.STATE_NORMAL, bg)
        self.emit('message-send', 'pressed')

gobject.type_register(OrderBox)
gobject.signal_new('message-send', OrderBox, gobject.SIGNAL_RUN_FIRST | gobject.SIGNAL_ACTION,
                   gobject.TYPE_NONE, (gobject.TYPE_STRING, ))


class HeadMode(CustomEventBox):
    def __init__(self, mode):
        super(HeadMode, self).__init__()
        self.mode = mode
        self.label = gtk.Label('')
        align = gtk.Alignment(0.1, 0.5, 0, 0) 
        align.add(self.label)
        align.show()
        self.add(align)
        self.set_custom_mode(self.mode)
        print gobject.signal_list_names(OrderBox)
        self.connect('message-send', self.on_message)

    def on_message(self, widget):
        print widget

    def set_custom_mode(self, mode, data=None):
        self.label.set_label(mode)

# gobject.type_register(HeadMode)


class MainWindow(object):
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Signal Demo")
        window.connect("destroy", Destroy)
        window.set_border_width(8)
        vbox = gtk.VBox()
        window.add(vbox)
        head = gtk.HBox()
        head.pack_start(HeadMode('start'), False, True, 4)
        head.show()
        vbox.pack_start(head, False, True, 4) 
        table = gtk.Table(3, 3, False)
        vbox.pack_start(table)
        vbox.show()

        for cx in range(3):
            for cy in range(3):
                if cy < 3 or (cy == 3 and cx <=5):
                    label_text = "11300%i0%i" % (cx+1, cy+1)
                else:
                    label_text = ""

                event_box = OrderBox(label_text)
                table.attach(event_box, cx, cx+1, cy, cy+1,
                          xpadding=4, ypadding=4)
                # More things you need an X window for ...
                event_box.realize()
        table.show()
#        window.fullscreen()
        window.show()


def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    MainWindow()
    main()

try to send signal from one gtk.EventBox child to another.

on init HeadMode (line 75) got error:

TypeError : unknown signal name: message-send

why?

#!/usr/bin/env python
# -*- coding: utf8 -*-

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


def Destroy(widget):
    gtk.main_quit()


class CustomEventBox(gtk.EventBox):
    def __init__(self):
        super(CustomEventBox, self).__init__()
        self.press_hid = self.connect("button_press_event", self.on_press)
        self.release_hid = self.connect("button_release_event", self.on_release)
        self.set_custom_color()
        self.show()

    def on_press(self, widget, event):
        print "v: " + widget.label.get_text()
        widget.pressed()

    def on_release(self, widget, event):
        print "^: " + widget.label.get_text()
        widget.released()

    def pressed(self):
        pass

    def released(self):
        pass

    def set_custom_color(self):
        pass


class OrderBox(CustomEventBox):
    def __init__(self, label_text):
        super(OrderBox, self).__init__()
        self.label = gtk.Label(label_text)
        self.add(self.label)
        self.label.show()

    def set_custom_color(self):
        bg = self.get_colormap().alloc_color('white')
        self.modify_bg(gtk.STATE_NORMAL, bg)
        self.emit('message-send', 'normal')

    released = set_custom_color

    def pressed(self):
        bg = self.get_colormap().alloc_color('green')
        self.modify_bg(gtk.STATE_NORMAL, bg)
        self.emit('message-send', 'pressed')

gobject.type_register(OrderBox)
gobject.signal_new('message-send', OrderBox, gobject.SIGNAL_RUN_FIRST | gobject.SIGNAL_ACTION,
                   gobject.TYPE_NONE, (gobject.TYPE_STRING, ))


class HeadMode(CustomEventBox):
    def __init__(self, mode):
        super(HeadMode, self).__init__()
        self.mode = mode
        self.label = gtk.Label('')
        align = gtk.Alignment(0.1, 0.5, 0, 0) 
        align.add(self.label)
        align.show()
        self.add(align)
        self.set_custom_mode(self.mode)
        print gobject.signal_list_names(OrderBox)
        self.connect('message-send', self.on_message)

    def on_message(self, widget):
        print widget

    def set_custom_mode(self, mode, data=None):
        self.label.set_label(mode)

# gobject.type_register(HeadMode)


class MainWindow(object):
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Signal Demo")
        window.connect("destroy", Destroy)
        window.set_border_width(8)
        vbox = gtk.VBox()
        window.add(vbox)
        head = gtk.HBox()
        head.pack_start(HeadMode('start'), False, True, 4)
        head.show()
        vbox.pack_start(head, False, True, 4) 
        table = gtk.Table(3, 3, False)
        vbox.pack_start(table)
        vbox.show()

        for cx in range(3):
            for cy in range(3):
                if cy < 3 or (cy == 3 and cx <=5):
                    label_text = "11300%i0%i" % (cx+1, cy+1)
                else:
                    label_text = ""

                event_box = OrderBox(label_text)
                table.attach(event_box, cx, cx+1, cy, cy+1,
                          xpadding=4, ypadding=4)
                # More things you need an X window for ...
                event_box.realize()
        table.show()
#        window.fullscreen()
        window.show()


def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    MainWindow()
    main()

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

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

发布评论

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

评论(1

一指流沙 2024-12-16 05:07:05

您必须为要发出或连接的对象定义 message-send 信号 obj.emit('message-send', ...)obj. connect('message-send', ...) 无论您在哪里做这些事情。

您尚未为 HeadMode 类定义信号 message-send。所以,这一行是错误的:

        self.connect('message-send', self.on_message)

也许你想做这样的事情:

        self.order_box.connect('message-send', self.on_message)

You have to define message-send signal for the object you are emiting or connecting obj.emit('message-send', ...) or obj.connect('message-send', ...) no matter where are you doing these things.

You have not defined signal message-send for HeadMode class. So, this line is wrong:

        self.connect('message-send', self.on_message)

Maybe you want do something like this:

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