Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the position of the console window with Python (Windows)

Is it possible to change the position of the Windows console through python? If not, is there any workaround?

I don't know if you would need any specific information, but just in case: I'm using Windows 8.1 (64x), Python 3.5.0, the console is being spawned through Popen and the main objective is to move it to the top right corner.

If any info is needed, please let me know.

like image 285
ArsonFG Avatar asked Oct 29 '22 03:10

ArsonFG


1 Answers

I adapted this from an answer by NYMK

This will move and resize a single command prompt window (opened by CMD). It is simple and does not handle errors, multiple command prompt windows or command line.

import win32gui

appname = 'Command Prompt'
xpos = 50
ypos = 100
width = 800
length = 600

def enumHandler(hwnd, lParam):
    if win32gui.IsWindowVisible(hwnd):
        if appname in win32gui.GetWindowText(hwnd):
            win32gui.MoveWindow(hwnd, xpos, ypos, width, length, True)


win32gui.EnumWindows(enumHandler, None)

for a full commandline ready - try:

import win32gui
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("app_name", type=str, default='Command Prompt', help="The window name")
parser.add_argument("xpos", type=int,  default=0, help="x position: 0 or greater")
parser.add_argument("ypos", type=int,  default=0, help="y position: 0 or greater")
parser.add_argument("width", type=int, default=100, help="window width: 10 or greater")
parser.add_argument("length", type=int, default=100, help="window length: 10 or greater")

args = parser.parse_args()

appname = args.app_name
xpos = args.xpos
ypos = args.ypos
width = args.width
length = args.length


def enumHandler(hwnd, lParam):
    if win32gui.IsWindowVisible(hwnd):
        if appname in win32gui.GetWindowText(hwnd):
            win32gui.MoveWindow(hwnd, xpos, ypos, width, length, True)


win32gui.EnumWindows(enumHandler, None)
like image 101
DarkLight Avatar answered Nov 13 '22 05:11

DarkLight