我选择处理程序类替代方案来连接我的glade项目的标志。
示例:
XML Galde
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<signal name="delete-event" handler="onDeleteWindow" swapped="no"/>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">button</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="pressed" handler="onButtonPressed" swapped="no"/>
</object>
</child>
</object>
</interface>
Python代码
from gi.repository import Gtk
class Handler:
def onDeleteWindow(self, *args):
Gtk.main_quit(*args)
def onButtonPressed(self, button):
print("Hello World!")
builder = Gtk.Builder()
builder.add_from_file("builder_example.glade")
builder.connect_signals(Handler())
window = builder.get_object("window1")
window.show_all()
Gtk.main()
如果我需要将参数传递给方法怎么办?例如,在下面的代码中。
from gi.repository import Gtk
class Handler:
def onDeleteWindow(self, *args):
Gtk.main_quit(*args)
def onButtonPressed(self, button, any_widget):
print("Hello World!", type(any_widget))
label = Gtk.Label() #HOW TO PASS THIS WIDGET TO THE onButtonPressed METHOD?
builder = Gtk.Builder()
builder.add_from_file("builder_example.glade")
builder.connect_signals(Handler())
window = builder.get_object("window1")
window.show_all()
Gtk.main()
我可以用字典来解决。但我不知道如何使用处理程序类。