Kivy - reStructuredText

创建于 2024-12-03 / 25
字体: [默认] [大] [更大]

reStructuredText 是一种文本文件格式,其中包含主要用于 Python 技术文档的数据。该文件通常具有".rst"扩展名。

  • reStructuredText 是 DocUtils 项目的一部分,其主要目的是为 Python 提供一组类似于 Javadoc for Java 的工具。其由 David Goodger 编写,最早版本于 2001 年发布,最新版本于 2019 年发布。

  • reStructuredText 可以被视为一种轻量级标记语言,与 MarkDown 语法有很多相似之处。它被用作 Python 的 Sphinx 文档生成系统的核心组件。

Kivy 以"kivy.uix.rst"模块中定义的 RstDocument 类的形式提供 reStructuredText 文档渲染器。

from kivy.uix.rst import RstDocument
doc = RstDocument(**kwargs)

格式化 reStructuredText

  • 段落 − 用空行分隔的一段文本(一行就够了)。段落必须具有相同的缩进。

  • 粗体 −两个星号之间的字符(例如:**Hello**)

  • 斜体 − 单个星号之间的字符(例如:*world*)

  • 枚举列表 − 以数字或字母开头,后跟句点"."、右括号")"或用括号"( )"括起来。例如 −

1. Python
2. Java
3. C++
  • 项目符号列表 − 以项目符号字符开头 - "-"、"+"或"*"

  • 部分 −这些是一行带有装饰的文本(一个或多个单词):单独的下划线,或下划线和上划线一起,以破折号"-----"表示,等于"======="

  • 图像 − 要在文档中包含图像,请使用图像指令。例如 −

.. image:: kivi-logo.png

示例

以下文本按照 reStructuredText 语法格式化。将以下文本保存为 index.rst −

================
Welcome to Kivy
================
Welcome to Kivy's documentation. **Kivy** is an open source software library for the rapid development of applications equipped with novel user interfaces, such as multi-touch apps.
With Kivy, you can create apps that run on:

* Desktop computers: macOS, Linux, *BSD Unix, Windows.
* iOS devices: iPad, iPhone.
* Android devices: tablets, phones.


-------------------
Virtual environment
-------------------
Create the virtual environment named kivy_venv in your current directory_::

   python -m virtualenv kivy_venv
   
Activate the *virtual environment*.

For Windows default CMD, in the command line do_::
   kivy_venvScriptsactivate
   
Your terminal should now preface the path with something like (kivy_venv), indicating that the kivy_venv environment is active.
If it doesn't say that, the virtual environment is not active and the following won't work.

Install Kivy
------------
The simplest is to install the current stable version of kivy is to use pip_::

   python -m pip install "kivy[base]" kivy_examples

让我们编写一个程序,在 Kivy 应用程序中呈现此 reStructuredText 文档。我们将 RstDocument 小部件放在具有单列的网格布局中。将此对象的源属性设置为我们创建的 RST 文件。

from kivy.app import App
from kivy.uix.rst import RstDocument
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window

Window.size = (720,400)

class rstdemoapp(App):
   def build(self):
      grid=GridLayout(cols=1)
      doc = RstDocument(source="index.rst")
      grid.add_widget(doc)
      return grid
rstdemoapp().run()

输出

运行以上代码。 RST 文档将按照上述格式语法显示。

Kivy 重构文本

0 人点赞过