使用wxPython troub生成连续位图

2024-10-01 04:44:13 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,虽然我不认为自己是一个有经验的程序员,尤其是在多媒体应用程序方面,我不得不做一个图像查看器程序,它可以全屏显示图像,当用户按下箭头键时,图像会发生变化。在

一般的想法是制作一个列表框,其中显示某个文件夹(imgs)中包含的所有图像,当有人双击或点击返回时,会生成一个新的全屏框架,最初包含在列表框中选定的图像,但当用户点击箭头后,图像按连续顺序变化,就像普通的图像查看器一样。在

一开始,当第一个15-20个图像被生成时,一切都很顺利,之后,虽然程序仍然起着中介作用,但是非常不愉快和非常不必要的作用,在图像生成之间会出现效果,基本上以前生成的一些图像会快速显示在屏幕上,然后再显示在右侧,出现连续图像。在最初的幻影中,这种效果几乎是显而易见的,但过了一段时间,代际之间的时间就越来越长了。在

以下是双击列表框条目时运行的代码:

def lbclick(self, eve):
    frm = wx.Frame(None, -1, '')
    frm.ShowFullScreen(True)
    self.sel = self.lstb.GetSelection() # getting the selection from the listbox
    def pressk(eve):
        keys = eve.GetKeyCode()
        if keys == wx.WXK_LEFT:
            self.sel = self.sel - 1
            if self.sel < 0:
                self.sel = len(self.chk) - 1
            imgs() # invocking the function made for displaying fullscreen images when left arrow key is pressed
        elif keys == wx.WXK_RIGHT:
            self.sel = self.sel + 1
            if self.sel > len(self.chk) - 1:
               self.sel = 0
            imgs() # doing the same for the right arrow 
       elif keys == wx.WXK_ESCAPE:
            frm.Destroy()       
       eve.Skip()
    frm.Bind(wx.EVT_CHAR_HOOK, pressk)
    def imgs(): # building the function
        imgsl = self.chk[self.sel]
        itm = wx.Image(str('imgs/{0}'.format(imgsl)), wx.BITMAP_TYPE_JPEG).ConvertToBitmap() # obtaining the name of the image stored in the list          self.chk
        mar = itm.Size   # Because not all images are landscaped I had to figure a method to rescale them after height dimension, which is common to all images
        frsz = frm.GetSize()
        marx = float(mar[0])
        mary = float(mar[1])
        val = frsz[1] / mary
        vsize = int(mary * val)
        hsize = int(marx * val)
        panl = wx.Panel(frm, -1, size = (hsize, vsize), pos = (frsz[0] / 2 -  hsize / 2, 0)) # making a panel container
        panl.SetBackgroundColour('#000000')
        imag = wx.Image(str('imgs/{0}'.format(imgsl)), wx.BITMAP_TYPE_JPEG).Scale(hsize, vsize, quality = wx.IMAGE_QUALITY_NORMAL).ConvertToBitmap()
        def destr(eve): # unprofessionaly trying to destroy the panel container when a new image is generated hopeing the unvanted effect, with previous generated images will disappear. But it doesn't.
            keycd = eve.GetKeyCode()
            if keycd == wx.WXK_LEFT or keycd == wx.WXK_RIGHT:
                try:
                    panl.Destroy()
                except:
                    pass
            eve.Skip()
        panl.Bind(wx.EVT_CHAR_HOOK, destr) # the end of my futile tries
        if vsize > hsize: # if the image is portrait instead of landscaped I have to put a black image as a container, otherwise in the background the previous image will remain, even if I use Refresh() on the container (the black bitmap named fundal)
            intermed = wx.Image('./res/null.jpg', wx.BITMAP_TYPE_JPEG).Scale(frsz[0], frsz[1]).ConvertToBitmap()
            fundal = wx.StaticBitmap(frm, 101, intermed)
            stimag = wx.StaticBitmap(fundal, -1, imag, size = (hsize, vsize), pos = (frsz[0] / 2 -  hsize / 2, 0))
            fundal.Refresh()
            stimag.SetToolTip(wx.ToolTip('Esc = exits fullscreen\n<- -> arrows = quick navigation'))
            def destr(eve): # the same lame attempt to destroy the container in the portarit images situation
                keycd = eve.GetKeyCode()
                if keycd == wx.WXK_LEFT or keycd == wx.WXK_RIGHT:
                    try:
                        fundal.Destroy()
                    except:
                        pass
                eve.Skip()
            frm.Bind(wx.EVT_CHAR_HOOK, destr)
        else: # the case when the images are landscape
            stimag = wx.StaticBitmap(panl, -1, imag)
            stimag.Refresh()
            stimag.SetToolTip(wx.ToolTip('Esc = exits fullscreen\n<- -> arrows = quick navigation'))
    imgs() # invocking the function imgs for the situation when someone does double click
    frm.Center()
    frm.Show(True)

谢谢你事先的建议。在

稍后添加:

问题是我正在尝试为一个DVD做一个自动运行演示,上面有很多图像。不管怎样,如果有其他选择的话,没有必要让上面的代码正常工作。我已经尝试过使用windows图像查看器,但奇怪的是,当我这样做时,它无法识别相对路径

^{pr2}$

它只在我的程序在硬盘上时打开图像,如果它在CD/映像驱动器上,它什么也不做。在


Tags: theto图像selfifevewximages
1条回答
网友
1楼 · 发布于 2024-10-01 04:44:13

wxPython演示包中包含一个图像查看器。{我还写了一个简单的。任何一个都应该在你的旅途中帮助你。我怀疑你没有重复使用你的面板/框架,相反,你看到的是那些从未被妥善销毁的旧面板/框架。在

相关问题 更多 >