Home > OS >  How to make a GUI for command-line application in Python?
How to make a GUI for command-line application in Python?

Time:05-20


I am a beginner in Python trying to make a command-line application using Python.

I have developed the program, but I see that my program gets executed in cmd.exe, which seems to be the default for that kind of programs.
But I would like a finer GUI, like that of Windows Terminal. But the problem is that, Windows Terminal is not installed in all Windows computers as is cmd.exe.

Because of this I would like to request you that to suggest any methods to make the GUI or a method to bundle up Windows Terminal with my Python program.

Thank you for any help.

CodePudding user response:

So first we need to understand some terminology. You compare Windows Terminal and cmd.exe, but those are actually two entirely different types of applications. CMD is the old Windows shell. It runs inside a terminal emulator (a.k.a. terminal). There are multiple terminals that it can run under in Windows:

  • By default in Windows 10, it runs in the legacy Windows Console Host. This is the nearly 30 year (maybe more) old terminal for Windows.
  • CMD can also run inside Windows Terminal, the replacement terminal by Microsoft that is available to install in Windows 10 or Windows 11. Under Windows 11, it can become the default terminal for Windows.

When you ask Windows to run a console/terminal app, like your Python CLI app, Windows always runs it in its default terminal -- Under Windows 10, that is always the Windows Console Host. Under Windows 11, it may be either Windows Console Host or Windows Terminal, depending on the user configuration.

But there are also multiple other terminals that can run your CLI Python app in Windows:

Some of the more popular are:

  • ConEmu
  • Cmder
  • MobaXterm

You can find others listed in this blog post.

So it's really not that your app runs in CMD, but that your app runs in Python (the interpreter), which is running under CMD (the shell), which is running under Windows Console Host (the default terminal emulator).

Note that Python can also run under other shells (PowerShell, Linux shells under WSL, Nushell, etc.). These shells can also run in any of the terminals above. Or your Python CLI app can run as the top-level inside a terminal.

Ok, now that we've established some basics ...

But I would like a finer GUI, like that of Windows Terminal.

It's a bit unclear what you mean by that, but there may be other solutions than trying to package Windows Terminal. Both the legacy Console and Windows Terminal should have similar capabilities for your application. Windows Terminal adds things like tabs for the user, but you won't have control over that in your application. Both Console Host and WT can be configured with the same Windows fonts.

One thing you can do in Windows Terminal that you can't in Console Host is define a Profile for your application, including the themes, background image, etc. that you want it to run under. Perhaps that's what you are asking for, but you don't specify that those capabilities.

suggest any methods to make the GUI

If you are looking to create a GUI in Python, consider building it using a GUI Framework rather than as a CLI application. You can even write a terminal emulator in Python, but that would likely be a bit overkill for this.

or a method to bundle up Windows Terminal with my Python program.

That's probably not a good idea. Windows Terminal works best when it is installed as a Microsoft Store app (a.k.a. UWP, Modern, etc.). It is not possible to bundle Microsoft Store apps with another application; just like you can't install another Apple App Store app automatically from another on iOS. It's also possible to install/run Windows Terminal as a traditional .exe, but that's not the default (or supported) way.

You might consider the following:

  • Have your main Python app be a "launcher" that checks to see if Windows Terminal is installed, perhaps by looking for the existing of the wt.exe.

  • If Windows Terminal is installed:

    • Check for the existence of your applications JSON fragment in the appropriate directory (see link). Install it if it doesn't exist.
    • Launch your application by calling something like wt.exe new-tab --profile <your_app_profile> to start or open a new tab in Windows Terminal with the profile you defined in your JSON fragment.
  • If Windows Terminal is not installed:

    • Recommend that the user install it. Open the Microsoft Store link to it.
    • If they choose not to, your launcher should still launch the CLI portion of the app -- Windows will use the default terminal, which should be Windows Console Host.
  • Provide the instructions for running the CLI portion by itself (without the launcher) so that users of other terminal emulators can run it inside their preferred environment.

  • Related