【问题标题】:WxPython adding text in a specific placeWxPython 在特定位置添加文本
【发布时间】:2020-03-06 13:03:08
【问题描述】:
在我制作的 GUI 中(使用 wxpython),我需要在 TextCtrl 的特定位置附加文本(如果需要,我可以将其更改为其他 textEntry)。
例如我有这样的文字:
Yuval 是一名冲浪者。
他喜欢 (HERE) 去海滩。
我想在“喜欢”这个词之后附加一个或几个词。如何使用 wxpython 模块做到这一点?
【问题讨论】:
标签:
python
user-interface
wxpython
wxtextctrl
textctrl
【解决方案1】:
如果您总是知道要添加其他单词的单词,您可以执行以下操作:
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."
希望这会有所帮助。