【问题标题】:How to use the RST module in Kivy?如何在 Kivy 中使用 RST 模块?
【发布时间】:2018-02-09 03:27:00
【问题描述】:

我正在 Kivy 中创建一个健身/营养应用程序。问题是大多数屏幕都包含供观众阅读的文本,我不希望文本只是像 .txt 文件那样的纯旧文本。我试着寻找一些东西,我发现有一个 RST 渲染模块可以让我的文本看起来不错,但是在尝试了几天之后,我似乎无法让它与我的代码一起使用。此外,我想将我将与 RST 一起使用的文本放入一个单独的文件中,这样我就可以保持我的代码干净,我该如何做到这一点?

Python 代码(main.py):

import kivy
kivy.require('1.9.0')

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ListProperty, StringProperty, OptionProperty, VariableListProperty
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.graphics import *
from kivy.base import runTouchApp
from kivy.uix.label import Label



class MainScreen(Screen):
    pass

class NutritionScreen(Screen):
    pass

class FitnessScreen(Screen):
    pass

class CalorcalcScreen(Screen):
    pass

class BigsixScreen(Screen):
    pass

class ProteinScreen(Screen):
    pass

class CarbScreen(Screen):
    pass

class FatScreen(Screen):
    pass

class VitaminScreen(Screen):
    pass

class MineralScreen(Screen):
    pass

class WaterScreen(Screen):
    pass

class SuppleScreen(Screen):
    pass

class DietScreen(Screen):
    pass

class ExerciseScreen(Screen):
    pass

class WorkoutplanScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass


presentation = Builder.load_file("nutrifit.kv")

class NutrifitApp(App):

    def build(self):
        return presentation

nfApp = NutrifitApp()
nfApp.run()

Kivy 代码(nutrifit.kv):

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:

    transition: FadeTransition()
    MainScreen:
    NutritionScreen:
    FitnessScreen:
    CalorcalcScreen:
    BigsixScreen:
    SuppleScreen:
    DietScreen:
    ExerciseScreen:
    WorkoutplanScreen:
    ProteinScreen:
    CarbScreen:
    FatScreen:
    VitaminScreen:
    MineralScreen:
    WaterScreen:



<MainScreen>:
    name: 'main'
    GridLayout:
        cols: 1
        rows: 3
        spacing: 1
        padding: 1

        Button:
            text: "Nutrition"
            font_size: 25
            on_release: app.root.current = "nutrition"

        Button:
            text: "Fitness"
            font_size: 25
            on_release: app.root.current = "fitness"

################################################################################           

<NutritionScreen>:
    name: 'nutrition'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Home'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "main"

        Label:
            text: 'Nutrition'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"center": 1, "top": 1}


    BoxLayout:
        orientation: 'vertical'
        size_hint: 1, .9

        Button:
            text: "Caloric Calculator"
            font_size: 25
            on_release: app.root.current = "calorcalc"

        Button:
            text: "The Big Six"
            font_size: 25
            on_release: app.root.current = "bigsix"

        Button:
            text: "Supplementation"
            font_size: 25
            on_release: app.root.current = "supple"

        Button:
            text: "Diets"
            font_size: 25
            on_release: app.root.current = "diet"


<CalorcalcScreen>:
    name: 'calorcalc'
<BackBar@ButtonBehavior+BoxLayout>:
    orientation: 'horizontal'
    bgcolor: [1, 0, 0, 1]
    on_press: self.bgcolor = [1, 0, 0, .5]
    on_release: self.bgcolor = [1, 0, 0, 1]
    canvas:
        Color:
            rgba: root.bgcolor
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text: '<--'
        size_hint_x: None
        width: root.height
    Label:
        text: 'Current name'
        text_size: self.size
        halign: 'left'
        valign: 'middle'

        RstDocument:
            text: root.text


<BigsixScreen>:
    name: 'bigsix'
    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "nutrition"



    GridLayout:
        cols: 2
        rows: 3
        size_hint: 1, .9
        Button:
            text: 'Protein'
            on_release: app.root.current = 'protein'
        Button:
            text: 'Carbohydrates'
            on_release: app.root.current = 'carb'
        Button:
            text: 'Fats'
            on_release: app.root.current = 'fat'
        Button:
            text: 'Vitamins'
            on_release: app.root.current = 'vitamin'
        Button:
            text: 'Minerals'
            on_release: app.root.current = 'mineral'
        Button:
            text: 'Water'
            on_release: app.root.current = 'water'


<ProteinScreen>:
    name: 'protein'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "bigsix"

        Label:
            text: 'Protein'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}

    FloatLayout:
        Label:
            text: 'this is protein'
            pos_hint: {"center_x": .5, "center_y": .5}

<CarbScreen>:
    name: 'carb'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "bigsix"

        Label:
            text: 'Carbohydrates'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}

    FloatLayout:
        Label:
            text: 'this is carbs'
            pos_hint: {"center_x": .5, "center_y": .5}

<FatScreen>:
    name: 'fat'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "bigsix"

        Label:
            text: 'Fats'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}

    FloatLayout:
        Label:
            text: 'this is fats'
            pos_hint: {"center_x": .5, "center_y": .5}

<MineralScreen>:
    name: 'mineral'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "bigsix"

        Label:
            text: 'Minerals'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}

    FloatLayout:
        Label:
            text: 'this is minerals'
            pos_hint: {"center_x": .5, "center_y": .5}

<VitaminScreen>:
    name: 'vitamin'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "bigsix"

        Label:
            text: 'Vitamins'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}

    FloatLayout:
        Label:
            text: 'this is vitamins'
            pos_hint: {"center_x": .5, "center_y": .5}

