Customized text (Phoenix)

Keywords : StaticText, Transparent background, Customized text.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Text with transparent background

img_sample_one.png

Note : works fine for Windows, Linux and Mac OS.

   1 # sample_one.py
   2 
   3 import wx
   4 
   5 # class MyStaticText
   6 # class MyFrame
   7 # class MyApp
   8 
   9 #---------------------------------------------------------------------------
  10 
  11 class MyStaticText(wx.Window):
  12     """
  13     Thanks to Alain Delgrange.
  14     """
  15     def __init__(self, parent, label):
  16         wx.Window.__init__(self, parent, -1)
  17 
  18         #------------
  19 
  20         # Attributes.
  21         self.parent = parent
  22         self.label = label
  23 
  24         #------------
  25 
  26         # Simplified init method.
  27         self.SetBackground()
  28         self.SetProperties()
  29         self.BindEvents()
  30 
  31     #-----------------------------------------------------------------------
  32 
  33     def SetBackground(self):
  34         """
  35         ...
  36         """
  37 
  38         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  39 
  40 
  41     def SetProperties(self):
  42         """
  43         Set the properties.
  44         """
  45 
  46         self.police = wx.Font(12, wx.FONTFAMILY_DEFAULT,
  47                               wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
  48 
  49         width, height = self.GetFullTextExtent(self.label, font=self.police)[0:2]
  50         self.SetSize((width, height))
  51 
  52 
  53     def BindEvents(self):
  54         """
  55         Bind all the events related to my dialog.
  56         """
  57 
  58         # Bind a event to an events handler.
  59         self.Bind(wx.EVT_PAINT, self.OnPaint)
  60 
  61 
  62     def OnPaint(self, event):
  63         """
  64         ...
  65         """
  66 
  67         x, y = self.GetPosition()
  68         dc = wx.PaintDC(self)
  69         l, h = dc.GetFullTextExtent(self.label, font=self.police)[0:2]
  70         self.SetSize((l, h))
  71         dc = wx.PaintDC(self)
  72         dc2 = wx.BufferedPaintDC(self.parent)
  73         dc.Blit(0, 0, l, h, dc2, x, y)
  74         dc.SetTextBackground(wx.NullColour)
  75         dc.SetTextForeground(wx.WHITE)
  76         dc.DrawText(self.label, 0, 0)
  77 
  78 #---------------------------------------------------------------------------
  79 
  80 class MyFrame(wx.Frame):
  81     """
  82     Thanks to Alain Delgrange.
  83     """
  84     def __init__(self):
  85         style = (wx.DEFAULT_FRAME_STYLE)
  86         wx.Frame.__init__(self,
  87                           None,
  88                           -1,
  89                           title="",
  90                           size=(380, 130),
  91                           style=style)
  92 
  93         #------------
  94 
  95         # Simplified init method.
  96         self.SetProperties()
  97         self.CreateCtrls()
  98         self.BindEvents()
  99 
 100     #-----------------------------------------------------------------------
 101 
 102     def SetProperties(self):
 103         """
 104         Set the frame properties (title, icon...).
 105         """
 106 
 107         self.SetTitle("Text with transparent background")
 108 
 109 
 110     def CreateCtrls(self):
 111         """
 112         Make widgets for my frame.
 113         """
 114 
 115         self.pnl = wx.Panel(self,
 116                             -1,
 117                             size=self.GetClientSize(),
 118                             style=wx.TAB_TRAVERSAL |
 119                                   wx.CLIP_CHILDREN)
 120         self.pnl.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
 121 
 122         self.staticTxt = MyStaticText(self.pnl, "Hello world !")
 123         self.staticTxt.CentreOnParent()
 124 
 125 
 126     def BindEvents(self):
 127         """
 128         Bind all the events related to my frame.
 129         """
 130 
 131         # Bind some events to an events handler.
 132         self.Bind(wx.EVT_SIZE, self.OnResize)
 133         self.pnl.Bind(wx.EVT_PAINT, self.OnPaint)
 134 
 135 
 136     def OnResize(self, event):
 137         """
 138         ...
 139         """
 140 
 141         self.pnl.SetSize(self.GetClientSize())
 142         self.pnl.Refresh()
 143         self.staticTxt.CentreOnParent()
 144 
 145 
 146     def OnPaint(self, event=None):
 147         """
 148         ...
 149         """
 150 
 151         size = self.pnl.GetRect()
 152 
 153         dc = wx.BufferedPaintDC(self.pnl)
 154         dc.GradientFillLinear((0,
 155                                0,
 156                                size[2]/2,
 157                                size[3]),
 158                                wx.RED,
 159                                wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENU),
 160                                wx.WEST)
 161 
 162         dc.GradientFillLinear((size[2]/2,
 163                                0,
 164                                size[2]/2,
 165                                size[3]),
 166                                wx.RED,
 167                                wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENU),
 168                                wx.EAST)
 169 
 170 #---------------------------------------------------------------------------
 171 
 172 class MyApp(wx.App):
 173     """
 174     ...
 175     """
 176     def OnInit(self):
 177 
 178         #------------
 179 
 180         frame = MyFrame()
 181         self.SetTopWindow(frame)
 182         frame.Show(True)
 183 
 184         return True
 185 
 186 #---------------------------------------------------------------------------
 187 
 188 def main():
 189     app = MyApp(False)
 190     app.MainLoop()
 191 
 192 #---------------------------------------------------------------------------
 193 
 194 if __name__ == "__main__" :
 195     main()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Alain Delgrange (sample_one.py coding) and the wxPython community...


About this page

Date (d/m/y) Person (bot) Comments :

16/03/18 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah...

Customized text (Phoenix) (last edited 2019-08-31 13:50:18 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.