以下是从here复制的简单rst编辑器的代码,在此代码中,selection_text被所需的标签包围,但这是一个问题我只能从左到右选择文本,当从右边选择文本时离开它不像以前那样工作,是否有可能从其他方面选择其他文本编辑器?
from kivy.app import App
from kivy.base import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
Builder.load_string("""
<root_wgt>:
orientation: 'vertical'
BoxLayout:
size_hint_y:0.2
Button:
text: 'Emphasize'
on_press: root.emphasize()
Button:
text: 'Section Header'
on_press: root.add_section_header()
Button:
text: 'Subection Header'
on_press: root.add_sub_section_header()
BoxLayout:
orientation: 'vertical'
TextInput:
id: textinput
RstDocument:
id: rstdocument
text: textinput.text
""")
class root_wgt(BoxLayout):
def emphasize(self):
text = self.ids.textinput.text
selection = self.ids.textinput.selection_text
begin = self.ids.textinput.selection_from
end = self.ids.textinput.selection_to
new_text = text[:begin] + ' **' + selection + '** ' + text[end:]
self.ids.textinput.text = new_text
self.ids.rstdocument.render()
def add_section_header(self):
self.ids.textinput.insert_text("""\n==============""")
def add_sub_section_header(self):
self.ids.textinput.insert_text("""\n-----------------""")
class MyApp(App):
def build(self):
return root_wgt()
if __name__ == '__main__':
MyApp().run()
答案 0 :(得分:0)
问题是由于当您从右向左选择时selection_to
大于selection_from
所以数据提取会导致重复元素导致这种不当行为,解决方法是{{1} }和begin
正确排序,我们使用sorted:
end