Apologies for the delay, but I’ve only just found some time to investigate this question.
I started with the example in the documentation for wx.lib.agw.scrolledthumbnail
. I modified it to add a popup menu which the ScrolledThumbnail
class displays when you right-click on a thumbnail.
In the code below, the popup menu has a single menu item which simply prints the pathnames of all the selected thumbnails.
Note: if you hold down Shift or Ctrl when right clicking, the selection will be extended, resulting in multiple selected thumbnails.
One drawback of this approach is that it accesses protected attributes of the ScrolledThumbnail
class.
import os
import wx
from wx.lib.agw.scrolledthumbnail import ScrolledThumbnail, Thumb, NativeImageHandler
ID_PRINT_PATHNAME = wx.NewIdRef()
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "ScrolledThumb Demo", size=(400,300))
self.scroll = ScrolledThumbnail(self, -1, size=(400,300))
menu = wx.Menu()
menu.Append(ID_PRINT_PATHNAME, "Print Selected Thumbnails")
self.scroll.SetPopupMenu(menu)
self.Bind(wx.EVT_MENU, self.OnMenuPrintPathnames, id=ID_PRINT_PATHNAME)
def ShowDir(self, dir):
files = os.listdir(dir)
thumbs = []
for f in files:
if os.path.splitext(f)[1] in [".jpg", ".gif", ".png"]:
thumbs.append(Thumb(dir, f, caption=f, imagehandler=NativeImageHandler))
self.scroll.ShowThumbs(thumbs)
def OnMenuPrintPathnames(self, event):
pathnames = [self.scroll._items[index].GetFullFileName() for index in self.scroll._selectedarray]
print(pathnames)
app = wx.App(False)
frame = MyFrame(None)
frame.ShowDir("images")
frame.Show(True)
app.MainLoop()
Here is the output of a test run:
['images/black_5.jpg']
['images/moon.jpg']
['images/mg_midget.jpg']
['images/black_5.jpg', 'images/moon.jpg', 'images/mg_midget.jpg']