Reputation: 55
I was wondering if anyone knows a way to program flashing text into wxPython? (I am fairly new to wxPython) It would flash between red and normal every half second or so, I am using Python 2.7.3, not the most recent release.
Thanks
Chris
Upvotes: 1
Views: 1945
Reputation: 33111
You'll want to look at how to change fonts on the fly. Typically, it's just calling the widget's SetFont() method. Since you want to do this on a regular basis, then you'll almost certainly want to use a wx.Timer. You can read my tutorial on that subject, if you want. I would probably use the StaticText widget.
Update: Here's a silly example:
import random
import time
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
self.flashingText = wx.StaticText(self, label="I flash a LOT!")
self.flashingText.SetFont(self.font)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.update, self.timer)
self.timer.Start(1000)
#----------------------------------------------------------------------
def update(self, event):
""""""
now = int(time.time())
mod = now % 2
print now
print mod
if mod:
self.flashingText.SetLabel("Current time: %i" % now)
else:
self.flashingText.SetLabel("Oops! It's mod zero time!")
colors = ["blue", "green", "red", "yellow"]
self.flashingText.SetForegroundColour(random.choice(colors))
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Flashing text!")
panel = MyPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
Upvotes: 1