如何让python窗口以“始终在最前面"的方式运行? [英] How to make python window run as "Always On Top"?

查看:145
本文介绍了如何让python窗口以“始终在最前面"的方式运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 中运行一个小程序,它启动一个小窗口,该窗口需要位于所有其他窗口的顶部.我相信这是特定于操作系统的,它是如何在 GNU-Linux 和 GNOME 中完成的?

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?

[更新 - Windows 解决方案]

太棒了,我想我已经成功了.我在 64 位 Vista 的 Eclipse 中使用 Python 2.5.4 和 Pygame 1.9.1.因此,这是针对 Windows 系统的.SetWindowPos 函数记录在这里.我会在解释中提到这一点.

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 SetWindowPos function is documented Here. I will refer to this in my explanation.

进口:

from ctypes import windll

然后我设置了一个调用SetWindowPos"的变量.在用户 32 中:

Then I set up a variable that calls the "SetWindowPos" in user32:

SetWindowPos = windll.user32.SetWindowPos

现在,假设我刚刚制作了一个窗口:

Now, let's say I just made a window:

screen = pygame.display.set_mode((100,100), pygame.NOFRAME)

下一行是关键.这会将窗口设置为位于其他窗口之上.

The next line is the key. This sets the window to be on top of other windows.

SetWindowPos(pygame.display.get_wm_info()['window'], -1, x, y, 0, 0, 0x0001)

基本上,您向 hWnd(窗口句柄)提供调用 display.get_wm_info() 返回的窗口 ID.现在该函数可以编辑您刚刚初始化的窗口.

Basically, You supply the hWnd(Window Handle) with the window ID returned from a call to display.get_wm_info(). Now the function can edit the window you just initialized.

-1 是我们的 hWndInsertAfter.

MSDN 站点说:

通过将 hWndInsertAfter 参数设置为 HWND_TOPMOST 并确保未设置 SWP_NOZORDER 标志,或通过设置窗口在 Z 顺序中的位置使其位于任何现有的最顶层窗口之上,可以将窗口设为最顶层窗口.当一个非最顶层的窗口被设为最顶层时,它拥有的窗口也被设为最顶层.然而,它的所有者并没有改变.

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.

因此,-1 确保窗口位于任何其他现有的最顶层窗口之上,但这可能不适用于所有情况.也许 -2 胜过 -1?它目前对我有用.:)

So, the -1 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. :)

xy 指定正在设置的窗口的新坐标.当 SetWindowPos 函数被调用时,我希望窗口保持在它的当前位置.唉,我找不到一种方法可以轻松地将当前窗口 (x,y) 位置传递给函数.我找到了解决方法,但假设我不应该在这个问题中引入一个新主题.

The x and y specify the new coordinates for the window being set. I wanted the window to stay at its current position when the SetWindowPos 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.

0, 0, 应该指定窗口的新宽度和高度,以像素为单位.好吧,该功能已经在您的 pygame.display.set_mode() 函数中,所以我将它们保留为 0.0x0001 忽略这些参数.

The 0, 0, are supposed to specify the new width and height of the window, in pixels. Well, that functionality is already in your pygame.display.set_mode() function, so I left them at 0. The 0x0001 ignores these parameters.

0x0001 对应于 SWP_NOSIZE 并且是我唯一的 uFlag.提供的文档页面上提供了所有可用 uFlag 的列表.他们的一些十六进制表示如下:

0x0001 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:

  • SWP_NOSIZE = 0x0001
  • SWP_NOMOVE = 0x0002
  • SWP_NOZORDER = 0x0004
  • SWP_NOREDRAW = 0x0008
  • SWP_NOACTIVATE = 0x0010
  • SWP_FRAMECHANGED = 0x0020
  • SWP_SHOWWINDOW = 0x0040
  • SWP_HIDEWINDOW = 0x0080
  • SWP_NOCOPYBITS = 0x0100
  • SWP_NOOWNERZORDER = 0x0200
  • SWP_NOSENDCHANGING = 0x0400

应该是这样!希望对你有用!

That should be it! Hope it works for you!

感谢 john@johnnypops.demon.co.uk 的 John Popplewell 的帮助.

Credit to John Popplewell at john@johnnypops.demon.co.uk for his help.

推荐答案

问题更像是你在使用哪个窗口工具包?PyGTK 和类似的受过教育的谷歌搜索给了我 这个:

The question is more like which windowing toolkit are you using ? PyGTK and similar educated googling gave me this:


gtk.Window.set_keep_above

如前所述,是否遵守此设置取决于窗口管理器.

As mentioned previously it is upto the window manager to respect this setting or not.

编辑以包含 SDL 特定的内容Pygame 使用 SDL 进行显示工作,并且显然不适合 窗口工具包.SDL Window 可以放在上面讨论 这里.

Edited to include SDL specific stuff Pygame uses SDL to do display work and apprently does not play nice with Windowing toolkits. SDL Window can be put on top is discussed here.

这篇关于如何让python窗口以“始终在最前面"的方式运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