Your problem is in addImage. Each time this line executes:
myButton = wx.Button(self.bitmap2, -1, size =(30,30), pos=(20,20))
You are adding another child window to self.bitmap2. The window perfectly overlaps the previous one added, so it is not apparent that you have multiple children. To see what is happening, add this line to the bottom of addImage:
print len(self.bitmap2.GetChildren())
To fix this problem you should destroy all the children before you add the new button. Add this to the top of addImage
self.bitmap2.DestroyChildren()
However, this approach destroys ALL windows you have added to bitmap2, so tread lightly. If you want to only destroy the button, you should keep a reference to it. I've modified your program to use this approach:
import wx
from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub
from threading import Thread
import threading
import time
class gui(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, None, id, title, size=(500,400))
myPanel = wx.Panel(self, -1, size=(300,200))
image1 = wx.Image('green.bmp', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.bitmap2 = wx.StaticBitmap(myPanel, -1, image1, (0, 0))
pub.subscribe(self.addImage, 'Update')
self.myButton = None
def addImage(self):
# Don't keep adding children to bitmap2
if self.myButton:
self.myButton.Destroy()
self.myButton = wx.Button(self.bitmap2, -1, size =(30,30), pos=(20,20))
image2 = wx.Image('blue.bmp', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.bitmap1 = wx.StaticBitmap(self.myButton, -1, image2, (0, 0))
class myThread(Thread):
def __init__(self):
Thread.__init__(self)
self.start()
def run(self):
while True:
time.sleep(2)
wx.CallAfter(pub.sendMessage, 'Update')
if __name__=='__main__':
app = wx.App()
frame = gui(parent=None, id=-1, title="Test")
frame.Show()
myThread()
app.MainLoop()