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

后端 未结 3 862
旧巷少年郎
旧巷少年郎 2020-12-09 14:09

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

How can I ensure that the console window is always on top and if possible resize it?

相关标签:
3条回答
  • 2020-12-09 14:31

    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)
    
    0 讨论(0)
  • 2020-12-09 14:31

    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 讨论(0)
  • 2020-12-09 14:50

    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
    
    0 讨论(0)
提交回复
热议问题