From Speedy Agouti, 3 Years ago, written in Python.
Embed
  1. import wx
  2.  
  3. if 'phoenix' in wx.PlatformInfo:
  4.     PHOENIX = True
  5.     CONTROL = wx.Control
  6. else:
  7.     PHOENIX = False
  8.     CONTROL = wx.PyControl
  9.  
  10.  
  11. class Frame(wx.Frame):
  12.  
  13.     def __init__(self, parent, *args, **kwargs):
  14.         wx.Frame.__init__(self, parent, *args, **kwargs)
  15.  
  16.         sizer = wx.BoxSizer()
  17.  
  18.         pnl = FatherPanel(self)
  19.         sizer.Add(pnl)
  20.  
  21.         self.SetSizerAndFit(sizer)
  22.  
  23.  
  24. class FatherPanel(wx.Panel):
  25.  
  26.     def __init__(self, parent, *args, **kwargs):
  27.         wx.Panel.__init__(self, parent, *args, **kwargs)
  28.  
  29.         sizer = wx.BoxSizer()
  30.  
  31.         pnl = ChildPanel(self)
  32.         sizer.Add(pnl, 0, wx.ALL, 50)
  33.  
  34.         self.SetSizer(sizer)
  35.  
  36.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  37.  
  38.     def GetSubImage(self, subwindow):
  39.         if hasattr(self, 'bg_image'):
  40.             rect = subwindow.GetRect()
  41.             return self.bg_image.GetSubImage(rect)
  42.  
  43.     def OnPaint(self, evt):
  44.         w, h = self.GetSize()
  45.  
  46.         if PHOENIX:
  47.             bmp_buffer = wx.Bitmap(w, h)
  48.         else:
  49.             bmp_buffer = wx.EmptyBitmap(w, h)
  50.  
  51.         dc = wx.BufferedPaintDC(self, bmp_buffer)
  52.         gc = wx.GraphicsContext.Create(dc)
  53.  
  54.         gradbrush = gc.CreateLinearGradientBrush(0, 0, 0, h, '#000000', '#ffffff')
  55.         gc.SetBrush(gradbrush)
  56.         gc.SetPen(wx.TRANSPARENT_PEN)
  57.         gc.DrawRectangle(0, 0, w, h)
  58.  
  59.         if PHOENIX:
  60.             self.bg_image = bmp_buffer.ConvertToImage()
  61.         else:
  62.             self.bg_image = wx.ImageFromBitmap(bmp_buffer)
  63.  
  64.  
  65. class ChildPanel(CONTROL):
  66.  
  67.     def __init__(self, parent, *args, **kwargs):
  68.         CONTROL.__init__(self, parent, id=wx.ID_ANY,
  69.                          style=wx.NO_BORDER | wx.TRANSPARENT_WINDOW, *args, **kwargs)
  70.  
  71.         self.parent = parent
  72.         self.SetFont(wx.Font(19, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
  73.         self.label = 'Yeah! Now it works'
  74.  
  75.         self.Bind(wx.EVT_SIZE, self.OnSize)
  76.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  77.  
  78.     def OnSize(self, evt):
  79.         self.Refresh()
  80.         evt.Skip()
  81.  
  82.     def OnPaint(self, evt):
  83.         dc = wx.BufferedPaintDC(self)
  84.         gc = wx.GraphicsContext.Create(dc)
  85.         dc.Clear()
  86.  
  87.         self.sub_img = self.parent.GetSubImage(self)
  88.         if self.sub_img:
  89.             dc.DrawBitmap(self.sub_img.ConvertToBitmap(), 0, 0, True)
  90.  
  91.         w, h = self.GetSize()
  92.         gc.SetPen(wx.Pen('#000000'))
  93.         gc.DrawRoundedRectangle(0, 0, w-1, h-1, 10)
  94.  
  95.         gc.SetFont(gc.CreateFont(self.GetFont(), '#000000'))
  96.         gc.DrawText(self.label, 0, 0)
  97.  
  98.         evt.Skip()
  99.  
  100.     def DoGetBestSize(self):
  101.         dc = wx.ClientDC(self)
  102.         dc.SetFont(self.GetFont())
  103.  
  104.         size = dc.GetTextExtent(self.label)
  105.  
  106.         if PHOENIX:
  107.             return wx.Size(size)
  108.         else:
  109.             return wx.Size(size[0], size[1])
  110.  
  111.  
  112. def main():
  113.     app = wx.App(False)
  114.  
  115.     frame = Frame(None)
  116.     frame.Center()
  117.     frame.Show()
  118.  
  119.     app.MainLoop()
  120.  
  121. if __name__ == '__main__':
  122.     main()
  123.