Hello, I would like to prevent users to enter numbers and some signs in the player name input field. Is that possible with DirectEntry?
I currently use this. Not the most sophisticated piece of code you’ve ever seen, but it works:
import string def check_entry(self, enteredtext): textok = False if not len(enteredtext): self.entry_info["text"] = "Enter a name with at least one character!" else: textok = True if len(enteredtext) > 18: self.entry_info["text"] = "Enter a name with less than 18 characters!" textok = False else: textok = True for character in enteredtext: if character in string.punctuation: textok = False self.entry_info["text"] = "Can not use special characters like * or ]" else: textok = True if textok: enteredtext = enteredtext.capitalize() self.do_stuff_with(enteredtext)
It checks the entered string after the fact, not before, as you propably want. I guess for that you had to use the
I don’t have problem checking a string and fixing it after it has been added to the DirectEntry (by pressing Enter or another custom button). What I need is to not allow some characters to appear on screen at all.
Hmm… These may not be the best ways to do what you want done, but the following are what occur to me:
Sub-class DirectEntry and override whatever text-entry methods it has; in your overrides, first check the input, and then, if it passes, call the DirectEntry version of the method.
Create a class that wraps DirectEntry, and use the bind method to catch input events (for example: self.entry.bind(DGG.TYPE, self.type)).
I’m not sure that either will be performant, however; I think that I had responsiveness problems using the latter method.
I’d have a function to strip the input from the baned symbols and every time one of those keys is pressed I’d run my striping function. And also run it after the input is submitted just in case (on windows you can past text into a DirectEntry from the system clipbord). A bit crude, but it should work.