Hi everyone! I’m not exactly sure how to word this, but I am trying to have my code replace a letter/word inputted by a user in a textbox by a symbol instantly (before they click enter). As in, say the user was to type out “pi”, I’m trying to have my code replace that with the “ℼ” symbol on the spot, but I’m not exactly sure how to go about it, and I would appreciate any help! Thank you in advance!
You can go to the text box events and set an event for ‘change’ and do some regex magic to search and replace.
Something like this:
self.textbox.set_event_handler('change',self.regex_function)
def regex_function(self,**event_args):
DO REGEX MAGIC HERE
Here is where you can read up on regex:
You could use the python package fuzzywuzzy package to match user input against a dictionary of your key words and their replacement values. Here’s a possible psuedocode:
- Create a keywords dictionary of words that you want to match as keys and their symbols as values
- convert user input to lower case
- use fuzzywuzzy to calculate the Levenshtein Distance (LD) of the input text against each of the keys in the keywords dictionary
- Get the value (or symbol) of the key with the highest LD from the keyword dictionary and replace it
Above approach would be quick if the number of keywords is small. Otherwise, you should consider machine learning approaches.
There is an old joke, “I had a problem, and I tried to solve it with regex… …now I have two problems”
You could use the text box change event to check using pure python, similar to what I did here for a solution to a question I didn’t even understand.
Replacing the text as you type is usually a bad idea. It makes it impossible to press the backspace, arrow keys or do any other editing operations. It is usually better (from a user experience point of view) to format, replace, etc. after the user has finished typing the text.
Said that, let’s get back to the question you actually asked
If you really want to change the text while the user is typing it, as @ianb suggests, you can use the change
event of the input component.
If you think that changing the text as the user types and preventing them from editing it would be to harsh, you can use the lost_focus
and or pressed_enter
events.
First of all, thank you for the advice! So I’m trying to go about it like you suggested, here’s my code:
def textchange(self,**properties):
for i in range(len(self.text_box_1.text)):
if self.text_box_1.text[i]==“d”:
self.text_box_1.text[i] == " ÷ "
But it doesn’t seem to work, not exactly sure how to go about this, and I would really appreciate your help
- Go to the design pane
- Select the input text_box
- Go to the “CONTAINER PROPERTIES” section at the bottom right
- Look for the change event input box under the EVENTS section
- add the handler function name to the change event input box (in you case this should be “textchange” )
The problem here is this line :
self.text_box_1.text[i] == " ÷ "
You have put two ==
here instead of =
so your code is just comparing them. Try removing that extra sign.
Also, there is a better way to do this by using the replace method
def textchange(self,**properties):
self.text_box_1.text= self.text_box_1.text.replace('d','÷')
Thank you so much! It worked I appreciate it
This worked, thank you for pointing out my mistake!