Python 2.7 在多重选择的iconview中获取最后选定的项目?

Python 2.7 在多重选择的iconview中获取最后选定的项目?,python-2.7,pygtk,Python 2.7,Pygtk,考虑下面的示例(pygtk 2,python 2),它生成以下GUI: 首先,单击通用工具按钮将其选中,然后按住SHIFT键,然后多选其余的工具;您将获得打印输出: selected [(0,)] selected [(1,), (0,)] selected [(2,), (1,), (0,)] selected [(3,), (2,), (1,), (0,)] 现在,选择最后一个工具按钮(“其他”)重置上一个多重选择,然后按住SHIFT键,并按相反顺序多重选择其他按钮;打印输出现在是: s

考虑下面的示例(pygtk 2,python 2),它生成以下GUI:

首先,单击通用工具按钮将其选中,然后按住SHIFT键,然后多选其余的工具;您将获得打印输出:

selected [(0,)]
selected [(1,), (0,)]
selected [(2,), (1,), (0,)]
selected [(3,), (2,), (1,), (0,)]
现在,选择最后一个工具按钮(“其他”)重置上一个多重选择,然后按住SHIFT键,并按相反顺序多重选择其他按钮;打印输出现在是:

selected [(3,)]
selected [(3,), (2,)]
selected [(3,), (2,), (1,)]
selected [(3,), (2,), (1,), (0,)]
如您所见,无论多选的顺序如何,
图标\u view.get\u selected\u items()
始终是排序的,因此我无法使用它来获取我最后选择多选中哪个项目的信息(第一种情况下为“other”,第二种情况下为“General”)

那么,在这种情况下,如何获取多重选择中的最后一个选定项

代码,
test.py

# modified from:
# PyGTK FAQ Entry: How can I use the IconView widget?
# http://faq.pygtk.org/index.py?req=show&file=faq19.016.htp

import pygtk
pygtk.require('2.0')
import gtk

class PreferencesMgr(gtk.Dialog):
  def __init__(self):
    gtk.Dialog.__init__(self, 'Preferences', None,
           gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
           (gtk.STOCK_OK, gtk.RESPONSE_OK,
          gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
    self.current_frame = None
    self.create_gui()

  def create_gui(self):

    model = gtk.ListStore(str, gtk.gdk.Pixbuf)

    #pixbuf = gtk.gdk.pixbuf_new_from_file('images/prefs_general.png')
    #pixbuf = gtk_widget_render_icon( widget, stock_item, size )
    pixbuf = gtk.AboutDialog().render_icon(gtk.STOCK_ABOUT, gtk.ICON_SIZE_MENU)
    model.append(['General', pixbuf])

    #pixbuf = gtk.gdk.pixbuf_new_from_file('images/prefs_security.png')
    model.append(['Security', pixbuf])
    model.append(['Nothing', pixbuf])
    model.append(['Other', pixbuf])

    self.icon_view = gtk.IconView(model)
    self.icon_view.set_text_column(0)
    self.icon_view.set_pixbuf_column(1)
    self.icon_view.set_orientation(gtk.ORIENTATION_VERTICAL)
    self.icon_view.set_selection_mode(gtk.SELECTION_MULTIPLE)#(gtk.SELECTION_SINGLE)
    self.icon_view.connect('selection-changed', self.on_select, model)
    self.icon_view.set_columns(1)
    self.icon_view.set_item_width(-1)
    self.icon_view.set_size_request(72, -1)

    self.content_box = gtk.HBox(False)
    self.content_box.pack_start(self.icon_view, fill=True, expand=False)
    self.icon_view.select_path((0,)) # select a category, will create frame
    self.show_all()
    self.vbox.pack_start(self.content_box)
    self.resize(640, 480)
    self.show_all()

  def on_select(self, icon_view, model=None):
    selected = icon_view.get_selected_items()
    if len(selected) == 0: return
    print "selected", selected
    i = selected[0][0]
    category = model[i][0]
    if self.current_frame is not None:
      self.content_box.remove(self.current_frame)
      self.current_frame.destroy()
      self.current_frame = None
    if category == 'General':
      self.current_frame = self.create_general_frame()
    elif category == 'Security':
      self.current_frame = self.create_security_frame()
    else:
      self.current_frame = self.create_generic_frame(category)
    self.content_box.pack_end(self.current_frame, fill=True, expand=True)
    self.show_all()

  def create_general_frame(self):
    frame = gtk.Frame('General')
    return frame

  def create_security_frame(self):
    frame = gtk.Frame('Security')
    return frame

  def create_generic_frame(self, instring):
    frame = gtk.Frame(instring)
    return frame

if __name__ == '__main__':
  p = PreferencesMgr()
  p.run()
  p.destroy()

这只是解决方案的一部分:

# Add to the PreferencesMgr __init__:
self.prev_sel_set = set()   # Set of elements selected previously
在on_select方法中,添加中间的4行:

print "selected", selected   # Old code

sel_set = set(selected)
last_selected = sel_set - self.prev_sel_set
self.prev_sel_set = sel_set
print last_selected          # Print last added item(s)

i = selected[0][0]           # Old code
这将为您提供最后添加到选择中的图标。我不知道它是否正确,因为shift键可以一次添加几个图标。如果您逐个进行多选,这将打印最后一项。如果一次添加2或3,则将打印所有内容