How to create a customized news-ticker (Phoenix)

Keywords : Ticker, News-ticker, Scrolling text control, Fade text, Gradient, Horizontal text, Vertical 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 ! (!)


Sample one

img_sample_one.png

   1 # sample_one.py
   2 
   3 import wx
   4 
   5 # class MyPanel
   6 # class MyFrame
   7 # class MyApp
   8 
   9 SCALE = 0.5
  10 
  11 #---------------------------------------------------------------------------
  12 
  13 class MyPanel(wx.Panel):
  14     """
  15     Create a panel with a canvas to draw on.
  16     """
  17 
  18     def __init__(self, parent, size):
  19         wx.Panel.__init__(self, parent, -1, size=size)
  20 
  21         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  22 
  23         #------------
  24 
  25         w, h = self.GetClientSize()
  26 
  27         #------------
  28 
  29         # Initial x position of image
  30         self.x = 440
  31 
  32         #------------
  33 
  34         self.timer = wx.Timer(self)
  35         # Start(int milliseconds, oneShot=False).
  36         # Lower timer interval gives faster speed.
  37         self.timer.Start(10)
  38         self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
  39 
  40         #------------
  41 
  42         # Simplified init method.
  43         self.BindEvents()
  44 
  45     #-----------------------------------------------------------------------
  46 
  47     def BindEvents(self):
  48         """
  49         Bind some events to an events handler.
  50         """
  51 
  52         self.Bind(wx.EVT_SIZE, self.OnResize)
  53         self.Bind(wx.EVT_PAINT, self.OnPaint)
  54         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
  55         # self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
  56 
  57 
  58     def OnMouseEvents(self, event):
  59         """
  60         ...
  61         """
  62 
  63         if event.LeftDown() :
  64             self.timer.Stop()
  65         else:
  66             self.timer.Start()
  67         event.Skip()
  68 
  69 
  70     def OnErase(self, evt):
  71         """
  72         Noop because of double buffering.
  73         """
  74 
  75         pass
  76 
  77 
  78     def OnResize(self, event):
  79         """
  80         Refresh widget after resizing.
  81         """
  82 
  83         self.Refresh()
  84         event.Skip()
  85 
  86 
  87     def OnPaint(self, event=None):
  88         """
  89         Create the paint canvas.
  90         """
  91 
  92         dc = wx.BufferedPaintDC(self)
  93         brush = wx.Brush("#009fe3")
  94         dc.SetBackground(brush)
  95         dc.Clear()
  96 
  97         #------------
  98 
  99         font = dc.GetFont()
 100         font.SetWeight(wx.BOLD)
 101         font.SetPointSize(12)
 102         # font.SetFaceName("Tahoma")
 103         dc.SetFont(font)
 104 
 105         #------------
 106 
 107         width, height = self.GetClientSize()
 108 
 109         #------------
 110 
 111         txt = "Loop for the text !!!"
 112 
 113         w, h = dc.GetTextExtent(txt)
 114 
 115         #------------
 116 
 117         # Draw to a bitmap and scale that.
 118         bmp = wx.Bitmap(width, h)
 119         mdc = wx.MemoryDC(bmp)
 120         brush = wx.Brush("#009fe3")
 121         mdc.SetBackground(brush)
 122         mdc.Clear()
 123         mdc.SetFont(font)
 124         mdc.SetTextForeground("white")
 125         mdc.DrawText(txt, int(self.x), 0)
 126         del mdc
 127 
 128         # wx.IMAGE_QUALITY_NEAREST, wx.IMAGE_QUALITY_BILINEAR,
 129         # wx.IMAGE_QUALITY_BICUBIC, wx.IMAGE_QUALITY_BOX_AVERAGE,
 130         # wx.IMAGE_QUALITY_NORMAL, wx.IMAGE_QUALITY_HIGH
 131         image = bmp.ConvertToImage()
 132         image = image.Scale(int(width*SCALE*2), int(height*SCALE*2), wx.IMAGE_QUALITY_HIGH)
 133         bmp = wx.Bitmap(image)
 134 
 135         dc.DrawBitmap(bmp, 0, 0)
 136 
 137 
 138     def OnTimer(self, event):
 139         """
 140         ...
 141         """
 142 
 143         w, h = self.GetClientSize()
 144 
 145         #------------
 146 
 147         # Increment x to start moving.
 148         self.x += -0.5
 149         # Optional continuous movement
 150         # experiment with wrap around values.
 151         if self.x > (w+100) or self.x < -w/2:
 152             self.x = w
 153 
 154         self.Refresh()
 155 
 156 #---------------------------------------------------------------------------
 157 
 158 class MyFrame(wx.Frame):
 159     """
 160     ...
 161     """
 162 
 163     def __init__(self):
 164         wx.Frame.__init__(self, None,
 165                           title="Horizontal news-ticker (image/scale)",
 166                           size=(400, 80))
 167 
 168         #------------
 169 
 170         # Simplified init method.
 171         self.SetProperties()
 172         self.CreateCtrls()
 173         self.DoLayout()
 174 
 175         #------------
 176 
 177         self.CenterOnScreen()
 178 
 179     #-----------------------------------------------------------------------
 180 
 181     def SetProperties(self):
 182         """
 183         ...
 184         """
 185 
 186         self.SetIcon(wx.Icon('icons/wxwin.ico'))
 187         self.SetMinSize((100, 80))
 188         self.SetBackgroundColour("yellow")
 189 
 190 
 191     def CreateCtrls(self):
 192         """
 193         ...
 194         """
 195 
 196         size = self.GetClientSize()
 197 
 198         self.pnl = MyPanel(self, size=size)
 199 
 200 
 201     def DoLayout(self):
 202         """
 203         ...
 204         """
 205 
 206         mainSizer = wx.BoxSizer(wx.VERTICAL)
 207 
 208         #------------
 209 
 210         mainSizer.Add(self.pnl, 1, wx.EXPAND)
 211 
 212         #------------
 213 
 214         self.SetSizer(mainSizer)
 215         mainSizer.Layout()
 216 
 217 #---------------------------------------------------------------------------
 218 
 219 class MyApp(wx.App):
 220     """
 221     ...
 222     """
 223     def OnInit(self):
 224 
 225         #------------
 226 
 227         frame = MyFrame()
 228         self.SetTopWindow(frame)
 229         frame.Show(True)
 230 
 231         return True
 232 
 233 #---------------------------------------------------------------------------
 234 
 235 def main():
 236     app = MyApp(False)
 237     app.MainLoop()
 238 
 239 #---------------------------------------------------------------------------
 240 
 241 if __name__ == "__main__" :
 242     main()


Sample two

