Home > OS >  Setting a python prompt title a random string
Setting a python prompt title a random string

Time:08-21

I am new to python and I'm having a little issue where I can't manage to set a title of a command prompt to a random string.

I know how to print random strings but don't really know how to implement it in a title bar.

import random
import string

letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(10)) )

this above was pretty easy to find out.

I tried:

letter =  string.ascii_lowercase
os.system("TITLE "   random.choices(letter))

and

letter =  string.ascii_lowercase
tit = random.choices(letter)
os.system("TITLE "   tit))

but none of this worked.

In c I did it like this:

std::string RandomString(const size_t length)
{
    std::string r;
    static const char bet[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyzZ1234567890" };
    srand((unsigned)time(NULL) * 5);
    for (int i = 0; i < length;   i)
        r  = bet[rand() % (sizeof(bet) - 1)];
    return r;
}
int main(main, char**)
{ 
SetConsoleTitleA(RandomString(30).c_str());

//code
}

it looks like this

But I just can't manage to do it in python. Is it even possible and how?

Thanks in advance!

CodePudding user response:

You were on the right track with this code, but in the second line...

letter = string.ascii_lowercase
os.system("TITLE "   random.choices(letter))

You'll want to modify how you grab random letters so you can create a window-title-safe string out of the list random.choices() creates. Here's an example snippet of code that works for me, you can modify k= to change the length of the randomized string.

os.system("title "   "".join(random.choices(letter, k=5))) # 5 character long random string as the title.

CodePudding user response:

You can use the ctypes module that is included with Python to call C APIs in DLLs.

From the SetConsoleTitle docs, the function is available in kernel32.dll. For APIs that take strings they typically have an A version and a W version, which take ANSI and wide strings, respectively. For Python, the str type is passed as a wide string, so prefer the wide version:

import random
import string
import ctypes as ct
from ctypes import wintypes as w           # defines many Windows types as ctypes types.

dll = ct.WinDLL('kernel32')                # DLL where API is found
dll.SetConsoleTitleW.argtypes = w.LPCWSTR, # sequence of argument types
dll.SetConsoleTitleW.restype = w.BOOL      # return type

letters = string.ascii_lowercase
title = ''.join(random.choice(letters) for i in range(10))
print(dll.SetConsoleTitleW(title))  # returns 1 (TRUE) on success
input('Press Enter to continue.')  # title may change back after Python exits.

Note: os.system will work in this case, too, but use input() to pause Python. Once it exits it may change back to command prompt default. It did for me, even for a C version. The command prompt sets the title constantly, at least with Windows 10.

CodePudding user response:

try

os.system("title " tit)

  • Related