简体   繁体   中英

Tkinter entry box disable spaces

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM