问题
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()
回答1:
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)
来源:https://stackoverflow.com/questions/7706192/custom-signal-from-widget-to-widget