How to create a customized news-ticker (Phoenix)
Keywords : Ticker, News-ticker, Scrolling text control, Fade text, Gradient, Horizontal text, Vertical text.
Contents
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
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
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
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
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()
Download source
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
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....