注册 登录
编程论坛 Python论坛

python小白请教如何获取ImageList的各个图片绑定事件

qwerch 发布于 2019-11-05 01:10, 2640 次点击
我想通过ImageList来制作电子相册预览,通过点击ImageList中的图片弹出相应figure来展示它们,但一直不知道怎么让各个事件和各个图片对应起来,
还有因为这份代码是在别人的基础上修改的,所以想请教一下有没有不用data字典来给图片做说明又能根据文件夹中图片的个数充分初始化ImageList,这里的代码必须在 for x in range()里手动输入参数来初始化
程序代码:
import wx
from PIL import Image
import matplotlib.pyplot as plt
import os
import tkinter
import re
import pygame

data = {0:"Zero",1:"first",2:"second",3:"three",4:"four",5:"five",6:"six",7:"seven",8:"eight",9:"nine",10:"ten",11:"eleven"}
#字典
#
h = 0

class MyFrame(wx.Frame):
    def __init__(self,parent=None):
        #h = 1

        super(MyFrame, self).__init__(parent, -1, "带位图的列表", size=(1000, 1000))#super可以调用父类以及父类的方法
        il = wx.ImageList(150,300,True)   #创建图像列表

        for f in os.listdir(r'E:\\photo2'):
            #h = h + 1

            img = wx.Image(r"E:\photo2"+"\\"+f,wx.BITMAP_TYPE_BMP)

            img.Rescale(150,300)

            bmp = img.ConvertToBitmap()
            il.Add(bmp)
            #s = il.GetBitmap(5)
            #print(s)

        self.list = wx.ListCtrl(self,-1,style=wx.LC_ICON|wx.LC_AUTOARRANGE)
        self.list.AssignImageList(il,wx.IMAGE_LIST_NORMAL)
        #h = self.list.InsertImageItem()

        #调用InsertImageStringItem()方法出入列表项,并为图标设置说明字符串
        #self.Bind(wx.EVT_LIST_ITEM_SELECTED, plt.show,il[])
        for x in range(11):
            self.list.InsertImageStringItem(x,data[x],x)





        #self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.courseListSelectFunc, self.list)
        #self.Bind(wx.EVT_LIST_ITEM_SELECTED,plt.show , self.list)


app = wx.App()

frame = MyFrame()
frame.Show()

app.MainLoop()

#AssignImageList和InsertImageStringItem去创建位图列表
现在程序运行的结果如下:
只有本站会员才能查看附件,请 登录
之前尝试添加过事件绑定不管点击哪个图都只能展示最后一张图,因此请教各位大佬对应绑定事件的办法
3 回复
#2
fall_bernana2019-11-05 11:21
回复 楼主 qwerch
程序代码:

import wx
from PIL import Image
import matplotlib.pyplot as plt
import os
import tkinter
import re
import pygame
#data = {0:"Zero",1:"first",2:"second",3:"three",4:"four",5:"five",6:"six",7:"seven",8:"eight",9:"nine",10:"ten",11:"eleven"}
class MyFrame(wx.Frame):
    def __init__(self,parent=None):
        super(MyFrame, self).__init__(parent, -1, "带位图的列表", size=(1000, 1000))#super可以调用父类以及父类的方法
        self.imagelist=[]
        il = wx.ImageList(150,150,True)   #创建图像列表
        self.list = wx.ListCtrl(self,-1,style=wx.LC_ICON|wx.LC_AUTOARRANGE) #创建ListCtrl
        for f in os.listdir(r'E:\\picture\\屏幕保护'):
            img = wx.Image("E:\\picture\\屏幕保护\\"+f,wx.BITMAP_TYPE_ANY)
            img.Rescale(150,150)
            bmp = img.ConvertToBitmap()
            il.Add(bmp)
            img1=plt.imread("E:\\picture\\屏幕保护\\"+f)
            self.imagelist.append(img1)
            #print(il.GetImageCount())
            #调用InsertItem()方法出入列表项,并为图标设置说明字符串
            self.list.InsertItem(il.GetImageCount()-1,f,il.GetImageCount()-1) #把图像按索引存入ListCtrl :InsertItem(索引index,lable,AssignImageList存放的ImageList的index)
        self.list.AssignImageList(il,wx.IMAGE_LIST_NORMAL)
        self.list.Bind(wx.EVT_LIST_ITEM_SELECTED, self.imagelistcontrol)#绑定左键点击事件:
        self.list.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.imagelistcontrol2)#绑定左键双击事件:
        self.list.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.imagelistcontrol3)#绑定右键点击事件:
        self.list.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.imagelistcontrol4)#绑定左键取消事件:
        #self.Bind(wx.EVT_LIST_DELETE_ITEM, self.OnItemDelete, self.list)
        #self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list)
        #self.Bind(wx.EVT_LIST_COL_RIGHT_CLICK, self.OnColRightClick, self.list)
        #self.Bind(wx.EVT_LIST_COL_BEGIN_DRAG, self.OnColBeginDrag, self.list)
        #self.Bind(wx.EVT_LIST_COL_DRAGGING, self.OnColDragging, self.list)
        #self.Bind(wx.EVT_LIST_COL_END_DRAG, self.OnColEndDrag, self.list)
        #self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginEdit, self.list)
        
    def imagelistcontrol(self, event):
        print(self.list.GetItemText(self.list.GetFocusedItem()))#获取选中的item的 lable
        itemcount = self.list.GetItemCount()#获取list的count
        print(itemcount)
        for i in range(itemcount):
            if self.list.IsSelected(i):#如果是选中的
                wx.MessageBox("select:"+str(i), caption="Message", style=wx.OK)
                plt.imshow(self.imagelist[i])
                plt.show()
    def imagelistcontrol2(self, event):
        print("EVT_LIST_ITEM_ACTIVATED")
    def imagelistcontrol3(self, event):
        print("EVT_LIST_ITEM_RIGHT_CLICK")
    def imagelistcontrol4(self, event):
        print("EVT_LIST_ITEM_DESELECTED")
   
app = wx.App()

frame = MyFrame()
frame.Show()

app.MainLoop()

#AssignImageList和InsertImageStringItem去创建位图列表


[此贴子已经被作者于2019-11-5 11:56编辑过]

#3
qwerch2019-11-05 14:16
回复 2楼 fall_bernana
大佬!!!!!太谢谢了,人狠话不多

#4
迷小弟2020-02-29 21:55
你可以查一下资料,书中自有黄金屋,加油!祝你成功!!!
1