在Python中设置Windows命令行终端标题

发布于 2024-12-04 06:44:44 字数 229 浏览 0 评论 0原文

我在 Windows 计算机上运行某个 Python 脚本的多个实例,每个实例都来自不同的目录并使用单独的 shell 窗口。不幸的是,Windows 为每个 shell 窗口提供了相同的名称:

<User>: C:\Windows\system32\cmd.exe - <script.py>

是否可以通过 Python 命令将此名称设置为其他名称?

I'm running several instances of a certain Python script on a Windows machine, each from a different directory and using a separate shell windows. Unfortunately Windows gives each of these shell windows the same name:

<User>: C:\Windows\system32\cmd.exe - <script.py>

Is it possible to set this name to something else through a Python command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

思念满溢 2024-12-11 06:44:44

在 Windows 上,一个简单的控制台命令就足够了:

from os import system
system("title " + myCoolTitle)

又好又简单。

On Windows, a simple console command will suffice:

from os import system
system("title " + myCoolTitle)

Nice and easy.

嘿咻 2024-12-11 06:44:44

这适用于 Windows 下的 Python2.7。

>>> import ctypes
>>> ctypes.windll.kernel32.SetConsoleTitleA("My New Title")

This works for Python2.7 under Windows.

>>> import ctypes
>>> ctypes.windll.kernel32.SetConsoleTitleA("My New Title")
策马西风 2024-12-11 06:44:44

由于没有足够的代表,我无法向上述帖子添加评论 - 因此作为新帖子。

Python 3 中,您可以使用:

import ctypes
ctypes.windll.kernel32.SetConsoleTitleW("My New Title")

我编辑了这个答案:请注意,它现在使用 SetConsoleTitleW,它是 SetConsoleTitle 函数。
这样您就可以使用 unicode,而不再需要将字符串/变量编码为字节对象。您只需将参数替换为字符串变量即可。

Due to not enough rep I cannot add a comment to the above post - so as a new post.

In Python 3 you can use:

import ctypes
ctypes.windll.kernel32.SetConsoleTitleW("My New Title")

I edited this answer: please remark, that it now uses SetConsoleTitleW, which is the Unicode version of the SetConsoleTitle function.
This way you can use unicode and no longer have to encode the string/variable to a byte object. You can just replace the argument with the string variable.

真心难拥有 2024-12-11 06:44:44

由于您只会在 Windows 上运行此程序(IOW,没有跨平台的方法来执行此操作):

  1. 下载并下载安装 Python 的 Win32 扩展
  2. 在脚本内部,您可以更改控制台的标题使用 函数

    win32console.SetConsoleTitle("My Awesome App")

Since you're only going to be running this on Windows (IOW, there's not a cross-platform way to do this):

  1. Download & install the Win32 extensions for python
  2. Inside of your script, you can change the title of the console with the function

    win32console.SetConsoleTitle("My Awesome App")

拧巴小姐 2024-12-11 06:44:44

我不知道如何从脚本中更改 cmd 窗口标题。

但是,如果使用 start 命令<,则可以在启动脚本时设置标题/a>.

I am not aware of a way to change the cmd window title from within the script.

However, you can set the title when launching the script if you use the start command.

才能让你更想念 2024-12-11 06:44:44

发布的 system() 和 的比较基于 windll 的方法

结合添加与已发布的两种方法相关的延迟开销的小型定量比较:

|>>> from zmq import Stopwatch
|>>> aSWX = Stopwatch()

|>>> from os import system
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15149L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15347L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15000L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14674L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14774L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14551L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14633L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15202L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14889L [us]

|>>> from ctypes import windll
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()   5767L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    643L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    573L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    749L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    689L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    651L [us]

在某些情况下,人们可能会花费大约半毫秒(但是不是几十个) windll.kernel32 方法似乎很有前途,并且可能更好地用于替代显示 WatchDOG / StateVARs / ProgressLOG / 自动自我诊断消息,存在在长时间运行的进程中,以软实时需求有效地显示。

Comparison of the posted system() & windll-based methods

tying to add a small quantitative comparison of latency overheads associated with two of the posted methods:

|>>> from zmq import Stopwatch
|>>> aSWX = Stopwatch()

|>>> from os import system
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15149L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15347L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15000L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14674L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14774L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14551L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14633L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15202L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14889L [us]

|>>> from ctypes import windll
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()   5767L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    643L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    573L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    749L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    689L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    651L [us]

In cases, where one might spend about a half of millisecond ( but not some tens of that ) the windll.kernel32 method seems promising and may serve better for an alternative display of a WatchDOG / StateVARs / ProgressLOG / auto-self-diagnostic messages, being efficiently displayed in a soft real-time need, during long running processes.

゛时过境迁 2024-12-11 06:44:44

使用:

import ctypes
ctypes.windll.kernel32.SetConsoleTitleW('new title')

或:

import os
os.system('title new title')

Use:

import ctypes
ctypes.windll.kernel32.SetConsoleTitleW('new title')

Or:

import os
os.system('title new title')
高速公鹿 2024-12-11 06:44:44

使用 OS 模块 与终端交互

  1. 导入库 - import os
  2. 与终端交互更改标题 - os.system('title your_tile')

解释:

所以如果你打开您的终端并输入 title your_title 会将终端标题更改为 your_title
CMD

在 Python 中,OS.system() 用于在终端中输入

这就是使用 Python 更改终端标题的方法

Using OS module To interact with the terminal

  1. Import the library - import os
  2. Interact with the terminal to change title - os.system('title your_tile')

Explanation:

So if you open your terminal and type title your_title is will change the terminal title to your_title
CMD

In Python the OS.system() is used to type into the terminal

This is how you will change title of terminal with Python

才能让你更想念 2024-12-11 06:44:44

如果启动 Idle-shell 是一个选项而不是 cmd壳:

idle.py [-c command] [-d] [-e] [-s] [-t title] [arg] ...

-c command  run this command
-d          enable debugger
-e          edit mode; arguments are files to be edited
-s          run $IDLESTARTUP or $PYTHONSTARTUP first
-t title    set title of shell window

If starting the Idle-shell is an option instead of the cmd shell:

idle.py [-c command] [-d] [-e] [-s] [-t title] [arg] ...

-c command  run this command
-d          enable debugger
-e          edit mode; arguments are files to be edited
-s          run $IDLESTARTUP or $PYTHONSTARTUP first
-t title    set title of shell window
迟到的我 2024-12-11 06:44:44

现在可以通过将标准转义序列输出到控制台(stdout)来更改任何语言中的窗口标题。这是批处理文件中的一个工作示例 更改命令提示符以仅显示当前目录名称,但只需打印 ESC 右括号 2 分号 - title-here BEL (control-G) 就可以了。还有一个易于修改的 PHP 示例:

function windowTitle($title)
  {printf("\033]2;%s\007", $title);}

It is now possible to change the window title from within any language via outputting a standard escape sequence to the console (stdout). Here's a working example from a batch file Change command prompt to only show current directory name however just printing ESC close-bracket 2 semicolon your-title-here BEL (control-G) will do it. Also an easily adapted PHP example:

function windowTitle($title)
  {printf("\033]2;%s\007", $title);}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文