Set Windows command-line terminal title in Python

白昼怎懂夜的黑 提交于 2019-12-17 15:25:39

问题


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?


回答1:


This works for Python2.7 under Windows.

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



回答2:


On Windows, a simple console command will suffice:

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

Nice & easy.




回答3:


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.




回答4:


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")




回答5:


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.




回答6:


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



回答7:


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.




回答8:


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);}


来源:https://stackoverflow.com/questions/7387276/set-windows-command-line-terminal-title-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!