<WaterScreen>:
    name: 'water'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "bigsix"

        Label:
            text: 'Water'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}

    FloatLayout:
        Label:
            text: 'this is water'
            pos_hint: {"center_x": .5, "center_y": .5}





<SuppleScreen>:
    name: 'supple'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "nutrition"

        Label:
            text: 'Supplementation'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}


    BoxLayout:
        orientation: 'vertical'
        size_hint: 1, .9

<DietScreen>:
    name: 'diet'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "nutrition"

        Label:
            text: 'Diets'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}


    BoxLayout:
        orientation: 'vertical'
        size_hint: 1, .9




################################################################################


<FitnessScreen>:
    name: 'fitness'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Home'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "main"

        Label:
            text: 'Fitness'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}


    BoxLayout:
        orientation: 'vertical'
        size_hint: 1, .9

        Button:
            text: "Exercises"
            font_size: 25
            on_release: app.root.current = "exercise"

        Button:
            text: "The Big Six"
            font_size: 25
            on_release: app.root.current = "workoutplan"



<WorkoutplanScreen>:
    name: 'workoutplan'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "fitness"

        Label:
            text: 'Workout Plans'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}


    BoxLayout:
        orientation: 'vertical'
        size_hint: 1, .9




<ExerciseScreen>:
    name: 'exercise'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "fitness"

        Label:
            text: 'Exercises'
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}


    BoxLayout:
        orientation: 'vertical'
        size_hint: 1, .9

如果我将其设置为垂直:

第一个例子

这是如果我输入参数:

size_hint: .5, .1
pos_hint: {"x": 0, "top": 1}

第二个例子

【问题讨论】:

  • 不确定我在这里看到了 RST 模块的任何实现。
  • 上面提供的代码没有上面 RST 模块的任何实现,因为我想提供我拥有的“最纯粹”的代码。这意味着它按照我的意图工作。我还认为有人回答会更容易。
  • 您期望得到什么样的回应?它的代码没有显示任何关于 .RST 的内容,它的代码可以用一个 hello world 替换它,它等效于相同的内容。
  • 我尝试在我的 kivy 文件中使用上面的 RST 代码,但它一直给我错误,我编辑了上面的帖子以显示。

标签: python kivy kivy-language


【解决方案1】:

如何在 Kivy 中使用 RST 文档?

参考:reStructuredText renderer

从输入文件中读取文本:

1。创建一个输入文件 - inFile.txt

使用以下文本创建一个名为“inFile.txt”的文件:

.. _top:

Hello world
===========

This is an **emphased text**, some ``interpreted text``.
And this is a reference to top_::

    $ print("Hello world")

2。编辑类 CalorcalcScreen

在您的课程 CalorcalcScreen(Screen) 中将“通过”替换为以下代码:

class CalorcalcScreen(Screen):
    text = ""
    with open("inFile.txt") as fobj:
        for line in fobj:
            text += line

3。编辑 kv 规则文件 - nutrifit.kv

处,将“Label”替换为“RstDocument”并删除“document = RstDocument...”

<CalorcalcScreen>:
    name: 'calorcalc'
    BoxLayout:
        orientation: 'horizontal'
        spacing: 1
        padding: 1
        Button:
            text: 'Back'
            size_hint: .25, .1
            pos_hint: {"x": 0, "top": 1}
            on_release: app.root.current = "nutrition"

        Label:
            text: ''
            size_hint_y: .1
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 1}


    BoxLayout:
        orientation: 'vertical'
        size_hint: 1, .9

        RstDocument:
            text: root.text


<BigsixScreen>:

新的导航栏

编辑 kv 规则文件

请更新您的 kv 规则文件如下:

<BackBar@ButtonBehavior+BoxLayout>:
    orientation: 'horizontal'
    bgcolor: [1, 0, 0, 1]
    on_press: self.bgcolor = [1, 0, 0, .5]
    on_release: self.bgcolor = [1, 0, 0, 1]
    canvas:
        Color:
            rgba: root.bgcolor
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text: '<--'
        size_hint: .25, .1
        pos_hint: {"x": 0, "top": 1}
    Label:
        text: 'Current name'
        text_size: self.size
        halign: 'left'
        valign: 'middle'
        size_hint: 1, .1
        pos_hint: {"right": 1, "top": 1}

<CalorcalcScreen>:
    name: 'calorcalc'
    BackBar:
    BoxLayout:
        orientation: 'vertical'
        size_hint: 1, .9

        RstDocument:
            text: root.text

<BigsixScreen>:

输出

【讨论】:

  • 非常感谢!好的,我想在 的顶部创建一个导航栏,并尝试将其插入到我的 .kv 文件中,但是一旦我运行它,它只是一个空白屏幕,没有与之相关的错误。我编辑了顶部以显示我尝试过的内容。
  • 我已经更新了我的 kv 规则文件解决方案,显示了旧的导航返回栏,还显示了你的新导航返回栏。您可能需要添加 BoxLayout 来分隔导航返回栏和 RstDocument。
  • 有没有办法将backbar与RstDocument分开?因为玩弄它,我已经看到您提供的解决方案是使用我的 backbar 和 Rstdoc,并在设置为垂直的同一个 BoxLayout 中使用它们。
  • 我编辑了帖子以显示我的意思的屏幕截图。
  • 我为模板 BackBar 添加了大小提示和位置提示,还为 RstDocument 添加了 BoxLayout。新导航栏请参考更新后的 kv 规则文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
相关资源
最近更新 更多