我目前正在使用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等,并显示所有这些解决方案,希望所有这些图像都不会显示为更低。不幸的是,图像仍会定期显示。我需要使用某种缓冲区吗?
提前致谢!
答案 0 :(得分:0)
如果您可以提供代码片段以及有关您要实现的更多详细信息,那将会很方便。
我怎么创建了一个小例子,展示了如何在面板上更改/更新图像。下面的代码基本上在myThread()
类中创建一个随机数。然后使用publisher-subscriber策略将该号码发送到gui()
班级。 changeImage()
类的gui()
会检查该值是否小于或等于5
,然后它会在名为self.myPanel
的面板上显示绿色图像,否则值大于5
,然后显示蓝色图像。
代码:请注意,图片文件应位于此脚本所在的目录中。
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()