获取多个iconview选项中的最后一个选定项目?

时间:2015-05-03 15:34:22

标签: python-2.7 pygtk

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

test.png

首先,单击常规工具按钮将其选中,然后按住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,)]

正如您所看到的,无论多重选择的顺序如何,icon_view.get_selected_items()始终是排序的,因此我无法使用它来获取有关我最后选择的多项选择中哪个项目的信息(第一种情况下为“其他”,第二种情况下为“一般”。

那么,在这种情况下,如何在多选中获得最后选择的项目?

代码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()

1 个答案:

答案 0 :(得分:0)

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

# 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,则将打印所有内容。