On 15/12/2010 00:09, Alessandro Dentella wrote:
> Hi Dieter,
>
> On Tue, Dec 14, 2010 at 08:28:19PM +0100, Dieter Verfaillie wrote:
>> On 14/12/2010 17:41, Alessandro Dentella wrote:
>>> What am I doing wong?
>>
>> See the attachment. Got it working :)
>
> If I understand correctly, what makes the difference, is that you're emitting
> the signal OUT of __init__. That works but does not explain why Test1 *can*
> emit from within the __init__.
That and the test class from your original example did not initialize
it's base class...
> In the real example, the signal is really emitted within __init__, and that
> have always worked in glade.
Are you sure? Changing the script to emit from __init__ seems to fail
for both libglade and gtkbuilder for me. Tested it both on mswindows
and linux with pygtk-2.22.0, pygobject-2.26.0, etc.
See the attached script, output here looks like:
manual test
========================================
instantiate Test1
__init__ Test1
my-signal1 has been emitted
instantiate Test2
__init__ Test2
__init__ Test1
my-signal1 has been emitted
my-signal2 has been emitted
libglade test
========================================
__init__ Test1
my-signal1 has been emitted
__init__ Test2
__init__ Test1
my-signal1 has been emitted
Traceback (most recent call last):
File "test.py", line 103, in __init__
self.emit('my-signal2')
TypeError: <Test2 object at 0x1595b70 (Test1 at 0xcea220)>: unknown
signal name: my-signal2
gtkbuilder test
========================================
instantiate Test1
__init__ Test1
my-signal1 has been emitted
instantiate Test2
__init__ Test2
__init__ Test1
my-signal1 has been emitted
Traceback (most recent call last):
File "test.py", line 103, in __init__
self.emit('my-signal2')
TypeError: <Test2 object at 0x1595d50 (Test1 at 0xcea310)>: unknown
signal name: my-signal2
#!/usr/bin/python
test_glade='''<?xml version="1.0"?>
<glade-interface>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<widget class="GtkWindow" id="window1">
<child>
<widget class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<child>
<widget class="Test1" id="test1">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="Test2" id="test2">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>'''
test1_ui = '''<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-requires pythonplugin 0.0 -->
<!-- interface-naming-policy project-wide -->
<object class="Test1" id="test1">
<property name="label" translatable="yes">Test1</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
</interface>'''
test2_ui = '''<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-requires pythonplugin 0.0 -->
<!-- interface-naming-policy project-wide -->
<object class="Test2" id="test2">
<property name="label" translatable="yes">Test2</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
</interface>'''
import sys
import gobject
import gtk
import gtk.glade
def log(msg=''):
sys.stdout.write('%s\n' % msg)
sys.stdout.flush()
class Test1(gtk.Button):
__gtype_name__ = 'Test1'
__gsignals__ = {'my-signal1':
(gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())}
def __init__(self):
log('__init__ Test1')
gtk.Button.__init__(self)
self.emit('my-signal1')
def do_my_signal1(self):
log('my-signal1 has been emitted')
class Test2(Test1):
__gtype_name__ = 'Test2'
__gsignals__ = {'my-signal2':
(gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())}
def __init__(self):
log('__init__ Test2')
Test1.__init__(self)
self.emit('my-signal2')
def do_my_signal2(self):
log('my-signal2 has been emitted')
def manual():
log('manual test')
log('========================================')
window = gtk.Window()
hbox = gtk.HBox()
window.add(hbox)
log('instantiate Test1')
test1 = Test1()
test1.set_label('Test1')
hbox.pack_start(test1)
log('instantiate Test2')
test2 = Test2()
test2.set_label('Test2')
hbox.pack_start(test2)
log()
def libglade():
log('libglade test')
log('========================================')
wtree = gtk.glade.xml_new_from_buffer(test_glade, len(test_glade))
window = wtree.get_widget('window1')
log()
def gtkbuilder():
log('gtkbuilder test')
log('========================================')
window = gtk.Window()
hbox = gtk.HBox()
window.add(hbox)
builder = gtk.Builder()
log('instantiate Test1')
builder.add_from_string(test1_ui)
test1 = builder.get_object('test1')
hbox.pack_start(test1)
log('instantiate Test2')
builder.add_from_string(test2_ui)
test2 = builder.get_object('test2')
hbox.pack_start(test2)
if __name__ == '__main__':
manual()
libglade()
gtkbuilder()
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/