Doing a Fade-in with wxPython

Today we’re going to go over how to make your application do a “fade-in”. One common place that Windows users see this is with Microsoft Outlook’s email notification. It fades in and then back out. wxPython provides a way to set the alpha transparency of any top window, which affects the widgets that are placed on the top-level widget.

In this example, I will use a frame object as the top level object and a timer to change the alpha transparency by a unit of 5 every second. The timer’s event handler will cause the frame to fade into view and then back out again. The range of values is 0 – 255 with 0 being completely transparent and 255 being completely opaque.

The code is below:

import wx

class Fader(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Test')
        self.amount = 5
        self.delta = 5
        panel = wx.Panel(self, wx.ID_ANY)

        self.SetTransparent(self.amount)

        ## ------- Fader Timer -------- ##
        self.timer = wx.Timer(self, wx.ID_ANY)
        self.timer.Start(60)
        self.Bind(wx.EVT_TIMER, self.AlphaCycle)
        ## ---------------------------- ##

    def AlphaCycle(self, evt):
        self.amount += self.delta
        if self.amount >= 255:
            self.delta = -self.delta
            self.amount = 255
        if self.amount <= 0:
            self.amount = 0
        self.SetTransparent(self.amount)

if __name__ == '__main__':
    app = wx.App(False)
    frm = Fader()
    frm.Show()
    app.MainLoop()

As you can see, all you need to do to change the transparency of the top-level widget is to call the SetTransparent() method of that widget and pass it the amount to set. I actually use this method in an application of my own that fades in a dialog to alert me to new mail in my Zimbra email account.

For more information, check out the following resources:

Timers
Transparent Frames

Code tested on the following:

OS: Windows XP
Python: 2.5.2
wxPython: 2.8.8.1 and 2.8.9.1

4 thoughts on “Doing a Fade-in with wxPython”

  1. if self.amount >= 255:
    if self.amount <= 0:

    there are some mistakes in the above codes.

    I can’t see fade in frame.

    Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:56)
    [GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import wx
    w>>> wx.__version__
    ‘2.8.9.1’

  2. if self.amount >= 255:
    if self.amount <= 0:

    there are some mistakes in the above codes.

    I can’t see fade in frame.

    Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:56)
    [GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import wx
    w>>> wx.__version__
    ‘2.8.9.1’

  3. Cocobear,

    You are correct. I had some issues around that time when my host had hardware issues and they moved my sites. A lot of pages were messed up and it looks like I missed that. I updated so it is correct now.

    Thanks and I apologize for not getting back to you sooner. My email has been sticking all my comments in my junk folder for some reason…

    Mike

  4. Cocobear,

    You are correct. I had some issues around that time when my host had hardware issues and they moved my sites. A lot of pages were messed up and it looks like I missed that. I updated so it is correct now.

    Thanks and I apologize for not getting back to you sooner. My email has been sticking all my comments in my junk folder for some reason…

    Mike

Leave a Comment

Your email address will not be published. Required fields are marked *