我喜欢关于 SO 的工作示例,所以我决定发布一个。所有的学分都是@Johan Dahlin!
#!/usr/bin/env python3
from gi.repository import Gtk
def button_release_event(button, event):
if event.button == 3:
menu.popup(None, None, None, None, event.button, event.time)
window = Gtk.Window()
window.connect("destroy", lambda q: Gtk.main_quit())
liststore = Gtk.ListStore(str)
liststore.append(["1"])
liststore.append(["2"])
menu=Gtk.Menu()
menu.append(Gtk.ImageMenuItem("Yep it works!"))
menu.append(Gtk.ImageMenuItem(":)"))
menu.show_all()
treeview = Gtk.TreeView(model=liststore)
window.add(treeview)
treeviewcolumn = Gtk.TreeViewColumn()
treeview.append_column(treeviewcolumn)
# Set the treeviewcolum as clickable
#
treeviewcolumn.set_clickable(True)
# force Gtk to create a header button for the Gtk.TreeViewColumn
#
label = Gtk.Label("Numbers")
label.show()
treeviewcolumn.set_widget(label)
# fetch the internal GtkButton of the header:
#
widget = treeviewcolumn.get_widget()
while not isinstance(widget, Gtk.Button):
widget = widget.get_parent()
widget.connect('button-release-event', button_release_event)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, 'text', 0)
window.show_all()
Gtk.main()
快乐的黑客!