img_sample_two.png

   1 # sample_two.py
   2 
   3 import wx
   4 
   5 # class MyPanel
   6 # class MyFrame
   7 # class MyApp
   8 
   9 SCALE = 0.5
  10 
  11 #---------------------------------------------------------------------------
  12 
  13 class MyPanel(wx.Panel):
  14     """
  15     Create a panel with a canvas to draw on.
  16     """
  17 
  18     def __init__(self, parent, size):
  19         wx.Panel.__init__(self, parent, -1, size=size)
  20 
  21         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  22 
  23         #------------
  24 
  25         w, h = self.GetClientSize()
  26 
  27         #------------
  28 
  29         # Initial x position of image
  30         self.y = h
  31 
  32         #------------
  33 
  34         self.timer = wx.Timer(self)
  35         # Start(int milliseconds, oneShot=False).
  36         # Lower timer interval gives faster speed.
  37         self.timer.Start(10)
  38         self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
  39 
  40         #------------
  41 
  42         # Simplified init method.
  43         self.BindEvents()
  44 
  45     #-----------------------------------------------------------------------
  46 
  47     def BindEvents(self):
  48         """
  49         Bind some events to an events handler.
  50         """
  51 
  52         self.Bind(wx.EVT_SIZE, self.OnResize)
  53         self.Bind(wx.EVT_PAINT, self.OnPaint)
  54         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
  55         # self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
  56 
  57 
  58     def OnMouseEvents(self, event):
  59         """
  60         ...
  61         """
  62 
  63         if event.LeftDown() :
  64             self.timer.Stop()
  65         else:
  66             self.timer.Start()
  67         event.Skip()
  68 
  69 
  70     def OnErase(self, evt):
  71         """
  72         Noop because of double buffering.
  73         """
  74 
  75         pass
  76 
  77 
  78     def OnResize(self, event):
  79         """
  80         Refresh widget after resizing.
  81         """
  82 
  83         self.Refresh()
  84         event.Skip()
  85 
  86 
  87     def OnPaint(self, event=None):
  88         """
  89         Create the paint canvas.
  90         """
  91 
  92         dc = wx.BufferedPaintDC(self)
  93         brush = wx.Brush("yellow")
  94         dc.SetBackground(brush)
  95         dc.Clear()
  96 
  97         #------------
  98 
  99         font = dc.GetFont()
 100         font.SetWeight(wx.BOLD)
 101         font.SetPointSize(12)
 102         # font.SetFaceName("Tahoma")
 103         dc.SetFont(font)
 104 
 105         #------------
 106 
 107         width, height = self.GetClientSize()
 108 
 109         #------------
 110 
 111         txt = "Loop for the text !!!"
 112 
 113         w, h = dc.GetTextExtent(txt)
 114 
 115         #------------
 116 
 117         # Draw to a bitmap and scale that.
 118         bmp = wx.Bitmap(width, h)
 119         mdc = wx.MemoryDC(bmp)
 120         brush = wx.Brush("yellow")
 121         mdc.SetBackground(brush)
 122         mdc.Clear()
 123         mdc.SetFont(font)
 124         mdc.SetTextForeground("gray")
 125         mdc.DrawText(txt, int(width/2-(w/2)), int(self.y))
 126         del mdc
 127 
 128         # wx.IMAGE_QUALITY_NEAREST, wx.IMAGE_QUALITY_BILINEAR,
 129         # wx.IMAGE_QUALITY_BICUBIC, wx.IMAGE_QUALITY_BOX_AVERAGE,
 130         # wx.IMAGE_QUALITY_NORMAL, wx.IMAGE_QUALITY_HIGH
 131         image = bmp.ConvertToImage()
 132         image = image.Scale(int(width*SCALE*2), int(height*SCALE*2), wx.IMAGE_QUALITY_HIGH)
 133         bmp = wx.Bitmap(image)
 134 
 135         dc.DrawBitmap(bmp, 0, 0)
 136 
 137 
 138     def OnTimer(self, event):
 139         """
 140         ...
 141         """
 142 
 143         w, h = self.GetClientSize()
 144 
 145         #------------
 146 
 147         # Increment x to start moving.
 148         self.y += -0.5
 149         # Optional continuous movement
 150         # experiment with wrap around values.
 151         if self.y > (h) or self.y < -h/2:
 152             self.y = h
 153 
 154         self.Refresh()
 155 
 156 #---------------------------------------------------------------------------
 157 
 158 class MyFrame(wx.Frame):
 159     """
 160     ...
 161     """
 162 
 163     def __init__(self):
 164         wx.Frame.__init__(self, None,
 165                           title="Vertical news-ticker (image/scale)",
 166                           size=(400, 80))
 167 
 168         #------------
 169 
 170         # Simplified init method.
 171         self.SetProperties()
 172         self.CreateCtrls()
 173         self.DoLayout()
 174 
 175         #------------
 176 
 177         self.CenterOnScreen()
 178 
 179     #-----------------------------------------------------------------------
 180 
 181     def SetProperties(self):
 182         """
 183         ...
 184         """
 185 
 186         self.SetIcon(wx.Icon('icons/wxwin.ico'))
 187         self.SetMinSize((100, 80))
 188         self.SetBackgroundColour("yellow")
 189 
 190 
 191     def CreateCtrls(self):
 192         """
 193         ...
 194         """
 195 
 196         size = self.GetClientSize()
 197 
 198         self.pnl = MyPanel(self, size=size)
 199 
 200 
 201     def DoLayout(self):
 202         """
 203         ...
 204         """
 205 
 206         mainSizer = wx.BoxSizer(wx.VERTICAL)
 207 
 208         #------------
 209 
 210         mainSizer.Add(self.pnl, 1, wx.EXPAND)
 211 
 212         #------------
 213 
 214         self.SetSizer(mainSizer)
 215         mainSizer.Layout()
 216 
 217 #---------------------------------------------------------------------------
 218 
 219 class MyApp(wx.App):
 220     """
 221     ...
 222     """
 223     def OnInit(self):
 224 
 225         #------------
 226 
 227         frame = MyFrame()
 228         self.SetTopWindow(frame)
 229         frame.Show(True)
 230 
 231         return True
 232 
 233 #---------------------------------------------------------------------------
 234 
 235 def main():
 236     app = MyApp(False)
 237     app.MainLoop()
 238 
 239 #---------------------------------------------------------------------------
 240 
 241 if __name__ == "__main__" :
 242     main()


Sample three

