Tkinter entry box disable spaces

Question:

Can you block the user from using spaces in an entry box in tkinter?

Its for creating a filename, so if they put spaces it won’t work.

Asked By: hastur

||

Answers:

You can use either this to prevent the user:

if no_spaces.count(' ') > 0:
    print("Please try again")

or the function strip. Python String strip() function will remove leading and trailing whitespaces, so if the user type "my file name.txt" your file will be myfilename.txt

You can also replace the spaces by underscores:

mystring.replace(" ", "_")

Regards,
Izabela.

Answered By: Izalion