0

I have a basic python program which takes a simple user input and performs some operation on it and displays an output. When I run this python file, it displays in the default black-background command prompt like window, which is resizable infinitely as far as I can see. I want my script/mini program to run in a small window instead which is locked in its dimensions. All resources regarding this point me towards doing the same thing in a Tkinter environment. Is there a way to do that without Tkinter?

I tried to find functions or libraries which help me limit the dimensions of the python output window but I could not find any.

Steel
  • 1
  • 1
    Does this answer your question? [Automatically Resize Command Line Window on Windows](https://stackoverflow.com/questions/9458870/automatically-resize-command-line-window-on-windows) – pflakus Apr 21 '23 at 11:15
  • Or simply limit the amount of output to 24 lines (bonus points if you want to support resizing the CMD window before running the script). – tripleee Apr 24 '23 at 14:11
  • Or perhaps you are actually looking for help with writing a simple curses application. I don't know if it's well-supported on Windows but this would let you write a basic text interface like what you see on old pre-Windows software. A popular third-party library these days is Rich; see https://textualize.io – tripleee Apr 24 '23 at 14:13

1 Answers1

0

You can use pygetwindow, os and sleep.

import pygetwindow
import os
from time import sleep

First you change the name to the one you want.

DesiredTitle = "Title"
os.system("title " + DesiredTitle)

os.system takes some time to change the title and if you don't put a delay pygetwindow.getWindowsWithTitle will not catch the window. 0.016 seconds works on my computer and I have not tested it further.

sleep(0.016)

Than you select the window whit the desired title.

win = pygetwindow.getWindowsWithTitle(DesiredTitle)[0]

Now you can move it and change the size.

win.moveTo(x, y)
win.resizeTo(width, height)

Here is the full code:

import pygetwindow
import os
from time import sleep

DesiredTitle = "Title"

os.system("title " + DesiredTitle)
sleep(0.016)

win = pygetwindow.getWindowsWithTitle(DesiredTitle)[0]
win.moveTo(x, y)
win.resizeTo(width, height)