Python Forum

Full Version: wx.StaticBitmap change image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,when I created a StaticBitmap, and a combobox. I had the idea that, as I would to change the image in StaticBitmap, from another event.
From combobox.

my code of StaticBitmap
self.m_bitmap1 = wx.StaticBitmap(self, wx.ID_LOGO, wx.Bitmap(u"example.png", wx.BITMAP_TYPE_ANY),
                                         wx.DefaultPosition, wx.DefaultSize, 0)
        self.m_bitmap1.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_ACTIVEBORDER))
        self.m_bitmap1.SetToolTip(u"logo of example")
        bSizer6.Add(self.m_bitmap1, 0, wx.ALIGN_CENTER | wx.ALL, 5)
event of combobox
    def option_combo(self, event):
        selection = self.comboBox_Dir.GetSelection()
        if selection == 0:
            pass      # change image in StaticBitmap


        #print(selection)
        #event.Skip()
I am a beginner with wxPython but I think you need to post the whole of your code and
most importantly what error did you get?
Well, it is not a mistake that I gave, rather I have the solution.
def option_combo(self, event):
    selection = self.comboBox_Dir.GetSelection()
    if selection == 0:
       self.m_bitmap1.SetBitmap(wx.Bitmap('example.jpg'))#change image in StaticBitmap
Okay. So as I am learning I need some explanations as to how this works. I could not see how to fit these into a working program. I still can't figure out how to use it.
Also the combobox part you posted originally is not the same as the one in your reply.
At least I can see how it would change things if I could understand how a value of 0 for selection would be how to trigger a change in the image. What would happen if it was some other value?
(Jul-02-2018, 05:49 AM)Barrowman Wrote: [ -> ]Okay. So as I am learning I need some explanations as to how this works. I could not see how to fit these into a working program. I still can't figure out how to use it.
Also the combobox part you posted originally is not the same as the one in your reply.
At least I can see how it would change things if I could understand how a value of 0 for selection would be how to trigger a change in the image. What would happen if it was some other value?
hi, hi:
for example,you have n images.
image1.png
image2.png
Be careful, it has to be of the same resolution.

in the code below it has no image, so pay attention in
def change_image( self, event ):


program.py
import wx
import wx.xrc

wx.ID_CHANGE = 1000

###########################################################################
## Class MyFrame1
###########################################################################

class MyFrame1 ( wx.Frame ):
	
	def __init__( self, parent ):
		wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
		
		self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
		
		bSizer2 = wx.BoxSizer( wx.VERTICAL )
		
		self.image = wx.BitmapButton( self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.DefaultSize, wx.BU_AUTODRAW )
		bSizer2.Add( self.image, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
		
		self.buton = wx.Button( self, wx.ID_CHANGE, u"Change", wx.DefaultPosition, wx.Size( 80,60 ), 0 )
		bSizer2.Add( self.buton, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
		
		
		self.SetSizer( bSizer2 )
		self.Layout()
		
		self.Centre( wx.BOTH )
		
		# Connect Events
		self.buton.Bind( wx.EVT_BUTTON, self.change_image )
	
	def __del__( self ):
		pass
	
	
	# Virtual event handlers, overide them in your derived class
	def change_image( self, event ):
		event.Skip()  #change code for: a)
a)
self.image.SetBitmap(wx.Bitmap('image1.jpg'))
I used selection== 1 , because I work with combobox and this offers a list for example.
in combobox the selection starts from 0 to n, n is the limit that you have given to the list, so my code works like this:
selection = 0
selection = 1
selection = 2

you just need to add the
if __main__== '__init__':
     #code execute class MyFrame1