如何使python窗口以"Always On Top"(始终在线)的方式运行? [英] How to make python window run as "Always On Top"?

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

问题描述

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

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

然后,我设置了一个变量,该变量在user32中调用"SetWindowPos":

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函数时,窗口保持在其当前位置. las,我找不到轻松将当前窗口(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.提供的文档页面上列出了所有可用的uFlags.它们的十六进制表示形式如下:

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
  • 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和受过类似教育的Google搜索使我:

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窗口放在顶部进行讨论此处.

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窗口以"Always On Top"(始终在线)的方式运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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