1

I'm running a simple script that compares two values and prints it's result, and I want to anyone who executes it, opens the CMD or Powershell and keep it on top of every other window, as if it was the GUI of the script (just to show Python prints).

E.G.

NUM1 = 0
NUM2 = 5

while NUM1 != NUM2:
    print(NUM1, NUM2)
    NUM1 += 1

print(NUM1, NUM2, 'are equals.')

The goal I have in mind is to create an .exe or keep as a script, which uses CMD as if it was its "GUI" to show my Python script prints, the same way as if you were using CMD commands (cd ~/main.py) to run the script.

When executed, it should open and resize that CMD window and make it always on top, and then run the Python code.


For now, I'm forcing the users to use a .bat file, which is re-sized, executes main.py, and recommending using Autohotkey to keep it on top.

Actual .bat file

mode con: cols=60 lines=10
timeout 10
cls

python main.py

Thanks in advance and sorry if I'm skipping something, it's my first post on StackOverflow

neBel
  • 11
  • 2
  • It's an interesting problem, but it's not clear what you're asking: How does Autohotkey come into play here? Is the batch file an _alternative_ to creating an `.exe`? – mklement0 Dec 12 '21 at 13:36
  • If you're looking for a PowerShell solution to make the current window always-on-top, see [this answer](https://stackoverflow.com/a/58542670/45375). – mklement0 Dec 12 '21 at 13:38
  • @mklement0 I'm using and recommending to use Autohotkey just to keep the CMD on top of everything and exaclty, I'm using the .bat file as an alternative to creating an .exe, as I only need to show the Python prints on an always on top and visible window. I was trying to avoid using Tkinter to create a GUI and creating an .exe just for this, but maybe it's the only solution(?), as it's simple to attribute it as an always on top window. – neBel Dec 12 '21 at 18:16
  • What is it that you're ultimately looking for? A _single_ executable (whether `.exe` or script) that performs all tasks (resize, make always-on-top, run Python code)? – mklement0 Dec 12 '21 at 20:41
  • @mklement0 The goal I have in mind is to create an .exe or script, which uses CMD as a "GUI" to show my Python script prints, as if you were using CMD (cd ~/main.py) to the script. When executed, it should open and resize that CMD window and make it always on top, and then run the Python code. Sorry if I was not clear at all ;S! – neBel Dec 13 '21 at 17:21
  • So, in other words: the only thing missing from your batch-file solution is the always-on-top part? Having a separate `.py` file is acceptable? – mklement0 Dec 13 '21 at 17:22
  • @mklement0 That's it! The ultimate goal is to create an .exe with all these steps, so the user is not forced to install Python (if it's remotely possible). The solution which came to my mind, is just to create a GUI to show the prints, but I was wondering if there's a simpler solution, as the program itself doesn't need anything else than showing those prints. – neBel Dec 13 '21 at 17:30
  • If you want to avoid having to install Python, then `pyinstaller` is probably your only option, which in turn means that you'll have to perform all tasks _from Python_ (call `mode.exe`, use the WinAPI to make the window always-on-top, e.g.. via `pip install pypiwin32`) – mklement0 Dec 13 '21 at 17:41
  • Thanks a lot @mklement0, I'll check the WinAPI to call CMD from Python and perform the tasks described! – neBel Dec 13 '21 at 20:09
  • You're welcome, @neBel. Note that you need the WinAPI only to make the window always-on-top, for calling `mode.com` (I misspoke about `mode.exe`), you need the `os` or `subprocess` module. – mklement0 Dec 13 '21 at 20:13

0 Answers0