简体   繁体   English

如何确保应用程序窗口始终位于顶部?

[英]How can I ensure that the application windows is always on top?

I have a simple Python script that runs in a console windows.我有一个在控制台窗口中运行的简单 Python 脚本。

How can I ensure that the console window is always on top and if possible resize it?如何确保控制台窗口始终位于顶部并在可能的情况下调整其大小?

Using Mark's answer I arrived at this:使用马克的回答我得到了这个:

import win32gui
import win32con

hwnd = win32gui.GetForegroundWindow()
win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,100,100,200,200,0)

If you are creating your own window, you can use Tkinter to create an "always on top" window like so:如果您要创建自己的窗口,则可以使用 Tkinter 创建一个“始终在顶部”的窗口,如下所示:

from Tkinter import *
root = Tk()
root.wm_attributes("-topmost", 1)
root.mainloop()

And then put whatever you want to have happen within the main loop.然后把你想要发生的任何事情放在主循环中。

If you are talking about the command prompt window, then you will have to use some Windows-specific utilities to keep that window on top.如果您正在谈论命令提示符窗口,那么您将必须使用一些特定于 Windows 的实用程序来将该窗口保持在顶部。 You can try this script for Autohotkey .您可以为 Autohotkey 尝试此脚本

To do this with the cmd window, you'll probably have to invoke a lot of win32 calls.要使用 cmd 窗口执行此操作,您可能必须调用大量 win32 调用。

  1. Enumerate all the windows using win32gui.EnumWindows to get the window handles使用 win32gui.EnumWindows 枚举所有窗口以获取窗口句柄
  2. Find the "window title" that matches how you run your program.找到与您运行程序的方式相匹配的“窗口标题”。 For example, doubling clicking on a .py file on my system the window title is "C:\\Python26\\python.exe".例如,双击我系统上的 .py 文件,窗口标题为“C:\\Python26\\python.exe”。 Running it on a command line, it is called c:\\Windows\\system32\\cmd.exe - c:\\python26\\python.exe test.py在命令行上运行它,它被称为 c:\\Windows\\system32\\cmd.exe - c:\\python26\\python.exe test.py
  3. Using the appropriate title get the cmd window handle.使用适当的标题获取 cmd 窗口句柄。
  4. Using win32gui.SetWindowPos make your window a "top-most" window, etc...使用win32gui.SetWindowPos使您的窗口成为“最顶层”窗口等...

import win32gui, win32process, win32con
import os

windowList = []
win32gui.EnumWindows(lambda hwnd, windowList: windowList.append((win32gui.GetWindowText(hwnd),hwnd)), windowList)
cmdWindow = [i for i in windowList if "c:\python26\python.exe" in i[0].lower()]
win32gui.SetWindowPos(cmdWindow[0][1],win32con.HWND_TOPMOST,0,0,100,100,0) #100,100 is the size of the window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM