pythonuser-interfacewxpythonwxtextctrltextctrl

WxPython adding text in a specific place


In the GUI that I make (With wxpython), I need to append text in a specific place of the TextCtrl (I can change it to other textEntry if it is needed). for example I have this text:
Yuval is a surfer.
He likes (HERE) to go to the beach.

I want to append a word or couple of words after the word "likes". How can I do that using wxpython modoule?


Solution

  • If you always know the word after which you want to add other words you could do something like this:

    new_text = 'Yuval is a surfer'
    search_text = 'likes'
    original_text = "He likes to go to the beach."
    result = original_text.replace(search_text, " ".join([search_text, new_text]))
    
    print(result)
    
    #Prints: "He likes Yuval is a surfer to go to the beach."
    

    If, on the contrary, what you know is the position of the word after which other words must be added:

    new_text = 'Yuval is a surfer'
    word_pos = 1
    original_text = "He likes to go to the beach."
    
    #convert into array:
    splitted = original_text.split()
    #get the word in the position and add new text:
    splitted[word_pos] = " ".join([splitted[word_pos], new_text])
    #join the array into a string:
    result = " ".join(splitted)
    print(result)
    
    #Prints: "He likes Yuval is a surfer to go to the beach."
    

    Hope this helps.