img_sample_three.png

   1 # sample_three.py
   2 
   3 import wx
   4 
   5 # class MyPanel
   6 # class MyFrame
   7 # class MyApp
   8 
   9 #---------------------------------------------------------------------------
  10 
  11 class MyPanel(wx.Panel):
  12     """
  13     Create a panel with a canvas to draw on.
  14     """
  15 
  16     def __init__(self, parent, size):
  17         wx.Panel.__init__(self, parent, -1, size=size)
  18 
  19         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  20 
  21         #------------
  22 
  23         w, h = self.GetClientSize()
  24 
  25         #------------
  26 
  27         # Initial x position of image.
  28         self.x = -w
  29 
  30         #------------
  31 
  32         self.timer = wx.Timer(self)
  33         # Start(int milliseconds, oneShot=False).
  34         # Lower timer interval gives faster speed.
  35         self.timer.Start(10)
  36         self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
  37 
  38         #------------
  39 
  40         # Simplified init method.
  41         self.BindEvents()
  42 
  43     #-----------------------------------------------------------------------
  44 
  45     def BindEvents(self):
  46         """
  47         Bind some events to an events handler.
  48         """
  49 
  50         self.Bind(wx.EVT_SIZE, self.OnResize)
  51         self.Bind(wx.EVT_PAINT, self.OnPaint)
  52         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
  53         # self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
  54 
  55 
  56     def OnMouseEvents(self, event):
  57         """
  58         ...
  59         """
  60 
  61         if event.LeftDown() :
  62             self.timer.Stop()
  63         else:
  64             self.timer.Start()
  65         event.Skip()
  66 
  67 
  68     def OnErase(self, evt):
  69         """
  70         Noop because of double buffering.
  71         """
  72 
  73         pass
  74 
  75 
  76     def OnResize(self, event):
  77         """
  78         Refresh widget after resizing.
  79         """
  80 
  81         self.Refresh()
  82         event.Skip()
  83 
  84 
  85     def OnPaint(self, event=None):
  86         """
  87         Create the paint canvas.
  88         """
  89 
  90         dc = wx.BufferedPaintDC(self)
  91         brush = wx.Brush("red")
  92         dc.SetBackground(brush)
  93         dc.Clear()
  94 
  95         #------------
  96 
  97         font = dc.GetFont()
  98         font.SetWeight(wx.BOLD)
  99         font.SetPointSize(12)
 100         # font.SetFaceName("Tahoma")
 101         dc.SetFont(font)
 102 
 103         #------------
 104 
 105         width, height = self.GetClientSize()
 106 
 107         #------------
 108 
 109         txt = "Loop for the text !!!"
 110 
 111         w, h = dc.GetTextExtent(txt)
 112 
 113         #------------
 114 
 115         dc.SetTextForeground("white")
 116         dc.DrawText(txt, int(self.x), int(height/2-(h/2)))
 117 
 118 
 119     def OnTimer(self, event):
 120         """
 121         ...
 122         """
 123 
 124         w, h = self.GetClientSize()
 125 
 126         #------------
 127 
 128         # Increment x to start moving.
 129         self.x += -0.5
 130         # Optional continuous movement
 131         # experiment with wrap around values.
 132         if self.x > w or self.x < -440:
 133             self.x = w
 134 
 135         self.Refresh()
 136 
 137 #---------------------------------------------------------------------------
 138 
 139 class MyFrame(wx.Frame):
 140     """
 141     ...
 142     """
 143 
 144     def __init__(self):
 145         wx.Frame.__init__(self, None,
 146                           title="Horizontal news-ticker (text)",
 147                           size=(400, 80))
 148 
 149         #------------
 150 
 151         # Simplified init method.
 152         self.SetProperties()
 153         self.CreateCtrls()
 154         self.DoLayout()
 155 
 156         #------------
 157 
 158         self.CenterOnScreen()
 159 
 160     #-----------------------------------------------------------------------
 161 
 162     def SetProperties(self):
 163         """
 164         ...
 165         """
 166 
 167         self.SetIcon(wx.Icon('icons/wxwin.ico'))
 168         self.SetMinSize((100, 80))
 169 
 170 
 171     def CreateCtrls(self):
 172         """
 173         ...
 174         """
 175 
 176         size = self.GetClientSize()
 177 
 178         self.pnl = MyPanel(self, size=size)
 179 
 180 
 181     def DoLayout(self):
 182         """
 183         ...
 184         """
 185 
 186         mainSizer = wx.BoxSizer(wx.VERTICAL)
 187 
 188         #------------
 189 
 190         mainSizer.Add(self.pnl, 1, wx.EXPAND)
 191 
 192         #------------
 193 
 194         self.SetSizer(mainSizer)
 195         mainSizer.Layout()
 196 
 197 #---------------------------------------------------------------------------
 198 
 199 class MyApp(wx.App):
 200     """
 201     ...
 202     """
 203     def OnInit(self):
 204 
 205         #------------
 206 
 207         frame = MyFrame()
 208         self.SetTopWindow(frame)
 209         frame.Show(True)
 210 
 211         return True
 212 
 213 #---------------------------------------------------------------------------
 214 
 215 def main():
 216     app = MyApp(False)
 217     app.MainLoop()
 218 
 219 #---------------------------------------------------------------------------
 220 
 221 if __name__ == "__main__" :
 222     main()


Sample four

img_sample_four.png

   1 # sample_four.py
   2 
   3 import wx
   4 
   5 # class MyPanel
   6 # class MyFrame
   7 # class MyApp
   8 
   9 #---------------------------------------------------------------------------
  10 
  11 class MyPanel(wx.Panel):
  12     """
  13     Create a panel with a canvas to draw on.
  14     """
  15 
  16     def __init__(self, parent, size):
  17         wx.Panel.__init__(self, parent, -1, size=size)
  18 
  19         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  20 
  21         #------------
  22 
  23         w, h = self.GetClientSize()
  24 
  25         #------------
  26 
  27         # Initial y position of image
  28         self.y = -h
  29 
  30         #------------
  31 
  32         self.timer = wx.Timer(self)
  33         # Start(int milliseconds, oneShot=False).
  34         # Lower timer interval gives faster speed.
  35         self.timer.Start(10)
  36         self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
  37 
  38         #------------
  39 
  40         # Simplified init method.
  41         self.BindEvents()
  42 
  43     #-----------------------------------------------------------------------
  44 
  45     def BindEvents(self):
  46         """
  47         Bind some events to an events handler.
  48         """
  49 
  50         self.Bind(wx.EVT_SIZE, self.OnResize)
  51         self.Bind(wx.EVT_PAINT, self.OnPaint)
  52         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
  53         # self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
  54 
  55 
  56     def OnMouseEvents(self, event):
  57         """
  58         ...
  59         """
  60 
  61         if event.LeftDown() :
  62             self.timer.Stop()
  63         else:
  64             self.timer.Start()
  65         event.Skip()
  66 
  67 
  68     def OnErase(self, evt):
  69         """
  70         Noop because of double buffering.
  71         """
  72 
  73         pass
  74 
  75 
  76     def OnResize(self, event):
  77         """
  78         Refresh widget after resizing.
  79         """
  80 
  81         self.Refresh()
  82         event.Skip()
  83 
  84 
  85     def OnPaint(self, event=None):
  86         """
  87         Create the paint canvas.
  88         """
  89 
  90         dc = wx.BufferedPaintDC(self)
  91         brush = wx.Brush("gray")
  92         dc.SetBackground(brush)
  93         dc.Clear()
  94 
  95         #------------
  96 
  97         font = dc.GetFont()
  98         font.SetWeight(wx.BOLD)
  99         font.SetPointSize(12)
 100         # font.SetFaceName("Tahoma")
 101         dc.SetFont(font)
 102 
 103         #------------
 104 
 105         width, height = self.GetClientSize()
 106 
 107         #------------
 108 
 109         txt = "Loop for the text !!!"
 110 
 111         w, h = dc.GetTextExtent(txt)
 112 
 113         #------------
 114 
 115         dc.SetTextForeground("white")
 116         dc.DrawText(txt, int(width/2-(w/2)), int(self.y))
 117 
 118 
 119     def OnTimer(self, event):
 120         """
 121 
 122         """
 123 
 124         w, h = self.GetClientSize()
 125 
 126         #------------
 127 
 128         # Increment x to start moving.
 129         self.y += -0.5
 130         # Optional continuous movement
 131         # experiment with wrap around values.
 132         if self.y > h or self.y < -100:
 133             self.y = h
 134 
 135         self.Refresh()
 136 
 137 #---------------------------------------------------------------------------
 138 
 139 class MyFrame(wx.Frame):
 140     """
 141     ...
 142     """
 143 
 144     def __init__(self):
 145         wx.Frame.__init__(self, None,
 146                           title="Vertical news-ticker (text)",
 147                           size=(400, 120))
 148 
 149         #------------
 150 
 151         # Simplified init method.
 152         self.SetProperties()
 153         self.CreateCtrls()
 154         self.DoLayout()
 155 
 156         #------------
 157 
 158         self.CenterOnScreen()
 159 
 160     #-----------------------------------------------------------------------
 161 
 162     def SetProperties(self):
 163         """
 164         ...
 165         """
 166 
 167         self.SetIcon(wx.Icon('icons/wxwin.ico'))
 168         self.SetMinSize((100, 80))
 169 
 170 
 171     def CreateCtrls(self):
 172         """
 173         ...
 174         """
 175 
 176         size = self.GetClientSize()
 177 
 178         self.pnl = MyPanel(self, size=size)
 179 
 180 
 181     def DoLayout(self):
 182         """
 183         ...
 184         """
 185 
 186         mainSizer = wx.BoxSizer(wx.VERTICAL)
 187 
 188         #------------
 189 
 190         mainSizer.Add(self.pnl, 1, wx.EXPAND)
 191 
 192         #------------
 193 
 194         self.SetSizer(mainSizer)
 195         mainSizer.Layout()
 196 
 197 #---------------------------------------------------------------------------
 198 
 199 class MyApp(wx.App):
 200     """
 201     ...
 202     """
 203     def OnInit(self):
 204 
 205         #------------
 206 
 207         frame = MyFrame()
 208         self.SetTopWindow(frame)
 209         frame.Show(True)
 210 
 211         return True
 212 
 213 #---------------------------------------------------------------------------
 214 
 215 def main():
 216     app = MyApp(False)
 217     app.MainLoop()
 218 
 219 #---------------------------------------------------------------------------
 220 
 221 if __name__ == "__main__" :
 222     main()


