How to create a transparent frame (Phoenix)

Keywords : Transparent, Transparency, Frame.


Introduction

Top level windows in wx (frames, dialogs, etc.) have a new !

SetTransparent method that will make the window transparent if the platform supports it.

Out of the box Windows 2000 and later will support it, as well as OS X.

On *nix you need to be running an X Server that supports the composite extension, have that extension loaded, and also be running a compositing manager.

(Basically, if you can make other windows be truly transparent, and not just redrawing the desktop background within the window, then it should be supported for wx Apps too.)


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 MyFrame
   6 # class MyApp
   7 
   8 #---------------------------------------------------------------------------
   9 
  10 class MyFrame(wx.Frame):
  11     def __init__(self):
  12         wx.Frame.__init__(self, None, title="Am I transparent ?")
  13 
  14         self.amount = 255
  15         self.delta = -3
  16 
  17         p = wx.Panel(self)
  18         self.st = wx.StaticText(p, -1, str(self.amount), (25, 25))
  19         self.st.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.NORMAL))
  20 
  21         self.timer = wx.Timer(self)
  22         self.timer.Start(25)   # 25 changes per second.
  23         self.Bind(wx.EVT_TIMER, self.AlphaCycle)
  24 
  25         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  26 
  27     #-----------------------------------------------------------------------
  28 
  29     def OnCloseWindow(self, evt):
  30         self.timer.Stop()
  31         del self.timer  # Avoid a memory leak.
  32         self.Destroy()
  33 
  34 
  35     def AlphaCycle(self, evt):
  36         """
  37         The term "alpha" means variable transparency
  38         as opposed to a "mask" which is binary transparency.
  39         alpha == 255 :  fully opaque
  40         alpha ==   0 :  fully transparent (mouse is ineffective!)
  41 
  42         Only top-level controls can be transparent; no other controls can.
  43         This is because they are implemented by the OS, not wx.
  44         """
  45 
  46         self.amount += self.delta
  47         if self.amount == 0 or self.amount == 255:
  48             # Reverse the increment direction.
  49             self.delta = -self.delta
  50         self.st.SetLabel(str(self.amount))
  51 
  52         # Note that we no longer need to use ctypes or win32api to
  53         # make transparent windows, however I'm not removing the
  54         # MakeTransparent code from this sample as it may be helpful
  55         # for somebody someday.
  56         #self.MakeTransparent(self.amount)
  57 
  58         # Instead we'll just call the SetTransparent method
  59         self.SetTransparent(self.amount)
  60 
  61 
  62     def MakeTransparent(self, amount):
  63         """
  64         This is how the method SetTransparent() is
  65         implemented on all MS Windows platforms.
  66         """
  67 
  68         hwnd = self.GetHandle()
  69         try:
  70             # DLL library interface constants' definitions.
  71             import ctypes
  72             # Create object to access DLL file user32.dll
  73             _winlib = ctypes.windll.user32
  74             # HERE, i'm not sure (for win10 64bit) :
  75             # style = _winlib.GetWindowLongA(hwnd, 0xffffffecL)
  76             style = _winlib.GetWindowLongA(hwnd, 0x804F700)
  77             style |= 0x00080000
  78             # _winlib.SetWindowLongA(hwnd, 0xffffffecL, style)
  79             _winlib.SetWindowLongA(hwnd, 0x804F700, style)
  80             _winlib.SetLayeredWindowAttributes(hwnd, 0, amount, 2)
  81 
  82         except ImportError:
  83             import win32api, win32con, winxpgui
  84             _winlib = win32api.LoadLibrary("user32")
  85             pSetLayeredWindowAttributes = win32api.GetProcAddress(
  86                 _winlib, "SetLayeredWindowAttributes")
  87             if pSetLayeredWindowAttributes == None:
  88                 return
  89             exstyle = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
  90             if 0 == (exstyle & 0x80000):
  91                 win32api.SetWindowLong(hwnd,
  92                                        win32con.GWL_EXSTYLE,
  93                                        exstyle | 0x80000)
  94             winxpgui.SetLayeredWindowAttributes(hwnd, 0, amount, 2)
  95 
  96 #---------------------------------------------------------------------------
  97 
  98 class MyApp(wx.App):
  99     def OnInit(self):
 100 
 101         #------------
 102 
 103         frame = MyFrame()
 104         frame.Show()
 105 
 106         return True
 107 
 108 #---------------------------------------------------------------------------
 109 
 110 def main():
 111     app = MyApp(redirect=False)
 112     app.MainLoop()
 113 
 114 #---------------------------------------------------------------------------
 115 
 116 if __name__ == "__main__" :
 117     main()


Download source

source.zip


Additional Information

Link :

https://github.com/wxWidgets/wxPython-Classic/blob/master/sandbox/test_transparentFrame.py

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Robin Dunn, Kevin Ollivier, Ray Pasco, the wxPython community...


About this page

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

14/06/20 - Ecco (Updated page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a transparent frame (Phoenix) (last edited 2020-12-13 17:13:27 by Ecco)

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