WxPython在特定位置添加文本

问题描述 投票:0回答:1

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

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

python user-interface wxpython wxtextctrl textctrl
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."

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.