Sample five

img_sample_five.png

   1 # sample_five.py
   2 
   3 import os
   4 import array
   5 
   6 import wx
   7 import wx.adv
   8 
   9 # class MyPanel
  10 # class MyThankDlg
  11 # class MyFrame
  12 # class MyApp
  13 
  14 #-------------------------------------------------------------------------------
  15 
  16 class MyPanel(wx.Panel):
  17     """
  18     ...
  19     """
  20     def __init__(self, parent, size):
  21         wx.Panel.__init__(self, parent, -1, size=size)
  22 
  23         #------------
  24 
  25         self.parent = parent
  26 
  27         #------------
  28 
  29         w, h = self.GetClientSize()
  30 
  31         # Initial y position of image.
  32         self.y = h
  33 
  34         #------------
  35 
  36         self.timer = wx.Timer(self)
  37         # Start(int milliseconds, oneShot=False).
  38         # Lower timer interval gives faster speed.
  39         self.timer.Start(10)
  40 
  41         #------------
  42 
  43         self.height = 50
  44 
  45         self.MakeBitmapRGBA(w+100, self.height)   # width, height for gradient.
  46         self.MakeBitmapRGBA_(w+100, self.height)   # width, height for gradient.
  47 
  48         #------------
  49 
  50         # Simplified init method.
  51         self.SetProperties()
  52         self.CreateCtrls()
  53         self.BindEvents()
  54 
  55     #---------------------------------------------------------------------------
  56 
  57     def SetProperties(self):
  58         """
  59         Set the properties.
  60         """
  61 
  62         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  63 
  64 
  65     def CreateCtrls(self):
  66         """
  67         ...
  68         """
  69 
  70         # Load the image.
  71         self.photo = wx.Bitmap("bitmaps/wxPython.png", wx.BITMAP_TYPE_PNG)
  72 
  73 
  74     def BindEvents(self):
  75         """
  76         Bind all the events related to my panel.
  77         """
  78 
  79         # Bind the Paint and Size event
  80         self.Bind(wx.EVT_SIZE, self.OnResize)
  81         self.Bind(wx.EVT_PAINT, self.OnPaint)
  82         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
  83         self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
  84         # self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
  85 
  86 
  87     def OnMouseEvents(self, event):
  88         """
  89         ...
  90         """
  91 
  92         if event.LeftDown() :
  93             self.timer.Stop()
  94         else:
  95             self.timer.Start()
  96         event.Skip()
  97 
  98 
  99     def OnErase(self, evt):
 100         """
 101         Noop because of double buffering.
 102         """
 103 
 104         pass
 105 
 106 
 107     def OnResize(self, event):
 108         """
 109         Refresh widget after resizing.
 110         """
 111 
 112         self.Refresh()
 113         event.Skip()
 114 
 115 
 116     def GetRGB(self, x, y, bpp):
 117         """
 118         ...
 119         """
 120 
 121         # Calculate some colour values for
 122         # this sample based on x,y position.
 123         # 255 = white  |  0 = black
 124         r = g = b = 0
 125 
 126         if bpp == 4:
 127             a = int(y * 255.0 / self.height)
 128 
 129             return r, g, b, a
 130         else:
 131             return r, g, b
 132 
 133 
 134     def MakeBitmapRGBA(self, width, height):
 135         """
 136         ...
 137         """
 138 
 139         # Make a bitmap using an array of RGBA bytes.
 140         bpp = 4  # Bytes per pixel.
 141         bytes = array.array('B', [0] * width*height*bpp)
 142 
 143         for y in range(height):
 144             for x in range(width):
 145                 offset = x*bpp - y*width*bpp
 146                 r,g,b,a = self.GetRGB(x, y, bpp)
 147                 bytes[offset + 0] = r
 148                 bytes[offset + 1] = g
 149                 bytes[offset + 2] = b
 150                 bytes[offset + 3] = a
 151 
 152         self.rgbaBmp = wx.Bitmap.FromBufferRGBA(width, height, bytes)
 153 
 154 
 155     def MakeBitmapRGBA_(self, width, height):
 156         """
 157         ...
 158         """
 159 
 160         # Make a bitmap using an array of RGBA bytes.
 161         bpp = 4  # Bytes per pixel.
 162         bytes = array.array('B', [0] * width*height*bpp)
 163 
 164         for y in range(height):
 165             for x in range(width):
 166                 offset = y*width*bpp - x*bpp
 167                 r,g,b,a = self.GetRGB(x, y, bpp)
 168                 bytes[offset + 0] = r
 169                 bytes[offset + 1] = g
 170                 bytes[offset + 2] = b
 171                 bytes[offset + 3] = a
 172 
 173         self.rgbaBmp_ = wx.Bitmap.FromBufferRGBA(width, height, bytes)
 174 
 175 
 176     def DrawBitmapAndMessage(self, dc, bmp, msg, x_, y_):
 177         """
 178         ...
 179         """
 180 
 181         x, y = x_, y_
 182 
 183         dc.DrawText(msg, x, y)
 184 
 185         # Draw the bitmap over the text.
 186         dc.DrawBitmap(bmp, x_, y_, True)
 187 
 188 
 189     def OnPaint(self, event):
 190         """
 191         ...
 192         """
 193 
 194         dc = wx.BufferedPaintDC(self)
 195         brush = wx.Brush("black")
 196         dc.SetBackground(brush)
 197         # or dc.SetBackground(wx.Brush(wx.WHITE))
 198         dc.Clear()
 199 
 200         #------------
 201 
 202         w = self.photo.GetWidth()
 203         h = self.photo.GetHeight()
 204 
 205         width, height = self.GetSize()
 206         l, h = ((width/2)-(w/2), (height/2)-(h/2))
 207 
 208         # Draw the image.
 209         dc.DrawBitmap(self.photo, int(l), int(h), False)
 210 
 211         #------------
 212 
 213         self.paintBox(dc)
 214 
 215 
 216     def OnTimer(self, event):
 217         """
 218         ...
 219         """
 220 
 221         w, h = self.GetClientSize()
 222 
 223         #------------
 224 
 225         # Increment x to start moving.
 226         self.y += -0.5
 227 
 228         # Optional continuous movement
 229         # experiment with wrap around values.
 230         if self.y > h or self.y < -500:
 231             self.y = h
 232         self.Refresh()
 233 
 234 
 235     def paintBox(self, dc):
 236         """
 237         ...
 238         """
 239 
 240         font = dc.GetFont()
 241         font.SetWeight(wx.BOLD)
 242         font.SetPointSize(10)
 243         # font.SetFaceName("Tahoma")
 244         dc.SetFont(font)
 245 
 246         #------------
 247 
 248         width, height = self.GetClientSize()
 249 
 250         #------------
 251 
 252         txt1  = ("Special thanks to :")
 253         txt2  = ("Vegaseat (DaniWeb)")
 254         txt3  = ("Mike Driscoll")
 255         txt4  = ("Robin Dunn")
 256         txt5  = ("Giuseppe Costanzi")
 257         txt6  = ("Pascal Faut.")
 258         txt7  = ("Cody Precord")
 259         txt8  = ("Andrea Gavana")
 260         txt9  = ("Werner F. Bruhin")
 261         txt10 = ("Robin Rappin")
 262         txt11 = ("Chris Barker")
 263         txt12 = ("Alain Delgrange")
 264         txt13 = ("ZetCode ")
 265         txt14 = ("Steven Sproat")
 266         txt15 = ("Beppe & Guldo")
 267         txt16 = ("Ray Pasco")
 268         txt17 = ("David Hughes")
 269         txt18 = ("Ianare Sévi")
 270         txt19 = ("Fenikso")
 271         txt20 = ("Yvan Lucas")
 272         txt21 = ("Christoph Gohlke...")
 273         txt22 = ("L'équipe des wxWidgets")
 274         txt23 = ("La communauté python et wxPython")
 275         txt24 = ("L'ensemble des forums dédiés")
 276 
 277         #------------
 278 
 279         wt1, ht1   = dc.GetTextExtent(txt1)
 280         wt2, ht2   = dc.GetTextExtent(txt2)
 281         wt3, ht3   = dc.GetTextExtent(txt3)
 282         wt4, ht4   = dc.GetTextExtent(txt4)
 283         wt5, ht5   = dc.GetTextExtent(txt5)
 284         wt6, ht6   = dc.GetTextExtent(txt6)
 285         wt7, ht7   = dc.GetTextExtent(txt7)
 286         wt8, ht8   = dc.GetTextExtent(txt8)
 287         wt9, ht9   = dc.GetTextExtent(txt9)
 288         wt10, ht10 = dc.GetTextExtent(txt10)
 289         wt11, ht11 = dc.GetTextExtent(txt11)
 290         wt12, ht12 = dc.GetTextExtent(txt12)
 291         wt13, ht13 = dc.GetTextExtent(txt13)
 292         wt14, ht14 = dc.GetTextExtent(txt14)
 293         wt15, ht15 = dc.GetTextExtent(txt15)
 294         wt16, ht16 = dc.GetTextExtent(txt16)
 295         wt17, ht17 = dc.GetTextExtent(txt17)
 296         wt18, ht18 = dc.GetTextExtent(txt18)
 297         wt19, ht19 = dc.GetTextExtent(txt19)
 298         wt20, ht20 = dc.GetTextExtent(txt20)
 299         wt21, ht21 = dc.GetTextExtent(txt21)
 300         wt22, ht22 = dc.GetTextExtent(txt22)
 301         wt23, ht23 = dc.GetTextExtent(txt23)
 302         wt24, ht24 = dc.GetTextExtent(txt24)
 303 
 304         #------------
 305 
 306         # Set the text and draw it (txt1).
 307         dc.SetTextForeground("#ffffff")
 308         dc.DrawText(txt1, int(width/2-(wt1/2)), int(self.y))
 309 
 310         dc.SetPen(wx.Pen('red', 2))
 311         # Draw a blue line (thickness = 2)
 312         dc.DrawLine(90, int(self.y+30), width-90, int(self.y+30))
 313 
 314         dc.SetTextForeground("#ff0084")
 315         dc.DrawText(txt2, int(width/2-(wt2/2)), int(self.y+40))
 316 
 317         dc.SetTextForeground("#ff00bb")
 318         dc.DrawText(txt3, int(width/2-(wt3/2)), int(self.y+60))
 319 
 320         dc.SetTextForeground("#ff00fb")
 321         dc.DrawText(txt4, int(width/2-(wt4/2)), int(self.y+80))
 322 
 323         dc.SetTextForeground("#c800ff")
 324         dc.DrawText(txt5, int(width/2-(wt5/2)), int(self.y+100))
 325 
 326         dc.SetTextForeground("#9000ff")
 327         dc.DrawText(txt6, int(width/2-(wt6/2)), int(self.y+120))
 328 
 329         dc.SetTextForeground("#5100ff")
 330         dc.DrawText(txt7, int(width/2-(wt7/2)), int(self.y+140))
 331 
 332         dc.SetTextForeground("#2200ff")
 333         dc.DrawText(txt8, int(width/2-(wt8/2)), int(self.y+160))
 334 
 335         dc.SetTextForeground("#0059ff")
 336         dc.DrawText(txt9, int(width/2-(wt9/2)), int(self.y+180))
 337 
 338         dc.SetTextForeground("#0095ff")
 339         dc.DrawText(txt10, int(width/2-(wt10/2)), int(self.y+200))
 340 
 341         dc.SetTextForeground("#00ddff")
 342         dc.DrawText(txt11, int(width/2-(wt11/2)), int(self.y+220))
 343 
 344         dc.SetTextForeground("#00ffc8")
 345         dc.DrawText(txt12, int(width/2-(wt12/2)), int(self.y+240))
 346 
 347         dc.SetTextForeground("#00ff11")
 348         dc.DrawText(txt13, int(width/2-(wt13/2)), int(self.y+260))
 349 
 350         dc.SetTextForeground("#8cff00")
 351         dc.DrawText(txt14, int(width/2-(wt14/2)), int(self.y+280))
 352 
 353         dc.SetTextForeground("#d9ff00")
 354         dc.DrawText(txt15, int(width/2-(wt15/2)), int(self.y+300))
 355 
 356         dc.SetTextForeground("#ffe500")
 357         dc.DrawText(txt16, int(width/2-(wt16/2)), int(self.y+320))
 358 
 359         dc.SetTextForeground("#ffc800")
 360         dc.DrawText(txt17, int(width/2-(wt17/2)), int(self.y+340))
 361 
 362         dc.SetTextForeground("#ff7f00")
 363         dc.DrawText(txt18, int(width/2-(wt18/2)), int(self.y+360))
 364 
 365         dc.SetTextForeground("#ff5100")
 366         dc.DrawText(txt19, int(width/2-(wt19/2)), int(self.y+380))
 367 
 368         dc.SetTextForeground("#ff0000")
 369         dc.DrawText(txt20, int(width/2-(wt20/2)), int(self.y+400))
 370 
 371         dc.SetTextForeground("#b92f2f")
 372         dc.DrawText(txt21, int(width/2-(wt21/2)), int(self.y+420))
 373 
 374         dc.SetTextForeground("#505050")
 375         dc.DrawText(txt22, int(width/2-(wt22/2)), int(self.y+440))
 376 
 377         dc.SetTextForeground("#787878")
 378         dc.DrawText(txt23, int(width/2-(wt23/2)), int(self.y+460))
 379 
 380         dc.SetTextForeground("#ffffff")
 381         dc.DrawText(txt24, int(width/2-(wt24/2)), int(self.y+480))
 382 
 383         # dc, img alpha, txt, x, y
 384         # Start and end for gradient.
 385         self.DrawBitmapAndMessage(dc, self.rgbaBmp, "", 0-100, -1)
 386         self.DrawBitmapAndMessage(dc, self.rgbaBmp_, "", 0-100, height-50+1)
 387 
 388 #-------------------------------------------------------------------------------
 389 
 390 class MyThankDlg(wx.Dialog):
 391     """
 392     ...
 393     """
 394     def __init__(self, parent, id, title):
 395         """
 396         ...
 397         """
 398         wx.Dialog.__init__(self, parent,
 399                            wx.ID_ANY,
 400                            title)
 401 
 402         #------------
 403 
 404         self.parent = parent
 405 
 406         #------------
 407 
 408         # Simplified init method
 409         self.SetProperties()
 410         self.CreateCtrls()
 411         self.BindEvents()
 412         self.DoLayout()
 413 
 414         #------------
 415 
 416         self.Centre(wx.BOTH)
 417 
 418     #---------------------------------------------------------------------------
 419 
 420     def SetProperties(self):
 421         """
 422         Set the dialog properties (title, icon...).
 423         """
 424 
 425         # Set frame icon.
 426         self.SetIcon(wx.Icon('icons/wxwin.ico'))
 427         self.SetBackgroundColour(wx.WHITE)
 428         self.SetTransparent(240)
 429         self.SetSize((360, 380))
 430         # self.SetTitle("Thank to...")
 431 
 432 
 433     def CreateCtrls(self):
 434         """
 435         Make some widgets.
 436         """
 437 
 438         self.panel1 = wx.Panel(self,
 439                                id=-1,
 440                                style=wx.BORDER_NONE |
 441                                wx.TAB_TRAVERSAL)
 442         self.panel1.SetBackgroundColour(wx.BLACK)
 443 
 444         self.panel2 = wx.Panel(self,
 445                                id=-1,
 446                                style=wx.TAB_TRAVERSAL)
 447         self.panel2.SetBackgroundColour(wx.WHITE)
 448 
 449         w, h = self.GetClientSize()
 450 
 451         self.ticker = MyPanel(self.panel1, size=(w, h))
 452 
 453         #------------
 454 
 455         self.btnClose = wx.Button(self.panel2,
 456                                   id=wx.ID_CLOSE,
 457                                   label="&Close")
 458         self.btnClose.SetFocus()
 459 
 460 
 461     def BindEvents(self):
 462         """
 463         Bind all the events related to my panel.
 464         """
 465 
 466         self.Bind(wx.EVT_BUTTON, self.OnClose, id=wx.ID_CLOSE)
 467         self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyUp)
 468         self.Bind(wx.EVT_CLOSE, self.OnClose)
 469 
 470 
 471     def DoLayout(self):
 472         """
 473         Do layout.
 474         """
 475 
 476         hBox1 = wx.BoxSizer(wx.HORIZONTAL)
 477         hBox1.Add(self.ticker, 1, wx.ALL | wx.EXPAND, 5)
 478         self.panel1.SetSizer(hBox1)
 479 
 480         #------------
 481 
 482         hBox2 = wx.BoxSizer(wx.HORIZONTAL)
 483         hBox2.Add(self.btnClose, 1, wx.BOTTOM | wx.EXPAND, 5)
 484         self.panel2.SetSizer(hBox2)
 485 
 486         vBox = wx.BoxSizer(wx.VERTICAL)
 487         vBox.Add(self.panel1, 1, wx.EXPAND | wx.ALL | wx.CENTER, 5)
 488         vBox.Add(self.panel2, 0, wx.ALL | wx.CENTER,  5)
 489 
 490         #------------
 491 
 492         self.SetAutoLayout(True)
 493         self.SetSizer(vBox)
 494 
 495 
 496     def OnClose(self, event):
 497         """
 498         ...
 499         """
 500 
 501         self.EndModal(event.GetId())
 502 
 503 
 504     def OnKeyUp(self, event):
 505         """
 506         Handles the wx.EVT_CHAR_HOOK event for the dialog.
 507         """
 508 
 509         if event.GetKeyCode() == wx.WXK_ESCAPE:
 510             # Close the dialog, no action.
 511             self.OnClose(event)
 512 
 513         event.Skip()
 514 
 515 #-------------------------------------------------------------------------------
 516 
 517 class MyFrame(wx.Frame):
 518     """
 519     ...
 520     """
 521     def __init__(self):
 522         super(MyFrame, self).__init__(None,
 523                                       -1,
 524                                       title="")
 525 
 526         #------------
 527 
 528         # Simplified init method.
 529         self.SetProperties()
 530         self.CreateCtrls()
 531         self.BindEvents()
 532         self.DoLayout()
 533 
 534         #------------
 535 
 536         self.CenterOnScreen()
 537 
 538     #-----------------------------------------------------------------------
 539 
 540     def SetProperties(self):
 541         """
 542         ...
 543         """
 544 
 545         self.SetTitle("Sample five")
 546         self.SetIcon(wx.Icon('icons/wxwin.ico'))
 547 
 548 
 549     def CreateCtrls(self):
 550         """
 551         ...
 552         """
 553 
 554         self.panel = wx.Panel(self, -1)
 555 
 556         #------------
 557 
 558         self.btnDlg = wx.Button(self.panel,
 559                                 -1,
 560                                 "&Show customized vertical news-ticker dialog")
 561 
 562         self.btnClose = wx.Button(self.panel,
 563                                   -1,
 564                                   "&Close")
 565 
 566 
 567     def BindEvents(self):
 568         """
 569         Bind some events to an events handler.
 570         """
 571 
 572         # Bind the close event to an event handler.
 573         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 574 
 575         # Bind the buttons event to an event handler.
 576         self.Bind(wx.EVT_BUTTON, self.OnThanksDlg, self.btnDlg)
 577         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
 578 
 579 
 580     def DoLayout(self):
 581         """
 582         ...
 583         """
 584 
 585         # mainSizer is the top-level one that manages everything.
 586         mainSizer = wx.BoxSizer(wx.VERTICAL)
 587 
 588         # wx.BoxSizer(window, proportion, flag, border)
 589         # wx.BoxSizer(sizer, proportion, flag, border)
 590         mainSizer.Add(self.btnDlg, 1, wx.EXPAND | wx.ALL, 10)
 591         mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
 592 
 593         # Finally, tell the panel to use the sizer for layout.
 594         self.panel.SetAutoLayout(True)
 595         self.panel.SetSizer(mainSizer)
 596 
 597         mainSizer.Fit(self.panel)
 598 
 599 
 600     def OnCloseMe(self, event):
 601         """
 602         ...
 603         """
 604 
 605         self.Close(True)
 606 
 607 
 608     def OnThanksDlg(self, event):
 609         """
 610         ...
 611         """
 612 
 613         thanksTo = MyThankDlg(self,
 614                               id=-1,
 615                               title="Thanks to...")
 616         result = thanksTo.ShowWindowModal()
 617 
 618         if result == wx.ID_CLOSE:
 619              result.Close(True)
 620              result.Destroy()
 621 
 622 
 623     def OnCloseWindow(self, event):
 624         """
 625         ...
 626         """
 627 
 628         self.Destroy()
 629 
 630 #-------------------------------------------------------------------------------
 631 
 632 class MyApp(wx.App):
 633     """
 634     ...
 635     """
 636     def OnInit(self):
 637 
 638         #------------
 639 
 640         frame = MyFrame()
 641         self.SetTopWindow(frame)
 642         frame.Show(True)
 643 
 644         return True
 645 
 646 #---------------------------------------------------------------------------
 647 
 648 def main():
 649     app = MyApp(False)
 650     app.MainLoop()
 651 
 652 #---------------------------------------------------------------------------
 653 
 654 if __name__ == "__main__" :
 655     main()


