wxPython: Showing a window without activating it (bringing it to focus).

I had a really hard time figuring out how to do this so I decided to post the (simple) solution on my blog.

I basically needed to be able to show a window without it stealing focus from the currently active top-level window.  A simple frame.Show() was not sufficient because it activates the window.  Eventually I went to a platform-specific solution for Windows:

from ctypes import windll
windll.user32.ShowWindow(window.GetHandle(), 8)

The second argument (8) is the SHOWNA constant which shows the window but does not activate it.  This worked but ended up breaking other things.  (The display contents underneath the window were not redrawn properly when the window was eventually closed.)  So, after downloading the wxWidgets source and browsing for a few minutes I found a new solution.

window.Disable()
window.Show()
window.Enable()

If a window is disabled, wxWidgets uses the SHOWNA constant when showing it.  This also fixed the redrawing issue, perhaps because now wx is still in charge of showing the window (maybe I was messing up some state internal to wxPython).

So there you have it.  To show a window without stealing focus in wxPython (or wxWidgets), just disable it, show it and then enable it.

This entry was posted in Programming. Bookmark the permalink.

3 Responses to wxPython: Showing a window without activating it (bringing it to focus).

  1. Matt says:

    yeah thx for this tip !
    it works fine

    However it seems that wx.ComboBox loses its focus when the window popups with this method

  2. kakarukeys says:

    hi, the tip doesn’t seem to work when showing window over a command prompt, any idea?

  3. ouned says:

    For some reason this doesnt work for me :(
    Im using wxGTK + wxMSW 2.9.0 ( tested it on both )

    So my window is still stealing the focus. I really need it because its a chat window which should not steal the focus.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">