温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python 3.x - frame does not open when image is not present in folder in wxpython
python-3.x wxpython

python 3.x - wxpython的文件夹中不存在图像时,框架不会打开

发布于 2020-03-27 11:46:56

'''在下面的代码中,我在三个不同的面板中显示文件夹中的图像。在这里,我遇到一个问题,问题是当文件夹中没有任何图像时,wxpython框架不会打开,但是代码执行完美,没有任何错误。主要问题是框架无法打开。即使文件夹中没有图像,如何放置任何逻辑来显示框架。'''

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(

查看更多

查看更多

提问者
mihir bhatt
被浏览
56
Rolf of Saxony 2019-07-04 18:33

sizer即使没有图像需要放入一些东西
我没有编辑您的代码,而是更新了先前的答案,因为您的代码混合了位置放置和大小调整,这势必会给您带来麻烦。
以下是将在左侧,中间和右侧显示的视频的示例代码,它们是2个单独的滚动面板,监视2个单独的目录。
希望对您有所帮助。

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()

在此处输入图片说明