Sample six

img_sample_six.png

   1 # sample_six.py
   2 
   3 import os
   4 import array
   5 
   6 import wx
   7 import wx.adv
   8 
   9 # class MyPanel
  10 # class MyThankDlg
  11 # class MyFrame
  12 # class MyApp
  13 
  14 #-------------------------------------------------------------------------------
  15 
  16 class MyPanel(wx.Panel):
  17     """
  18     ...
  19     """
  20     def __init__(self, parent, size):
  21         wx.Panel.__init__(self, parent, -1, size=size)
  22 
  23         #------------
  24 
  25         self.parent = parent
  26 
  27         #------------
  28 
  29         w, h = self.GetClientSize()
  30 
  31         # Initial y position of image.
  32         self.x = w
  33 
  34         #------------
  35 
  36         self.timer = wx.Timer(self)
  37         # Start(int milliseconds, oneShot=False).
  38         # Lower timer interval gives faster speed.
  39         self.timer.Start(10)
  40 
  41         #------------
  42 
  43         self.width = 65
  44 
  45         self.MakeBitmapRGBA(self.width, h)   # width, height for gradient.
  46         self.MakeBitmapRGBA_(self.width, h)   # width, height for gradient.
  47 
  48         #------------
  49 
  50         # Simplified init method.
  51         self.SetProperties()
  52         self.CreateCtrls()
  53         self.BindEvents()
  54 
  55     #---------------------------------------------------------------------------
  56 
  57     def SetProperties(self):
  58         """
  59         Set the properties.
  60         """
  61 
  62         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  63 
  64 
  65     def CreateCtrls(self):
  66         """
  67         ...
  68         """
  69 
  70         # Load the image.
  71         self.photo = wx.Bitmap("bitmaps/wxWidgets.png", wx.BITMAP_TYPE_PNG)
  72 
  73 
  74     def BindEvents(self):
  75         """
  76         Bind all the events related to my panel.
  77         """
  78 
  79         # Bind the Paint and Size event
  80         self.Bind(wx.EVT_SIZE, self.OnResize)
  81         self.Bind(wx.EVT_PAINT, self.OnPaint)
  82         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
  83         self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
  84         # self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
  85 
  86 
  87     def OnMouseEvents(self, event):
  88         """
  89         ...
  90         """
  91 
  92         if event.LeftDown() :
  93             self.timer.Stop()
  94         else:
  95             self.timer.Start()
  96         event.Skip()
  97 
  98 
  99     def OnErase(self, evt):
 100         """
 101         Noop because of double buffering.
 102         """
 103 
 104         pass
 105 
 106 
 107     def OnResize(self, event):
 108         """
 109         Refresh widget after resizing.
 110         """
 111 
 112         self.Refresh()
 113         event.Skip()
 114 
 115 
 116     def GetRGB(self, x, y, bpp):
 117         """
 118         ...
 119         """
 120 
 121         # Calculate some colour values for
 122         # this sample based on x,y position.
 123         # 255 = white  |  0 = black
 124         r = g = b = 255
 125 
 126         if bpp == 4:
 127             a = int(x * 255.0 / self.width)
 128             return r, g, b, a
 129         else:
 130             return r, g, b
 131 
 132 
 133     def MakeBitmapRGBA(self, width, height):
 134         """
 135         ...
 136         """
 137 
 138         # Make a bitmap using an array of RGBA bytes.
 139         bpp = 4  # Bytes per pixel.
 140         bytes = array.array('B', [0] * width*height*bpp)
 141 
 142         for y in range(height):
 143             for x in range(width):
 144                 offset = y*width*bpp + x*bpp
 145                 r,g,b,a = self.GetRGB(x, y, bpp)
 146                 bytes[offset + 0] = r
 147                 bytes[offset + 1] = g
 148                 bytes[offset + 2] = b
 149                 bytes[offset + 3] = a
 150 
 151         self.rgbaBmp = wx.Bitmap.FromBufferRGBA(width, height, bytes)
 152 
 153 
 154     def MakeBitmapRGBA_(self, width, height):
 155         """
 156         ...
 157         """
 158 
 159         # Make a bitmap using an array of RGBA bytes.
 160         bpp = 4  # Bytes per pixel.
 161         bytes = array.array('B', [0] * width*height*bpp)
 162 
 163         for y in range(height):
 164             for x in range(width):
 165                 offset = y*width*bpp - x*bpp
 166                 r,g,b,a = self.GetRGB(x, y, bpp)
 167                 bytes[offset + 0] = r
 168                 bytes[offset + 1] = g
 169                 bytes[offset + 2] = b
 170                 bytes[offset + 3] = a
 171 
 172         self.rgbaBmp_ = wx.Bitmap.FromBufferRGBA(width, height, bytes)
 173 
 174 
 175     def DrawBitmapAndMessage(self, dc, bmp, msg, x_, y_):
 176         """
 177         ...
 178         """
 179 
 180         x, y = x_, y_
 181 
 182         dc.DrawText(msg, x, y)
 183 
 184         # Draw the bitmap over the text.
 185         dc.DrawBitmap(bmp, x_, y_, True)
 186 
 187 
 188     def OnPaint(self, event):
 189         """
 190         ...
 191         """
 192 
 193         dc = wx.BufferedPaintDC(self)
 194         brush = wx.Brush("white")
 195         dc.SetBackground(brush)
 196         # or dc.SetBackground(wx.Brush(wx.WHITE))
 197         dc.Clear()
 198 
 199         #------------
 200 
 201         w = self.photo.GetWidth()
 202         h = self.photo.GetHeight()
 203 
 204         width, height = self.GetSize()
 205         l, h = ((width/2)-(w/2), (height/2)-(h/2))
 206 
 207         # Draw the image.
 208         dc.DrawBitmap(self.photo, int(l), int(h), False)
 209 
 210         #------------
 211 
 212         self.paintBox(dc)
 213 
 214 
 215     def OnTimer(self, event):
 216         """
 217         ...
 218         """
 219 
 220         w, h = self.GetClientSize()
 221 
 222         #------------
 223 
 224         # Increment x to start moving.
 225         self.x += -0.5
 226 
 227         # Optional continuous movement
 228         # experiment with wrap around values.
 229         if self.x > w or self.x < -430:
 230             self.x = w
 231         self.Refresh()
 232 
 233 
 234     def paintBox(self, dc):
 235         """
 236         ...
 237         """
 238 
 239         font = dc.GetFont()
 240         font.SetWeight(wx.BOLD)
 241         font.SetPointSize(10)
 242         # font.SetFaceName("Tahoma")
 243         dc.SetFont(font)
 244 
 245         #------------
 246 
 247         width, height = self.GetClientSize()
 248 
 249         #------------
 250 
 251         txt  = ("Special thanks to : Vegaseat (DaniWeb), Mike Driscoll, Robin Dunn...")
 252 
 253         #------------
 254 
 255         wt, ht   = dc.GetTextExtent(txt)
 256 
 257         #------------
 258 
 259         # Set the text and draw it (txt1).
 260         dc.SetTextForeground("black")
 261         dc.DrawText(txt, int(self.x), int(height/2-ht/2))
 262 
 263         # dc, img alpha, txt, x, y
 264         # Start and end for gradient.
 265         self.DrawBitmapAndMessage(dc, self.rgbaBmp, "", width-65, 0)
 266         self.DrawBitmapAndMessage(dc, self.rgbaBmp_, "", -2, 0)
 267 
 268 #-------------------------------------------------------------------------------
 269 
 270 class MyThankDlg(wx.Dialog):
 271     """
 272     ...
 273     """
 274     def __init__(self, parent, id, title):
 275         """
 276         ...
 277         """
 278         wx.Dialog.__init__(self, parent,
 279                            wx.ID_ANY,
 280                            title)
 281 
 282         #------------
 283 
 284         self.parent = parent
 285 
 286         #------------
 287 
 288         # Simplified init method
 289         self.SetProperties()
 290         self.CreateCtrls()
 291         self.BindEvents()
 292         self.DoLayout()
 293 
 294         #------------
 295 
 296         self.Centre(wx.BOTH)
 297 
 298     #---------------------------------------------------------------------------
 299 
 300     def SetProperties(self):
 301         """
 302         Set the dialog properties (title, icon...).
 303         """
 304 
 305         # Set frame icon.
 306         self.SetIcon(wx.Icon('icons/wxwin.ico'))
 307         self.SetBackgroundColour("black")
 308         self.SetTransparent(240)
 309         self.SetSize((440, 300))
 310         # self.SetTitle("Thank to...")
 311 
 312 
 313     def CreateCtrls(self):
 314         """
 315         Make some widgets.
 316         """
 317 
 318         self.panel1 = wx.Panel(self,
 319                                id=-1,
 320                                style=wx.BORDER_NONE |
 321                                wx.TAB_TRAVERSAL)
 322         self.panel1.SetBackgroundColour(wx.WHITE)
 323 
 324         self.panel2 = wx.Panel(self,
 325                                id=-1,
 326                                style=wx.TAB_TRAVERSAL)
 327         self.panel2.SetBackgroundColour("black")
 328 
 329         w, h = self.GetClientSize()
 330 
 331         self.ticker = MyPanel(self.panel1, size=(w, h))
 332 
 333         #------------
 334 
 335         self.btnClose = wx.Button(self.panel2,
 336                                   id=wx.ID_CLOSE,
 337                                   label="&Close")
 338         self.btnClose.SetFocus()
 339 
 340 
 341     def BindEvents(self):
 342         """
 343         Bind all the events related to my panel.
 344         """
 345 
 346         self.Bind(wx.EVT_BUTTON, self.OnClose, id=wx.ID_CLOSE)
 347         self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyUp)
 348         self.Bind(wx.EVT_CLOSE, self.OnClose)
 349 
 350 
 351     def DoLayout(self):
 352         """
 353         Do layout.
 354         """
 355 
 356         hBox1 = wx.BoxSizer(wx.HORIZONTAL)
 357         hBox1.Add(self.ticker, 1, wx.ALL | wx.EXPAND, 5)
 358         self.panel1.SetSizer(hBox1)
 359 
 360         #------------
 361 
 362         hBox2 = wx.BoxSizer(wx.HORIZONTAL)
 363         hBox2.Add(self.btnClose, 1, wx.BOTTOM | wx.EXPAND, 5)
 364         self.panel2.SetSizer(hBox2)
 365 
 366         vBox = wx.BoxSizer(wx.VERTICAL)
 367         vBox.Add(self.panel1, 1, wx.EXPAND | wx.ALL | wx.CENTER, 5)
 368         vBox.Add(self.panel2, 0, wx.ALL | wx.CENTER,  5)
 369 
 370         #------------
 371 
 372         self.SetAutoLayout(True)
 373         self.SetSizer(vBox)
 374 
 375 
 376     def OnClose(self, event):
 377         """
 378         ...
 379         """
 380 
 381         self.EndModal(event.GetId())
 382 
 383 
 384     def OnKeyUp(self, event):
 385         """
 386         Handles the wx.EVT_CHAR_HOOK event for the dialog.
 387         """
 388 
 389         if event.GetKeyCode() == wx.WXK_ESCAPE:
 390             # Close the dialog, no action.
 391             self.OnClose(event)
 392 
 393         event.Skip()
 394 
 395 #-------------------------------------------------------------------------------
 396 
 397 class MyFrame(wx.Frame):
 398     """
 399     ...
 400     """
 401     def __init__(self):
 402         super(MyFrame, self).__init__(None,
 403                                       -1,
 404                                       title="")
 405 
 406         #------------
 407 
 408         # Simplified init method.
 409         self.SetProperties()
 410         self.CreateCtrls()
 411         self.BindEvents()
 412         self.DoLayout()
 413 
 414         #------------
 415 
 416         self.CenterOnScreen()
 417 
 418     #-----------------------------------------------------------------------
 419 
 420     def SetProperties(self):
 421         """
 422         ...
 423         """
 424 
 425         self.SetTitle("Sample six")
 426         self.SetIcon(wx.Icon('icons/wxwin.ico'))
 427 
 428 
 429     def CreateCtrls(self):
 430         """
 431         ...
 432         """
 433 
 434         self.panel = wx.Panel(self, -1)
 435 
 436         #------------
 437 
 438         self.btnDlg = wx.Button(self.panel,
 439                                 -1,
 440                                 "&Show customized horizontal news-ticker dialog")
 441 
 442         self.btnClose = wx.Button(self.panel,
 443                                   -1,
 444                                   "&Close")
 445 
 446 
 447     def BindEvents(self):
 448         """
 449         Bind some events to an events handler.
 450         """
 451 
 452         # Bind the close event to an event handler.
 453         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 454 
 455         # Bind the buttons event to an event handler.
 456         self.Bind(wx.EVT_BUTTON, self.OnThanksDlg, self.btnDlg)
 457         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
 458 
 459 
 460     def DoLayout(self):
 461         """
 462         ...
 463         """
 464 
 465         # mainSizer is the top-level one that manages everything.
 466         mainSizer = wx.BoxSizer(wx.VERTICAL)
 467 
 468         # wx.BoxSizer(window, proportion, flag, border)
 469         # wx.BoxSizer(sizer, proportion, flag, border)
 470         mainSizer.Add(self.btnDlg, 1, wx.EXPAND | wx.ALL, 10)
 471         mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
 472 
 473         # Finally, tell the panel to use the sizer for layout.
 474         self.panel.SetAutoLayout(True)
 475         self.panel.SetSizer(mainSizer)
 476 
 477         mainSizer.Fit(self.panel)
 478 
 479 
 480     def OnCloseMe(self, event):
 481         """
 482         ...
 483         """
 484 
 485         self.Close(True)
 486 
 487 
 488     def OnThanksDlg(self, event):
 489         """
 490         ...
 491         """
 492 
 493         thanksTo = MyThankDlg(self,
 494                               id=-1,
 495                               title="Thanks to...")
 496         result = thanksTo.ShowWindowModal()
 497 
 498         if result == wx.ID_CLOSE:
 499              result.Close(True)
 500              result.Destroy()
 501 
 502 
 503     def OnCloseWindow(self, event):
 504         """
 505         ...
 506         """
 507 
 508         self.Destroy()
 509 
 510 #-------------------------------------------------------------------------------
 511 
 512 class MyApp(wx.App):
 513     """
 514     ...
 515     """
 516     def OnInit(self):
 517 
 518         #------------
 519 
 520         frame = MyFrame()
 521         self.SetTopWindow(frame)
 522         frame.Show(True)
 523 
 524         return True
 525 
 526 #---------------------------------------------------------------------------
 527 
 528 def main():
 529     app = MyApp(False)
 530     app.MainLoop()
 531 
 532 #---------------------------------------------------------------------------
 533 
 534 if __name__ == "__main__" :
 535     main()


Download source

source.zip


Additional Information

Link :

# Show a moving image using a wx.ClientDC() canvas on a panel (Vegaseat) :

https://www.daniweb.com/programming/software-development/threads/128350/starting-wxpython-gui-code/6

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Vegaseat (DaniWeb), Mike Driscoll, Robin Dunn, the wxPython community...


About this page

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

18/05/20 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a customized news-ticker (Phoenix) (last edited 2020-12-13 14:14:04 by Ecco)

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