0

在我制作的 GUI(使用 wxpython)中,我需要在 TextCtrl 的特定位置附加文本(如果需要,我可以将其更改为其他 textEntry)。例如我有这样的文字:
Yuval is a surfer。
他喜欢(这里)去海滩。

我想在“喜欢”这个词之后附加一个或几个词。如何使用 wxpython 模块做到这一点?

4

1 回答 1

0

如果您总是知道要添加其他单词的单词,则可以执行以下操作:

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

希望这可以帮助。

于 2019-11-13T15:36:08.587 回答