【问题标题】:WXPython Display Images Sometimes do not DisplayWXPython 显示图像有时不显示
【发布时间】:2014-04-08 15:51:54
【问题描述】:

我目前正在使用 WXPython 做一个显示图像的 GUI。我目前正在以每秒 1 到 2 次的速度更改图像:

    image = image.Scale(scaledHeight, scaledWidth)
    image1 = image.ConvertToBitmap()

    # Center the image
    self.panel.bmp1 = wx.StaticBitmap(self.panel, -1, image1, ((width / 2) - (image.GetWidth() / 2), (height / 2) - (image.GetHeight() / 2)), (image.GetWidth(),image.GetHeight()))

唯一的问题是图像不定期显示。我尝试了一个低效的解决方案,并将图像复制到 image1、image2 等中,并显示所有这些,希望所有这些不显示的机会更低。不幸的是,图像仍将定期不显示。我需要使用某种缓冲区吗?

提前致谢!

【问题讨论】:

    标签: python image wxpython


    【解决方案1】:

    如果您可以提供您的代码 sn-p 以及有关您要实现的目标的更多详细信息,那将会很方便。

    我如何创建一个小示例来展示如何更改/更新面板上的图像。下面的代码基本上在myThread() 类中创建了一个随机数。然后使用publisher-subscriber 策略将该号码发送到gui() 类。 gui() 类的changeImage() 检查值是否小于或等于5,然后在名为self.myPanel 的面板上显示绿色图像,否则如果值大于5 则显示蓝色图像。

    图片可以从这里下载:green.bmpblue.bmp

    代码:请注意,图像文件应与此脚本所在的目录相同。

    import wx
    import time
    from wx.lib.pubsub import setupkwargs
    from wx.lib.pubsub import pub
    from threading import Thread
    import random
    
    class gui(wx.Frame):
    
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, None, id, title, size=(100,100))
            self.myPanel = wx.Panel(self, -1)
            image_file1 = 'green.bmp'
            image_file2 = 'blue.bmp'
            self.image1 = wx.Image(image_file1, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            self.image2 = wx.Image(image_file2, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            self.bitmap = wx.StaticBitmap(self.myPanel, -1)
            pub.subscribe(self.changeImage, 'Update')
    
        def changeImage(self, value):
            #To destroy any previous image on self.myPanel
            if self.bitmap:
                self.bitmap.Destroy()
            #If the value received from myThread() class is <= 5 then display the green image on myPanel
            if value <=5: 
                self.bitmap = wx.StaticBitmap(self.myPanel, -1, self.image1, (0, 0))
            #If the value received from myThread() class is > 5 then display the green image on myPanel
            else:
                self.bitmap = wx.StaticBitmap(self.myPanel, -1, self.image2, (10, 10))
    
    class myThread(Thread):
        def __init__(self):
            Thread.__init__(self)
            self.daemon = True
            self.start()
        def run(self):
            while True:
                number = random.randrange(1,10)
                wx.CallAfter(pub.sendMessage, 'Update', value=number)
                time.sleep(1)
    
    if __name__=='__main__':
        app = wx.App()
        frame = gui(parent=None, id=-1, title="Test")
        frame.Show()
        myThread()
        app.MainLoop()
    

    【讨论】:

      猜你喜欢
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      相关资源
      最近更新 更多