코드 스 니펫을 제공 할 수 있고 달성하려는 내용에 대한 자세한 정보가 있으면 편리 할 것입니다.
패널에서 이미지를 변경/업데이트하는 방법을 보여주는 작은 예제를 어떻게 만듭니 까 만들었습니다. 아래의 코드는 기본적으로 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()