Search code examples
wxpython

Keep window always on bottom


I started looking into wxPython today and i was wondering if there was an alternative to wx.STAY_ON_TOP. What i'm looking for is a window to be always on bottom. Unfortunately there is no wx.STAY_ON_BOTTOM window style.

Basically i want other windows (which i do not own) to be always on top of mine even when it has focus. (Think of widgets on a desktop).

I tried using Lower() on that windows focus to no avail.

Any suggestions?


Solution

  • According to this article, the wx.EVT_SET_FOCUS and wx.EVT_KILL_FOCUS events that I expect you were binding to are not fired on a panel or frame that contains widgets which themselves accept focus.

    This answer suggests using the wx.EVT_ACTIVATE event on the frame to catch whenever the window is brought into the foreground or sent into the background. Using the event object you can determine whether or not the window is currently the foreground window.

    The following makes an empty frame that will stay behind all other windows:

    import wx
    
    
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kwargs):
            super(MyFrame, self).__init__(*args, **kwargs)
            self.Bind(wx.EVT_ACTIVATE, self.on_focus)
    
        def on_focus(self, evt):
            self.Lower()
    
    
    if __name__ == '__main__':
        app = wx.App(False)
        main_window = MyFrame(None)
        main_window.Show()
        app.MainLoop()
    

    However, it appears that the wx.EVT_ACTIVATE event is not fired when the title bar of the window is clicked or when anything inside of the window is clicked after the window has gained focus this way. So, if you click on the title bar (not click and drag) you can make the window be on top and stay that way until the window loses focus. This does not exactly match what you are asking for but I think it is more of a feature than a bug.