循环浏览 wx.StaticBitmap 中的图像
Cycling through images in wx.StaticBitmap
我正在处理图像数据库,并希望显示在 wx.StaticBitmap 对象中处理的每个图像。据我所知,onView 函数中的行 "self.imageCtrl.SetBitmap(wx.Bitmap(self.img))" 应该更改图像。但我所做的任何事情都无法让它显示出来。该代码来自我对答案的探索,是图像显示应用程序的一部分,每次单击按钮都会更改图像。这非常有效,但是一旦代码嵌入到循环中,它就无法刷新,直到循环完成,最后一个文件最终显示。
import time
import os
import wx
#============ App and panel class ========#
class PhotoCtrl(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
self.frame = wx.Frame(None, title='Photo Control')
self.panel = wx.Panel(self.frame)
self.PhotoMaxSize = 256
self.createWidgets()
self.frame.Show()
#=========== Set up button, wx.StaticBitmap etc ===============#
def createWidgets(self):
#instructions = 'Browse for an image'
img = wx.Image(256,256)
self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.Bitmap(img))
browseBtn = wx.Button(self.panel, label='Go')
browseBtn.Bind(wx.EVT_BUTTON, self.onView)
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY),
0, wx.ALL|wx.EXPAND, 5)
#self.mainSizer.Add(instructLbl, 0, wx.ALL, 5)
self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
#self.sizer.Add(self.photoTxt, 0, wx.ALL, 5)
self.sizer.Add(browseBtn, 0, wx.ALL, 5)
self.mainSizer.Add(self.sizer, 0, wx.ALL, 5)
self.panel.SetSizer(self.mainSizer)
self.mainSizer.Fit(self.frame)
self.panel.Layout()
#=== Toy code to simulate automatic change of image to be displayed ===#
def onView(self, event):
for i in range(1,10):
im_pth = os.getcwd() + f'/Cats/cats/Cats_{i}.png'
self.img = wx.Image(im_pth, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.image = self.img.ConvertToImage()
self.img = wx.Bitmap(self.image.Scale(256, 256))
self.imageCtrl.SetBitmap(wx.Bitmap(self.img))
self.panel.Refresh()
print(f"should be showing cat_{i}") #This prints but the image doesn't show
time.sleep(1)
if __name__ == '__main__':
app = PhotoCtrl()
app.MainLoop()
这是因为你在一个循环中,它不会将控制权释放回mainloop
。
您需要 Yield
在 time.sleep(1)
之前
即
def onView(self, event):
for i in range(1,10):
im_pth = os.getcwd() + f'/frame{i}.png'
self.img = wx.Image(im_pth, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.image = self.img.ConvertToImage()
self.img = wx.Bitmap(self.image.Scale(256, 256))
self.imageCtrl.SetBitmap(wx.Bitmap(self.img))
self.panel.Refresh()
wx.Yield()
print(f"should be showing cat_{i}") #This prints but the image doesn't show
time.sleep(1)
严格来说,在 wxPython Phoenix 下应该是 wx.GetApp().Yield()
但 wx.Yield()
仍然有效。
我正在处理图像数据库,并希望显示在 wx.StaticBitmap 对象中处理的每个图像。据我所知,onView 函数中的行 "self.imageCtrl.SetBitmap(wx.Bitmap(self.img))" 应该更改图像。但我所做的任何事情都无法让它显示出来。该代码来自我对答案的探索,是图像显示应用程序的一部分,每次单击按钮都会更改图像。这非常有效,但是一旦代码嵌入到循环中,它就无法刷新,直到循环完成,最后一个文件最终显示。
import time
import os
import wx
#============ App and panel class ========#
class PhotoCtrl(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
self.frame = wx.Frame(None, title='Photo Control')
self.panel = wx.Panel(self.frame)
self.PhotoMaxSize = 256
self.createWidgets()
self.frame.Show()
#=========== Set up button, wx.StaticBitmap etc ===============#
def createWidgets(self):
#instructions = 'Browse for an image'
img = wx.Image(256,256)
self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.Bitmap(img))
browseBtn = wx.Button(self.panel, label='Go')
browseBtn.Bind(wx.EVT_BUTTON, self.onView)
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY),
0, wx.ALL|wx.EXPAND, 5)
#self.mainSizer.Add(instructLbl, 0, wx.ALL, 5)
self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
#self.sizer.Add(self.photoTxt, 0, wx.ALL, 5)
self.sizer.Add(browseBtn, 0, wx.ALL, 5)
self.mainSizer.Add(self.sizer, 0, wx.ALL, 5)
self.panel.SetSizer(self.mainSizer)
self.mainSizer.Fit(self.frame)
self.panel.Layout()
#=== Toy code to simulate automatic change of image to be displayed ===#
def onView(self, event):
for i in range(1,10):
im_pth = os.getcwd() + f'/Cats/cats/Cats_{i}.png'
self.img = wx.Image(im_pth, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.image = self.img.ConvertToImage()
self.img = wx.Bitmap(self.image.Scale(256, 256))
self.imageCtrl.SetBitmap(wx.Bitmap(self.img))
self.panel.Refresh()
print(f"should be showing cat_{i}") #This prints but the image doesn't show
time.sleep(1)
if __name__ == '__main__':
app = PhotoCtrl()
app.MainLoop()
这是因为你在一个循环中,它不会将控制权释放回mainloop
。
您需要 Yield
在 time.sleep(1)
即
def onView(self, event):
for i in range(1,10):
im_pth = os.getcwd() + f'/frame{i}.png'
self.img = wx.Image(im_pth, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.image = self.img.ConvertToImage()
self.img = wx.Bitmap(self.image.Scale(256, 256))
self.imageCtrl.SetBitmap(wx.Bitmap(self.img))
self.panel.Refresh()
wx.Yield()
print(f"should be showing cat_{i}") #This prints but the image doesn't show
time.sleep(1)
严格来说,在 wxPython Phoenix 下应该是 wx.GetApp().Yield()
但 wx.Yield()
仍然有效。