我试图显示所有的图像从图像文件夹使用for循环和所有显示正确。但是我想在运行时添加另一个映像,所以我在这里使用线程threading.Timer(2.0, self.basicgui).start()
每当我将新映像添加到文件夹时,我可以在映像列表中看到映像的名称,但它无法更新。我想刷新面板,以显示更新的图像,我如何才能解决这个问题
import wx
import threading
from PIL import Image
import wx.lib.scrolledpanel
import os
class windowclass(wx.Frame):
def __init__(self, *args, **kwargs):
super(windowclass, self).__init__(*args, **kwargs)
global panel1
panel1 = wx.Panel(self,size=(1000,28), pos=(0,0), style=wx.SIMPLE_BORDER)
panel1.SetBackgroundColour('#FDDF99')
global panel2
panel2 = wx.lib.scrolledpanel.ScrolledPanel(panel1,-1, size=(350,710), pos=(1010,0), style=wx.SIMPLE_BORDER)
panel2.SetupScrolling()
panel2.SetBackgroundColour('#FFFFFF')
self.basicgui()
def basicgui(self):
padding = 0
bSizer = wx.BoxSizer( wx.VERTICAL )
self.jpgs = GetJpgList("./image")
allimage = len(self.jpgs)
print(allimage)
for i in range(0,allimage):
#print("i=",i)
bitmap = wx.Bitmap(self.jpgs[i])
image = wx.ImageFromBitmap(bitmap)
image = image.Scale(300, 200, wx.IMAGE_QUALITY_HIGH)
bitmap = wx.BitmapFromImage(image)
control = wx.StaticBitmap(panel2, -1, bitmap)
#print(control)
control.SetPosition((10, padding+10))
padding += 210
bSizer.Add( control, 0, wx.ALL, 5 )
panel2.SetSizer( bSizer )
panel2.Layout()
threading.Timer(2.0, self.basicgui).start()
self.Show()
return
def GetJpgList(dir):
jpgs = [f for f in os.listdir(dir) if f[-4:] == ".jpg"]
# print "JPGS are:", jpgs
return [os.path.join(dir, f) for f in jpgs]
def main():
app = wx.App()
windowclass(None)
app.MainLoop()
main()
这是您的代码,修改后效率会提高一点。
目前,您每次都会生成图像,使用少量图像就可以了,但如果图像太多,则会占用您的cpu。
在这里,我建立了一个图像字典,所以只需要对添加和删除进行转换。发现是否需要做任何工作也是很有用的,所以我假设一个简单的计数可以告诉我什么都没有改变,因此不需要做任何工作
相关问题 更多 >
编程相关推荐