我想处理菜单点击事件,但是,
这个丢失的处理程序警告正在发生,
哪里我错了?
我将信号ab与处理程序abcd连接起来
在menuitem6上,关于激活事件,
处理点击事件是否正确?
这是错误:
abc.py:15: RuntimeWarning: missing handler 'a
bc'
builder.connect_signals({ "on_window_destroy" : gtk.main_quit })
None
这是代码:
abc.py:
import pygtk
pygtk.require("2.0")
import gtk
def inIta(col, cell, model, iter, mymodel):
s = model.get_string_from_iter(iter)
niter = mymodel.get_iter_from_string(s)
obj = mymodel.get_value(niter, 0)
cell.set_property('text', obj)
class TutorialApp(object):
def __init__(self):
builder = gtk.Builder()
builder.add_from_file("./tutorial.xml")
builder.connect_signals({ "on_window_destroy" : gtk.main_quit })
print builder.connect_signals({ "abc" : self.abcd })
self.window = builder.get_object("window")
self.widgets = {}
self.widgets['disasm'] = builder.get_object('disasm')
self.widgets['disasmw'] = builder.get_object('disassemblerWindow')
self.widgets['regw'] = builder.get_object('regWin')
self.widgets['stackw'] = builder.get_object('stackWin')
self.widgets['dumpw'] = builder.get_object('dumpWin')
self.widgets['stack'] = builder.get_object('stack')
self.widgets['dump'] = builder.get_object('dump')
self.widgets['registers'] = builder.get_object('registers')
""" add columns to disasm listview"""
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn("Address", rendererText, text=0)
self.widgets['disasmw'].append_column(column)
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn("Opcode", rendererText, text=1)
self.widgets['disasmw'].append_column(column)
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn("Mneumonic", rendererText, text=2)
self.widgets['disasmw'].append_column(column)
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn("Registers", rendererText, text=0)
self.widgets['regw'].append_column(column)
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn("Stack", rendererText, text=0)
self.widgets['stackw'].append_column(column)
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn("Dump", rendererText, text=0)
self.widgets['dumpw'].append_column(column)
self.widgets['disasm'].append(["alokkkkkkkkkkk","alokkkkkkkkkk","alokkkkkkkkkk"])
self.widgets['disasm'].append(["alokkkkkkkkkkk","alokkkkkkkkkk","alokkkkkkkkkk"])
self.widgets['disasm'].append(["alokkkkkkkkkkk","alokkkkkkkkkk","alokkkkkkkkkk"])
self.window.show()
def abcd(menuitem):
print "tst"
if __name__ == "__main__":
app = TutorialApp()
gtk.main()
,这是xml:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkListStore" id="disasm">
<columns>
<!-- column-name gchararray1 -->
<column type="gchararray"/>
<!-- column-name gchararray2 -->
<column type="gchararray"/>
<!-- column-name gchararray3 -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkListStore" id="dump"/>
<object class="GtkListStore" id="registers"/>
<object class="GtkListStore" id="stack"/>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="title" translatable="yes">untitled</property>
<property name="window_position">center</property>
<property name="default_width">440</property>
<property name="default_height">250</property>
<property name="icon">ico.ico</property>
<signal name="destroy" handler="on_window_destroy" swapped="no"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuBar" id="menubar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_File</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem1">
<property name="label">gtk-new</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem2">
<property name="label">gtk-open</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem3">
<property name="label">gtk-save</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem4">
<property name="label">gtk-save-as</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem" id="separatormenuitem1">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem5">
<property name="label">gtk-quit</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Edit</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem6">
<property name="label">gtk-cut</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem7">
<property name="label">gtk-copy</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem8">
<property name="label">gtk-paste</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem9">
<property name="label">gtk-delete</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_View</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Options</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Settings</property>
<property name="use_underline">True</property>
<signal name="activate" handler="abc" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Help</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem10">
<property name="label">gtk-about</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkTreeView" id="disassemblerWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">disasm</property>
<property name="enable_grid_lines">both</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkTreeView" id="regWin">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">registers</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkHBox" id="hbox2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkTreeView" id="dumpWin">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">dump</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkTreeView" id="stackWin">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">stack</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkStatusbar" id="statusbar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">2</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
答案 0 :(得分:2)
您可以使用builder.connect_signals(self)
。然后你只需要创建像xml文件中的处理程序一样的方法。所以在这种情况下:
class TutorialApp(object):
def __init__(self):
builder = gtk.Builder()
builder.add_from_file('./tutorial.xml')
builder.connect_signals(self)
self.window = builder.get_object('disasm')
##rest of you __init__
def abc(self, menuitem):
print "tst"
def on_window_destroy(self, window):
gtk.main_quit()