运行时,图像未显示在面板wxpython中
我正在尝试使用for循环显示图像文件夹中的所有图像,并且所有图像都显示正确。但是我想在运行时添加另一个映像,所以我在这里使用threading.Timer(2.0,self.basicgui).start()每当我向文件夹添加新映像时,我都可以在映像列表中看到映像的名称,但无法更新。我想刷新面板以显示更新的图像如何修复此问题运行时,图像未显示在面板wxpython中,python,python-3.x,wxpython,Python,Python 3.x,Wxpython,我正在尝试使用for循环显示图像文件夹中的所有图像,并且所有图像都显示正确。但是我想在运行时添加另一个映像,所以我在这里使用threading.Timer(2.0,self.basicgui).start()每当我向文件夹添加新映像时,我都可以在映像列表中看到映像的名称,但无法更新。我想刷新面板以显示更新的图像如何修复此问题 导入wx 导入线程 从PIL导入图像 导入wx.lib.scrolled面板 导入操作系统 类windowclass(wx.Frame): 定义初始化(self,*args
导入wx
导入线程
从PIL导入图像
导入wx.lib.scrolled面板
导入操作系统
类windowclass(wx.Frame):
定义初始化(self,*args,**kwargs):
super(windowclass,self)。\uuuuuu初始化(*args,**kwargs)
全球专题小组1
panel1=wx.Panel(self,size=(1000,28),pos=(0,0),style=wx.SIMPLE\u BORDER)
面板1.立根底色(“#FDDF99”)
全球专题小组2
panel2=wx.lib.scrolledpanel.scrolledpanel(panel1,-1,大小=(350710),位置=(1010,0),样式=wx.SIMPLE\u边框)
panel2.设置滚动()
镶板2.立根基色(“#FFFFFF”)
self.basicgui()
def basicgui(自身):
填充=0
bSizer=wx.BoxSizer(wx.VERTICAL)
self.jpgs=GetJpgList(“./图像”)
allimage=len(self.jpgs)
打印(allimage)
对于范围内的i(0,allimage):
#打印(“i=”,i)
bitmap=wx.bitmap(self.jpgs[i])
image=wx.ImageFromBitmap(位图)
图像=图像比例(300,200,宽x图像质量高)
位图=wx.BitmapFromImage(图像)
控件=wx.StaticBitmap(面板2,-1,位图)
#打印(控制)
控制。设置位置((10,填充+10))
填充+=210
添加(控件,0,wx.ALL,5)
面板2.设置器(B设置器)
面板2.布局()
Timer(2.0,self.basicgui.start())
self.Show()
返回
def GetJpgList(目录):
jpgs=[f表示os.listdir(dir)中的f,如果f[-4:][==”.jpg“]
#打印“JPG是:”,JPG
返回[os.path.join(dir,f)for jpgs中的f]
def main():
app=wx.app()
windowclass(无)
app.MainLoop()
main()
这是您的代码,经过修改后效率更高。目前,您每次都会生成图像,使用少量图像就可以了,但如果图像太多,这会消耗您的cpu。
在这里,我构建了一个图像字典,所以只需要对添加和删除进行转换。发现是否需要做任何工作也是很有用的,所以我假设一个简单的计数可以告诉我没有任何变化,因此不需要做任何工作
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()
您可以使用
wx.Timer
来代替。我如何使用wx.Timer.来代替threading.Timer
使用wx.Timer
并回调到basicgui
,仅此网站上就有306次点击。计时器每(n)毫秒运行一次回调,直到停止。它可以工作,但我遇到了一个问题。滚动条未显示Call panel2 self.panel2并在回调中再次发出self.panel2.SetupScrolling()
。非常欢迎您的帮助!欢迎来到StackOverflow。