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:
Code tested on the following:
OS: Windows XP
wxPython: 126.96.36.199 and 188.8.131.52
Copyright © 2022 Mouse Vs Python | Powered by Pythonlibrary