如何在Python中为命令行应用程序制作GUI?
我正在尝试使用Python进行命令行应用程序。
我已经开发了该程序,但是我看到我的程序在cmd.exe
中似乎是默认的一种程序。
但是我想要一个更好的GUI,就像Windows终端一样。但是问题是,Windows终端并未像cmd.exe
。
一样,在所有Windows计算机中
安装通过我的Python程序将Windows终端捆绑的方法。
I am 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,首先我们需要了解一些术语。您比较Windows终端和
cmd.exe
,但实际上是两个完全不同的应用程序类型。 CMD是旧的Windows shell 。它在A 终端模拟器(又称终端)内运行。它可以在Windows中运行多个终端:当您要求Windows运行控制台/终端应用程序(例如Python CLI应用程序)时,Windows总是在默认终端运行它 - 在Windows 10下,始终是Windows Console Host。在Windows 11下,它可以是Windows控制台主机或Windows终端,具体取决于用户配置。
但是,还有其他多个终端可以在Windows中运行您的CLI Python应用程序:
一些更受欢迎的终端是:
您可以在此博客文章。
因此,您的应用程序实际上不是在CMD中运行在Windows控制台主机(默认终端仿真器)下。
请注意,python也可以在其他外壳(PowerShell,WSL下的Linux shell,Nushell等)下运行。这些壳也可以在上面的任何端子中运行。或者您的Python CLI应用程序可以作为终端内部的顶级运行。
好的,现在我们已经建立了一些基础知识...
有点不清楚您的意思,但是除了试图打包Windows终端外,还有其他解决方案。传统控制台和Windows终端都应具有类似的功能 。 Windows Terminal添加了用户的标签之类的内容,但是您在应用程序中无法控制它。控制台主机和WT都可以使用相同的Windows字体配置。
您在Windows终端中执行的一件事是您无法在控制台主机中执行的一件事是为您的应用程序定义A profile ,包括主题,背景图像等。它在下面运行。也许这就是您的要求,但是您没有指定这些功能。
如果您想在Python创建GUI, 框架而不是作为CLI应用程序。您甚至可以在Python中编写终端模拟器,但这可能有点过分杀伤。
这可能不是一个好主意。 Windows Terminal将其安装为Microsoft Store应用程序(又称UWP,Modern等)时,最有效。不可能将Microsoft Store应用程序与另一个应用程序捆绑在一起;就像您无法从iOS上的另一个Apple App Store应用程序自动安装另一个Apple App Store应用程序一样。也可以以传统的
.exe
来安装/运行Windows终端,但这不是默认(或支持)方式。您可以考虑以下内容:
让您的主要Python应用程序为“启动器”,该应用程序检查是否已安装Windows终端,也许是通过寻找
wt.exe
。 P>如果安装了Windows终端:
wt.exe new-tab -profile< your_app_profile>
之类的内容来启动您的应用程序,以启动或打开Windows终端中的新选项卡,其中包含您在JSON Fragment中定义的配置文件。如果未安装Windows终端:
提供用于运行CLI部分的说明(无发射器),以便其他终端模拟器的用户可以在其首选环境中运行它。
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: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:
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 ...
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.
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.
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:
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:
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.