小部件之间的自定义信号
尝试将信号从一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须为要发出或连接的对象定义
message-send
信号obj.emit('message-send', ...)
或obj. connect('message-send', ...)
无论您在哪里做这些事情。您尚未为
HeadMode
类定义信号message-send
。所以,这一行是错误的:也许你想做这样的事情:
You have to define
message-send
signal for the object you are emiting or connectingobj.emit('message-send', ...)
orobj.connect('message-send', ...)
no matter where are you doing these things.You have not defined signal
message-send
forHeadMode
class. So, this line is wrong:Maybe you want do something like this: