Hi, I'm new, so please bear with me. I'm working on a memory card game in wxpython, using gui bitmap buttons.

I need to change the bitmap from the "face down" bitmap to another bitmap when the button is pressed. (and to wait for a second button to be pressed, then turn them both back to the first if they don't match, and remove them both if they do match, but that's a question for another day, I think....one thing at a time....)

Here is how I'm currently defining a "card".

c1 = wx.BitmapButton(self, -1, wx.Bitmap('/home/full/path/to/A.png'))

I need some help with the exactly syntax to use SetBitmapSelected to change the bitmap. I've read the online docs, and they are confusing to a novice programmer. No examples either.

Then I populate a grid with wx.GridSizer.

gs = wx.GridSizer(3, 4, 15, 15)
gs.Add(c1)
gs.Add(c2)
gs.Add(c3)

and so on......

My second question, I can't get a relative path to the .png to work, it has to be a full path. Just in case it matters, I'm using Ubuntu Ibex.

Any hints and tips are much appreciated. I can handle straight up scripting languages just fine, but I'm learning wxpython from web tutorials and pdf books, and well, they aren't as clear as I might like, at least to me.

I can post the rest of my simple program if you think it'll help.

Thanks much....

David

Recommended Answers

All 2 Replies

Use SetBitmapSelected() only if you want the different image to show as long as the button is pressed. For what you have in mind, you may want to use SetBitmapLabel():

# playing with wxPython's
# wx.BitmapButton(parent, id, bitmap, pos, size, style)
# show a different image for selected (depressed) button
# change the button image on button click 

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)

        # create a BitmapButton as an input widget
        # pick a button image file you have (.bmp .jpg .gif or .png)
        image_file1 = r".\cards\Deck1.gif"
        self.image1 = wx.Bitmap(image_file1)
        image_file2 = r".\cards\DK.gif"
        self.image2 = wx.Bitmap(image_file2)
        
        self.button = wx.BitmapButton(self, wx.ID_ANY, bitmap=self.image1,
            pos=(10, 20),
            size=(self.image1.GetWidth()+15, self.image1.GetHeight()+15))
        # bind mouse click event to an action
        self.button.Bind(wx.EVT_BUTTON, self.on_click)
        # create an output widget
        self.label = wx.StaticText(self, wx.ID_ANY, "", pos=(10, 100))

    def on_click(self, event):
        """response to button press/click"""
        # changes the images on button click for 2.5 seconds
        self.button.SetBitmapLabel(self.image2)
        self.Update()
        wx.Sleep(2.5)
        self.button.SetBitmapLabel(self.image1)


app = wx.App()
# create a MyFrame instance and show the frame
MyFrame(None, 'testing the wx.BitmapButton()', (400, 300)).Show()
app.MainLoop()

Thanks much... that does exactly what I need, at least for now. Now back to the google method of learning to program, slowly.

Thanks again...

David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.