Python 3.9.5
wxPython 4.2.0
I’m trying to fix a logo at at corner of a panel. If the panel get resize, the logo should move (or redrawn). It is not good to make a StaticBitmap and set its position because of z-order issues. So I tried using PAINT or ERASE_BACKGROUND events like the attached code.
However it works well for macOS but bad for Windows.
Anyone has suggestions?
import wx
import base64
from io import BytesIO
class BackgroundPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
img_b64 = "iVBORw0KGgoAAAANSUhEUgAAAEcAAABHCAMAAABibqotAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAzUExURQAAAP8AAP8AAP8AAP8AAP8AAP8AAP8AAP8AAP8AAP8AAP8AAP8AAP8AAP8AAP8AAP8AAOmONxAAAAAQdFJOUwAQIDBAUGBwgI+fr7/P3+8jGoKKAAAACXBIWXMAAA7DAAAOwwHHb6hkAAACbklEQVRYR+2X65KrIBCEMyIaFYX3f9rTc0F0427A1PmXr2orY1ZbaGYG8vjy5f+weAsyFDqLWvDpRSfOFrWwRLJoZ4oWNEBxsqjg0mBRPWPqLTqwLRbUs6wWHJnSy1zfcTEtTPbF+3e4yycojRbV0qfLXGk2aLzWCcGCWobiqF+KVR/ojCml/ekbOss0OAQuQifxiMg/Q2xOoGfg55d54w8IBejFdb5TqR0GoCpgnUbXnISFXejOQApkKqk5Ac94U4FT9k0D5AR2o+hwC7N/yE0VzPYomk2Z1xO+29rFSq+eersI5XilXSaFtnXDUkFo5KfjJDKxtWsoKiQr330go0I9y6T5ExkRUlPibZl+4r1BJiWwTP/k71qgCWW5ZndUpl/wuTbVR4/ZrPzU0IkK6l1V8IdEqoQmvH8kx/mYc4a7UPAPxy+oTGgejJ4IRCkDFYYT82pLOtF77zH+uBf3rmQqwGFy6+j9xXabsaqUwVCn2MRGvZKK4I4Nfj980Lptm76Z2NILZBQ0476tKgfyOv2ktZ1BpyOBBxb7HN/R0WiATj57hNs6vDqMmnFbZ+9dKnRbpySiHA3v6nSyKSuscFenbBe68bTroA1CxzJX4DULDdUu0CZnlX3nAJteN3VFWlLk7nDUkRwK+n0tk3Xjoz9SlhjnVr97wRb1oWynubLgf/WRDEew3Kn2NLRKlwV428YUJA12YaUYtB/v0XSrvCZ4yaksnM4JBhpmjdfzacfLhXH4EYXu8fqT6gV4fMxZ+Coc849ihdfrDxthBzgvNrx+OyA3/LhF+6FdGO6PzeI36GzYB7w39cuXT3g8/gGjSCvxOsZEaAAAAABJRU5ErkJggg=="
img_bytes = base64.b64decode(img_b64.encode("ascii"))
img_io = BytesIO(img_bytes)
img_wx = wx.Image(img_io, wx.BITMAP_TYPE_PNG)
self.bmp = wx.Bitmap(img_wx)
self.bmp_size = self.bmp.GetSize()
img_io.close()
self.SetBackgroundStyle(wx.BG_STYLE_PAINT)
self.Bind(wx.EVT_PAINT, self.on_paint)
"""
Below works incorretly also.
"""
# self.SetBackgroundStyle(wx.BG_STYLE_ERASE)
# self.Bind(wx.EVT_ERASE_BACKGROUND, self.on_erase)
def on_paint(self, event):
dc = wx.PaintDC(self)
dc.Clear()
size_dc = dc.GetSize()
margin = 15
dc.DrawBitmap(
self.bmp,
size_dc[0]-self.bmp_size[0]-margin,
size_dc[1]-self.bmp_size[1]-margin
)
# def on_erase(self, event):
# dc = event.GetDC()
# dc.Clear()
# size_dc = dc.GetSize()
# margin = 15
# dc.DrawBitmap(
# self.bmp,
# size_dc[0]-self.bmp_size[0]-margin,
# size_dc[1]-self.bmp_size[1]-margin
# )
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="TEST")
panel = BackgroundPanel(self)
n_row = 5
n_col = 5
sz_grid = wx.GridSizer(n_row, n_col, 30, 30)
for i in range(n_col):
for j in range(n_row):
bt = wx.Button(panel, label=f"{i}{j}")
sz_grid.Add(bt)
sz_panel = wx.BoxSizer(wx.VERTICAL)
sz_panel.AddMany((
((-1, -1), 1),
(sz_grid, 0, wx.ALIGN_CENTER_HORIZONTAL),
((-1, -1), 1)
))
sz_main = wx.BoxSizer(wx.VERTICAL)
sz_main.Add(sz_panel, 1, wx.EXPAND|wx.ALL, 50)
panel.SetSizerAndFit(sz_main)
self.SetSize(self.GetBestSize())
if __name__ == "__main__":
app = wx.App()
TestFrame().Show()
app.MainLoop()