title: How to make python window run as "Always On Top"?
body: <p>I am running a little program in python that launches a small window that needs to stay on top of all the other windows. I believe this is OS specific, how is it done in GNU-Linux with GNOME?</p>
  <p>[<strong>Update - Solution for Windows</strong>]</p>
  <p>Lovely, I think I got it working. I am using Python 2.5.4 with Pygame 1.9.1 in Eclipse on Vista 64-bit. Thus, this is for windows systems. The <code>SetWindowPos</code> function is documented <a href="http://msdn.microsoft.com/en-us/library/ms633545%28VS.85%29.aspx" rel="noreferrer">Here</a>. I will refer to this in my explanation.</p>
  <p>Imports:</p>
  <pre><code>from ctypes import windll
  </code></pre>
  <p>Then I set up a variable that calls the &quot;SetWindowPos&quot; in user32:</p>
  <pre><code>SetWindowPos = windll.user32.SetWindowPos
  </code></pre>
  <p>Now, let's say I just made a window:</p>
  <pre><code>screen = pygame.display.set_mode((100,100), pygame.NOFRAME)
  </code></pre>
  <p>The next line is the key. This sets the window to be on top of other windows.</p>
  <pre><code>SetWindowPos(pygame.display.get_wm_info()['window'], -1, x, y, 0, 0, 0x0001)
  </code></pre>
  <p>Basically, You supply the <code>hWnd</code>(Window Handle) with the window ID returned from a call to <code>display.get_wm_info()</code>. Now the function can edit the window you just initialized.</p>
  <p>The <code>-1</code> is our <code>hWndInsertAfter</code>.</p>
  <p>The MSDN site says:</p>
  <blockquote>
  <p>A window can be made a topmost window either by setting the hWndInsertAfter parameter to HWND_TOPMOST and ensuring that the SWP_NOZORDER flag is not set, or by setting a window's position in the Z order so that it is above any existing topmost windows. When a non-topmost window is made topmost, its owned windows are also made topmost. Its owners, however, are not changed.</p>
  </blockquote>
  <p>So, the <code>-1</code> makes sure the window is above any other existing topmost windows, but this may not work in all cases. Maybe a -2 beats a -1? It currently works for me. :)</p>
  <p>The <code>x</code> and <code>y</code> specify the new coordinates for the window being set. I wanted the window to stay at its current position when the <code>SetWindowPos</code> function was called on it. Alas, I couldn't find a way to easily pass the current window (x,y) position into the function. I was able to find a work around, but assume I shouldn't introduce a new topic into this question.</p>
  <p>The <code>0, 0,</code> are supposed to specify the new width and height of the window, in pixels. Well, that functionality is already in your <code>pygame.display.set_mode()</code> function, so I left them at 0. The <code>0x0001</code> ignores these parameters.</p>
  <p><code>0x0001</code> corresponds to SWP_NOSIZE and is my only uFlag. A list of all the available uFlags are on the provided documentation page. Some of their Hex representations are as follows:</p>
  <ul>
  <li>SWP_NOSIZE          =   0x0001</li>
  <li>SWP_NOMOVE          =   0x0002</li>
  <li>SWP_NOZORDER        =   0x0004</li>
  <li>SWP_NOREDRAW        =   0x0008</li>
  <li>SWP_NOACTIVATE      =   0x0010</li>
  <li>SWP_FRAMECHANGED    =   0x0020</li>
  <li>SWP_SHOWWINDOW      =   0x0040</li>
  <li>SWP_HIDEWINDOW      =   0x0080</li>
  <li>SWP_NOCOPYBITS      =   0x0100</li>
  <li>SWP_NOOWNERZORDER   =   0x0200</li>
  <li>SWP_NOSENDCHANGING  =   0x0400</li>
  </ul>
  <p>That should be it! Hope it works for you!</p>
  <p>Credit to John Popplewell at john@johnnypops.demon.co.uk for his help.</p>