[英]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 调用。
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.