Search code examples
pythonwxpython

Why can't I draw onto two different bitmaps in wxPython?


I am trying to draw a button on a bitmap object. Depending on the y position, it should draw on bitmap1 if the y position is within bmp1's height value, and bitmap2 if it isn't. For some reason this does not work:

wx.Button(bitmap1 if ypos <= bmp1.GetHeight() else bitmap2, label='Run', id=i, pos=(xpos, ypos))

I can only draw the button on one wx.StaticBitmap image or the panel. The images parents are the panel.

This works fine if I want to switch between the bitmap or onto the panel directly.

What gives?

NOTE: I managed to work around this using PIL to create a dynamic image large enough to accomodate my generated buttons (a continuous y-size, according to their count and placement), however this idea/code should still be valid.

If I substitute the 'bitmap2' value for the panel, and shift the bitmap2 image drawn on the panel by a bit, then I see that the program draws underneath bitmap2. Why? The image is placed exactly like bitmap1, and bitmap1 has no problems being drawn on it by buttons? :O


Solution

  • I figured out the problem:

    The button's parent object should get the ypos according to the parent's dimensions, not on where it is drawn on the frame, like so:

    wx.Button(bitmap1 if ypos <= bmp1.GetHeight() else bitmap2, label='Run {i}', id=i, pos=(80, ypos if ypos <= bmp1.GetHeight() else ypos-img_height))
    

    ypos if ypos <= bmp1.GetHeight() else ypos-img_height

    Finally!