Python Forum
[WxPython] wx.StaticBitmap change image
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[WxPython] wx.StaticBitmap change image
#1
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()
Reply
#2
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?
Reply
#3
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
Reply
#4
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?
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How do I change an image dynamically LeeMadeux 7 21,487 Jun-04-2020, 10:00 PM
Last Post: deanhystad
  [WxPython] StaticBitmap: unexpected behaviour giu88 0 1,368 Oct-06-2018, 10:09 AM
Last Post: giu88

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020