''' In below code,I am displaying images from folder in three different panel. Here I am getting one problem, the problem is that when there is no any image present in the folder the wxpython frame does not open but code executes perfectly there is no any error. main problem is that frame does not open. How can put any logics to display frame even image is not present in folder.'''
import wx
import threading
from PIL import Image
import wx.lib.scrolledpanel
import os, time
import glob
class windowclass(wx.Frame):
def __init__(self, *args, **kwargs):
super(windowclass, self).__init__(*args, **kwargs)
self.SetTitle("Face Recognition")
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=(185, 660), pos=(1175, 50), style=wx.SIMPLE_BORDER)
self.panel2.SetupScrolling()
self.panel2.SetBackgroundColour('#FFFFFF')
self.panel2.SetMinSize((185,660))
self.panel3 = wx.lib.scrolledpanel.ScrolledPanel(self.panel1,-1, size=(185, 660), pos=(985, 50), style=wx.SIMPLE_BORDER)
self.panel3.SetupScrolling()
self.panel3.SetBackgroundColour('#FFFFFF')
self.panel3.SetMinSize((185,660))
self.panel4 = wx.lib.scrolledpanel.ScrolledPanel(self.panel1,-1, size=(185, 660), pos=(795, 50), style=wx.SIMPLE_BORDER)
self.panel4.SetupScrolling()
self.panel4.SetBackgroundColour('#FFFFFF')
self.panel4.SetMinSize((185,660))
videopanel = wx.Panel(self.panel1,size=(700,500), pos=(10,50), style=wx.SIMPLE_BORDER)
videopanel.SetBackgroundColour('#FFFFFF')
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.basicgui, self.timer)
self.timer1 = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.basicgui1, self.timer1)
self.timer2 = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.basicgui2, self.timer2)
self.knowndict = {}
self.unknowndict = {}
self.visitordict = {}
self.knownbSizer = wx.BoxSizer( wx.VERTICAL )
self.unknownbSizer = wx.BoxSizer( wx.VERTICAL )
self.visitorbSizer = wx.BoxSizer( wx.VERTICAL )
self.panel2.SetSizer(self.knownbSizer)
self.panel3.SetSizer(self.unknownbSizer)
self.panel4.SetSizer(self.visitorbSizer)
self.mainsizer = wx.BoxSizer( wx.HORIZONTAL )
self.mainsizer.Add(self.panel1,1,wx.EXPAND)
self.SetSizer(self.mainsizer)
self.title()
self.ontimer()
def ontimer(self):
self.timer.Start(10)
self.timer1.Start(10)
self.timer2.Start(10)
def title(self):
font = wx.Font(15, wx.SWISS,wx.NORMAL,wx.BOLD)
font1 = wx.Font(20, wx.SWISS,wx.NORMAL,wx.BOLD)
known = wx.StaticText(self.panel1,-1,style = wx.ALIGN_CENTER, pos=(1200,20))
known.SetFont(font)
known.SetLabel("Known People")
unknown = wx.StaticText(self.panel1,-1,style = wx.ALIGN_CENTER, pos=(1010,20))
unknown.SetFont(font)
unknown.SetLabel("Unknown People")
visitor = wx.StaticText(self.panel1,-1,style = wx.ALIGN_CENTER, pos=(850,20))
visitor.SetFont(font)
visitor.SetLabel("Visitor")
videotitle = wx.StaticText(self.panel1,-1,style = wx.ALIGN_CENTER, pos=(260,10))
videotitle.SetFont(font1)
videotitle.SetLabel("Video Streaming")
def basicgui(self, event):
self.GetJpgListknown("./image")
allimage = len(self.knowndict)
items = self.knownbSizer.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.knowndict.items():
im = Image.open(item)
imagename = im.filename[8:-4]
mainpanel = wx.Panel(self.panel2, size=(150,150), style=wx.SIMPLE_BORDER, pos=(0,0))
control = wx.StaticBitmap(mainpanel, -1, bitmap )
photoname = wx.StaticText(mainpanel,-1,style = wx.ALIGN_CENTER,pos = (0,115), size=(150,50))
photoname.SetLabel(imagename)
self.knownbSizer.Add( mainpanel, 0, wx.ALL, 5 )
#reset scrolling
self.panel2.SetupScrolling(scrollToTop=False)
self.Layout()
self.Show()
def basicgui1(self, event):
self.GetJpgListunknown("./image1")
allimage = len(self.unknowndict)
items = self.unknownbSizer.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.unknowndict.items():
im = Image.open(item)
imagename = im.filename[9:-4]
mainpanel = wx.Panel(self.panel3, size=(150,150), style=wx.SIMPLE_BORDER, pos=(0,0))
control = wx.StaticBitmap(mainpanel, -1, bitmap )
photoname = wx.StaticText(mainpanel,-1,style = wx.ALIGN_CENTER,pos = (0,115), size=(150,50))
photoname.SetLabel(imagename)
self.unknownbSizer.Add( mainpanel, 0, wx.ALL, 5 )
#reset scrolling
self.panel3.SetupScrolling(scrollToTop=False)
self.Layout()
self.Show()
def basicgui2(self, event):
self.GetJpgListvisitor("./image2")
allimage = len(self.visitordict)
items = self.visitorbSizer.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.visitordict.items():
im = Image.open(item)
imagename = im.filename[9:-4]
mainpanel = wx.Panel(self.panel4, size=(150,150), style=wx.SIMPLE_BORDER, pos=(0,0))
control = wx.StaticBitmap(mainpanel, -1, bitmap )
photoname = wx.StaticText(mainpanel,-1,style = wx.ALIGN_CENTER,pos = (0,115), size=(150,50))
photoname.SetLabel(imagename)
self.visitorbSizer.Add( mainpanel, 0, wx.ALL, 5 )
#reset scrolling
self.panel4.SetupScrolling(scrollToTop=False)
self.Layout()
self.Show()
def GetJpgListknown(self,dir):
#jpg = [f for f in os.listdir(dir) if f[-4:] == ".jpg"]
# print "JPGS are:", jpgs
#jpgs = [os.path.join(dir, f) for f in jpg]
jpgs = glob.glob(dir+"/*.jpg")
for i in jpgs:
#if image already in dict bail
if i in self.knowndict:
continue
bitmap = wx.Bitmap(i)
print(bitmap)
image = bitmap.ConvertToImage()
image = image.Scale(150, 100, wx.IMAGE_QUALITY_HIGH)
bitmap = wx.Bitmap(image)
self.knowndict[i] = bitmap
#make a list of any deleted images
del_list = []
for i in self.knowndict:
if i not in jpgs:
del_list.append(i)
#remove deleted images from the dictionary
for i in del_list:
self.knowndict.pop(i)
return
def GetJpgListunknown(self,dir):
jpg = [f for f in os.listdir(dir) if f[-4:] == ".jpg"]
# print "JPGS are:", jpgs
jpgs = [os.path.join(dir, f) for f in jpg]
for i in jpgs:
#if image already in dict bail
if i in self.unknowndict:
continue
bitmap = wx.Bitmap(i)
print(bitmap)
image = bitmap.ConvertToImage()
image = image.Scale(150, 100, wx.IMAGE_QUALITY_HIGH)
bitmap = wx.Bitmap(image)
self.unknowndict[i] = bitmap
#make a list of any deleted images
del_list = []
for i in self.unknowndict:
if i not in jpgs:
del_list.append(i)
#remove deleted images from the dictionary
for i in del_list:
self.unknowndict.pop(i)
return
def GetJpgListvisitor(self,dir):
jpg = [f for f in os.listdir(dir) if f[-4:] == ".jpg"]
# print "JPGS are:", jpgs
jpgs = [os.path.join(dir, f) for f in jpg]
for i in jpgs:
#if image already in dict bail
if i in self.visitordict:
continue
bitmap = wx.Bitmap(i)
print(bitmap)
image = bitmap.ConvertToImage()
image = image.Scale(150, 100, wx.IMAGE_QUALITY_HIGH)
bitmap = wx.Bitmap(image)
self.visitordict[i] = bitmap
#make a list of any deleted images
del_list = []
for i in self.visitordict:
if i not in jpgs:
del_list.append(i)
#remove deleted images from the dictionary
for i in del_list:
self.visitordict.pop(i)
return
def main():
app = wx.App()
windowclass(None)
app.MainLoop()
main(
)
You need to put something into the sizer
even if there are no images.
I haven't edited your code but uprated a previous answer instead, because your code is mixing positional placement and sizers, which is bound to end up giving you trouble.
Below is sample code for a video to be displayed on the left and on the middle and on the right are 2 separate scrolling panels, monitoring 2 separate directories.
I hope it helps.
import wx
import wx.lib.scrolledpanel
import glob
import wx.media
class windowclass(wx.Frame):
def __init__(self, *args, **kwargs):
super(windowclass, self).__init__(*args, **kwargs)
self.panel1 = wx.Panel(self, -1, size=(350,550))
self.panel1.SetBackgroundColour('#FDDF99')
self.h1 = wx.StaticText(self.panel1, -1, "Video")
self.Media = wx.media.MediaCtrl()
self.Media.Create(self.panel1, style=wx.SIMPLE_BORDER, szBackend="")
self.slider = wx.Slider(self.panel1, -1, 0, 0, 10)
self.slider.SetMinSize((340, -1))
self.Media.Load("/home/rolf/BBB.ogv")
self.Media.SetInitialSize((340,300))
self.panel2 = wx.lib.scrolledpanel.ScrolledPanel(self,-1, size=(350,550))
self.h2 = wx.StaticText(self.panel2, -1, "Known")
self.panel2.SetupScrolling()
self.panel2.SetBackgroundColour('#FFFFFF')
self.panel2.SetMinSize((350,550))
self.panel3 = wx.lib.scrolledpanel.ScrolledPanel(self,-1, size=(350,550))
self.h3 = wx.StaticText(self.panel3, -1, "Unknown")
self.panel3.SetupScrolling()
self.panel3.SetBackgroundColour('#FFFFFF')
self.panel3.SetMinSize((350,550))
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.basicgui, self.timer)
self.vtimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.video, self.vtimer)
self.dict1 = {}
self.dict2 = {}
self.sizer1 = wx.BoxSizer( wx.VERTICAL )
self.sizer1.Add(self.h1,0,wx.ALIGN_CENTER)
self.sizer1.Add(self.Media)
self.sizer1.Add(self.slider)
self.sizer2 = wx.BoxSizer( wx.VERTICAL )
self.sizer2.Add(self.h2,0,wx.ALIGN_CENTER)
self.sizer3 = wx.BoxSizer( wx.VERTICAL )
self.sizer3.Add(self.h3,0,wx.ALIGN_CENTER)
self.panel1.SetSizer(self.sizer1)
self.panel2.SetSizer(self.sizer2)
self.panel3.SetSizer(self.sizer3)
self.mainsizer = wx.BoxSizer( wx.HORIZONTAL )
self.mainsizer.Add(self.panel1,0,wx.EXPAND)
self.mainsizer.Add(self.panel2,0,wx.EXPAND)
self.mainsizer.Add(self.panel3,0,wx.EXPAND)
self.SetSizer(self.mainsizer)
self.Bind(wx.EVT_CLOSE, self.Stop)
self.timer.Start(3000)
self.vtimer.Start(1000)
self.Media.Play()
self.slider.SetRange(0, self.Media.Length())
self.Show()
def Stop(self, event):
self.Media.Stop()
self.Destroy()
def video(self,event):
offset = self.Media.Tell()
self.slider.SetValue(offset)
def basicgui(self,event):
self.GetJpgList("./image", self.dict1)
image_cnt = len(self.dict1)
items = self.sizer2.GetChildren()
#if the image count is different perform processing
update = False
if len(items)-1 != image_cnt:
#Clear sizer of it's items
self.sizer2.Clear(delete_windows=True)
h2 = wx.StaticText(self.panel2, -1, "Known")
self.sizer2.Add(h2,0,wx.ALIGN_CENTER)
# add images to the sizer
if image_cnt != 0:
for item, bitmap in self.dict1.items():
control = wx.StaticBitmap(self.panel2, -1, bitmap)
self.sizer2.Add( control, 0, wx.CENTER|wx.ALL, 5 )
else:
control = wx.StaticBitmap(self.panel2, -1, wx.NullBitmap)
self.sizer2.Add( control, 0, wx.CENTER|wx.ALL, 5 )
#reset scrolling
self.panel2.SetupScrolling(scrollToTop=False)
update = True
self.GetJpgList("./image1", self.dict2)
image_cnt = len(self.dict2)
items = self.sizer3.GetChildren()
#if the image count is different perform processing
if len(items)-1 != image_cnt:
#Clear sizer of it's items
self.sizer3.Clear(delete_windows=True)
h3 = wx.StaticText(self.panel3, -1, "Unknown")
self.sizer3.Add(h3,0,wx.ALIGN_CENTER)
# add images to the sizer
if image_cnt != 0:
for item, bitmap in self.dict2.items():
control = wx.StaticBitmap(self.panel3, -1, bitmap)
self.sizer3.Add( control, 0, wx.CENTER|wx.ALL, 5 )
else:
control = wx.StaticBitmap(self.panel2, -1, wx.NullBitmap)
self.sizer2.Add( control, 0, wx.CENTER|wx.ALL, 5 )
#reset scrolling
self.panel3.SetupScrolling(scrollToTop=False)
update = True
if update == True:
self.Layout()
self.Show()
def GetJpgList(self, the_dir, the_dict):
jpgs = glob.glob(the_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 the_dict:
continue
print("adding", i)
image = wx.Image(i)
image = image.Scale(300, 200, wx.IMAGE_QUALITY_HIGH)
bitmap = wx.Bitmap(image)
the_dict[i] = bitmap
#make a list of any deleted images
del_list = []
for i in the_dict:
if i not in jpgs:
del_list.append(i)
#remove deleted images from the dictionary
for i in del_list:
the_dict.pop(i)
print("deleting",i)
return
def main():
app = wx.App()
windowclass(None)
app.MainLoop()
main()
it works but it is refreshing panel continuously and i don't want that continuous refresh. what should i do.
My code or yours? If it's mine, put the video update in another
Timer
and only perform theLayout()
andShow()
if updating the sizers.My mistake! I added in the "known", "unknown" texts into the sizers, so the test for the number of items in the sizer vs the number of jpgs, was always out by 1. See my edited code above.