开发者

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

I have a simple Python script that runs in a console windows.

How can I ensure that the console window is always on to开发者_运维百科p 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)


To do this with the cmd window, you'll probably have to invoke a lot of win32 calls.

  1. Enumerate all the windows using win32gui.EnumWindows to get the window handles
  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". Running it on a command line, it is called c:\Windows\system32\cmd.exe - c:\python26\python.exe test.py
  3. Using the appropriate title get the cmd window handle.
  4. Using win32gui.SetWindowPos make your window a "top-most" window, etc...

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


If you are creating your own window, you can use Tkinter to create an "always on top" window like so:

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. You can try this script for Autohotkey.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