WxPython 在特定位置添加文本

WxPython adding text in a specific place

在我制作的GUI中(使用wxpython),我需要在TextCtrl的特定位置附加文本(如果需要我可以将其更改为其他textEntry)。 例如我有这个文本:
Yuval 是一名冲浪者。
他喜欢(这里)去海滩。

我想在单词 "likes" 后附加一个或几个单词。我怎样才能使用 wxpython 模块做到这一点?

如果您总是知道要在其后添加其他单词的单词,您可以这样做:

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."

如果相反,你知道的是这个词的位置,后面必须加上其他词:

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."

希望对您有所帮助。