0
考慮下面的例子中(pygtk的2,蟒2),其產生以下GUI:在多次選擇iconview中獲取上次選擇的項目?
首先,單擊常規工具按鈕來選擇它,然後按住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()