用wxPython问题生成连续的位图

时间:2021-11-09 20:58:54

Well, while I don't consider myself an experienced programmer, especially when it comes to multimedia applications, I had to do this image viewer sort of a program that displays images fullscreen, and the images change when the users press <- or -> arrows.

好吧,虽然我不认为自己是一位经验丰富的程序员,特别是涉及到多媒体应用程序时,我不得不做这个图像查看器,这是一个全屏显示图像的程序,当用户按< - 或 - >时图像会改变箭头。

The general idea was to make a listbox where all the images contained in a certain folder (imgs) are shown, and when someone does double click or hits RETURN a new, fullscreen frame is generated containing, at first, the image selected in the listbox but after the user hits the arrows the images change in conecutive order, just like in a regular image viewer.

一般的想法是制作一个列表框,其中显示某个文件夹(imgs)中包含的所有图像,当有人双击或点击时返回一个新的全屏帧,其中首先包含在列表框中选择的图像但是在用户点击箭头后,图像会以连续顺序变化,就像在常规图像查看器中一样。

At the beginning, when the first 15 - 20 images are generated, everything goes well, after that, although the program still works an intermediary, very unpleasant and highly unwanted, effect appears between image generations, where basically some images that got generated previously are displayed quickly on the screen and after this the right, consecutive image appears. On the first apparitions the effect is barelly noticeble, but after a while it takes longer and longer between generations.

一开始,当生成前15-20张图像时,一切顺利,之后,虽然程序仍然工作中介,非常不愉快和非常不需要的效果出现在图像生成之间,其中基本上先前生成的一些图像是在屏幕上快速显示,然后显示正确的连续图像。在第一个幻影中,这种效果几乎是显而易见的,但过了一段时间后,它们需要更长的时间和更长的时间。

Here's the code that runs when someone does double click on a listbox entry:

这是当有人双击列表框条目时运行的代码:

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)

Thanks for any advice in advance.

在此先感谢您的任何建议。

Added later:

The catch is I'm trying to do an autorun presentation for a DVD with lots of images on it. Anyway it's not necessarely to make the above piece of code work properly if there are any other options. I've already tried to use windows image viewer, but strangely enough it doesn't recognizes relative paths and when I do this

问题是,我正在尝试为DVD上进行自动运行演示,其中包含大量图像。无论如何,如果有任何其他选项,则无需使上述代码正常工作。我已经尝试过使用Windows图像查看器了,但奇怪的是它不能识别相对路径,当我这样做时

path = os.getcwd() # getting the path of the current working directory
sel = listbox.GetSelection() # geting the value of the current selection from the list box
imgname = memlist[sel] # retrieving the name of the images stored in a list, using the listbox selection, that uses as choices the same list
os.popen(str('rundll32.exe C:\WINDOWS\System32\shimgvw.dll,ImageView_Fullscreen {0}/imgsdir/{1}'.format(path, imgname))) # oepning the images up with bloody windows image viewer

it opens the images only when my program is on the hard disk, if it's on a CD / image drive it doesn't do anything.

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

1 个解决方案

#1


0  

There's an image viewer included with the wxPython demo package. I also wrote a really simple one here. Either one should help you in your journey. I suspect that you're not reusing your panels/frames and instead you're seeing old ones that were never properly destroyed.

wxPython演示包中包含一个图像查看器。我在这里写了一个非常简单的。任何人都应该帮助你完成旅程。我怀疑你没有重复使用你的面板/框架,而是你看到的旧的从未被正确销毁过。

#1


0  

There's an image viewer included with the wxPython demo package. I also wrote a really simple one here. Either one should help you in your journey. I suspect that you're not reusing your panels/frames and instead you're seeing old ones that were never properly destroyed.

wxPython演示包中包含一个图像查看器。我在这里写了一个非常简单的。任何人都应该帮助你完成旅程。我怀疑你没有重复使用你的面板/框架,而是你看到的旧的从未被正确销毁过。