Set Windows command-line terminal title in Python

前端 未结 9 1919
攒了一身酷
攒了一身酷 2020-12-01 00:52

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 giv

相关标签:
9条回答
  • 2020-12-01 01:23

    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);}
    
    0 讨论(0)
  • 2020-12-01 01:24

    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.

    0 讨论(0)
  • 2020-12-01 01:26

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

    0 讨论(0)
  • 2020-12-01 01:29

    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.

    0 讨论(0)
  • 2020-12-01 01:32

    This works for Python2.7 under Windows.

    >>> import ctypes
    >>> ctypes.windll.kernel32.SetConsoleTitleA("My New Title")
    
    0 讨论(0)
  • 2020-12-01 01:33

    In place of XYZ, enter the window name you want to keep.

    import os
    os.system("title XYZ")
    
    0 讨论(0)
提交回复
热议问题