Вот ваш код, исправленный, чтобы быть немного более эффективным.
В настоящее время вы генерируете изображения каждый раз, что с небольшим числом может подойти, но если изображений много, это съест ваш процессор.
Здесь я построил словарь изображений, поэтому нужно преобразовывать только добавления и удаления. Также полезно выяснить, нужно ли вообще выполнять какую-либо работу, поэтому я предположил, что простой подсчет может сказать мне, что ничего не изменилось и, следовательно, никакой работы не требуется.
import wx
import wx.lib.scrolledpanel
import glob
class windowclass(wx.Frame):
def __init__(self, *args, **kwargs):
super(windowclass, self).__init__(*args, **kwargs)
self.panel1 = wx.Panel(self,size=(1000,28), style=wx.SIMPLE_BORDER)
self.panel1.SetBackgroundColour('#FDDF99')
self.panel2 = wx.lib.scrolledpanel.ScrolledPanel(self.panel1,-1, size=(350,550))
self.panel2.SetupScrolling()
self.panel2.SetBackgroundColour('#FFFFFF')
self.panel2.SetMinSize((350,550))
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.basicgui, self.timer)
self.dict = {}
self.bSizer = wx.BoxSizer( wx.VERTICAL )
self.panel2.SetSizer(self.bSizer)
self.mainsizer = wx.BoxSizer( wx.HORIZONTAL )
self.mainsizer.Add(self.panel1,1,wx.EXPAND)
self.SetSizer(self.mainsizer)
self.timer.Start(2000)
def basicgui(self,event):
self.GetJpgList("./images")
allimage = len(self.dict)
items = self.bSizer.GetChildren()
#if the image count is the same as before nothing has changed - bail
if len(items) == allimage:
return
#remove existing images from the sizer
for i in items:
i.GetWindow().Destroy()
# add images to the sizer
for item, bitmap in self.dict.items():
control = wx.StaticBitmap(self.panel2, -1, bitmap)
self.bSizer.Add( control, 0, wx.CENTER|wx.ALL, 5 )
#reset scrolling
self.panel2.SetupScrolling(scrollToTop=False)
self.Layout()
self.Show()
def GetJpgList(self, dir):
#use glob rather than os.listdir
jpgs = glob.glob(dir+"/*.jpg")
#Build a dictionary of the images
#this way we only have to create them once
for i in jpgs:
#if image already in dict bail
if i in self.dict:
continue
print("adding", i)
bitmap = wx.Bitmap(i)
image = bitmap.ConvertToImage()
image = image.Scale(300, 200, wx.IMAGE_QUALITY_HIGH)
bitmap = wx.Bitmap(image)
self.dict[i] = bitmap
#make a list of any deleted images
del_list = []
for i in self.dict:
if i not in jpgs:
del_list.append(i)
#remove deleted images from the dictionary
for i in del_list:
self.dict.pop(i)
print("deleting",i)
return
def main():
app = wx.App()
windowclass(None)
app.MainLoop()
main()