Kivy - 为 reStructuredText 创建样式按钮
Kivy - Create style buttons for reStructuredText
我想在 Kivy 中为 reStructuredText 小部件创建按钮。这些按钮可以做一些基本的事情,比如粗体、下划线或制作标题,这样用户就不必手动输入标记。例如,用户可以 select 一些文本,然后单击 'bold' 按钮,然后文本将被 [b]...[/b].
包围
我很想展示我尝试过的代码,但老实说我什至不知道从哪里开始。 (或者如果有更好的方法在 Kivy 中实现基本文本编辑,请告诉我。)我目前使用 Kivy 语言通过简单地添加
来显示第一个小部件
RstDocument:
show_errors: True
到 kv 文件(以及保存等...按钮)。
在你的问题中,我是第一次听说 RstDocument
小部件。你让我感兴趣,我想出了一个最小的示例应用程序,这可能是你添加更多内容的一个很好的起点。
这是我的 python 文件
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()
或者,您可以使用也有一些样式选项的标签https://kivy.org/docs/api-kivy.uix.label.html#markup-text实现看起来非常相似。
我想在 Kivy 中为 reStructuredText 小部件创建按钮。这些按钮可以做一些基本的事情,比如粗体、下划线或制作标题,这样用户就不必手动输入标记。例如,用户可以 select 一些文本,然后单击 'bold' 按钮,然后文本将被 [b]...[/b].
包围我很想展示我尝试过的代码,但老实说我什至不知道从哪里开始。 (或者如果有更好的方法在 Kivy 中实现基本文本编辑,请告诉我。)我目前使用 Kivy 语言通过简单地添加
来显示第一个小部件RstDocument:
show_errors: True
到 kv 文件(以及保存等...按钮)。
在你的问题中,我是第一次听说 RstDocument
小部件。你让我感兴趣,我想出了一个最小的示例应用程序,这可能是你添加更多内容的一个很好的起点。
这是我的 python 文件
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()
或者,您可以使用也有一些样式选项的标签https://kivy.org/docs/api-kivy.uix.label.html#markup-text实现看起来非常相似。