为什么 kivy 不处理我的 kv 文件中的规则,即使列出了 class 名称?

Why is kivy not processing a rule in my kv file even though the class name is listed?

我是一个 kivy 新手,正在尝试编写一个供我个人使用的应用程序。我正在使用 ScreenManager。第一个屏幕 (ThoughtsClass) 的 class 规则从 kv 文件中正确读取并执行。但是当涉及到第二个屏幕 (Distortion) 时,Kivy 没有阅读规则,我只得到一个空白屏幕。如果我在一个小部件上手动调用 add_widget,该小部件就会出现。但是没有从 kv 文件中处理任何内容。

这是我的 kv 文件:

Root:
    ThoughtsClass:
    Distortion:


<ThoughtsClass>:
    cols: 1
    thought: thought
    id: thoughtclass

    TextInput:
        id: thought
        multiline: False

    BoxLayout:
        size_hint_y: 0.25
        orientation: 'horizontal'

        Button:
            text: 'Next Thought'
            on_press: thoughtclass.nextthought()

        Button:
            text: 'Done'
            on_press: thoughtclass.donethought()

<Distortion>:
    cols: 1
    disttext: disttext
    thoughtdisplay: thoughtdisplay

    Label:
        id:thoughtdisplay
        text: ''

    BoxLayout:
        orientation: 'horizontal'
        SelectDist:
            id: disttext

这里是主要的 Python 代码:

#!/usr/bin/env python3

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.properties import StringProperty, ObjectProperty, DictProperty
from kivy.storage.dictstore import DictStore
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen

class Root(ScreenManager):

    def __init(self, **kwargs):
        super(Root, self).__init__(self,**kwargs)


class ThoughtsClass(Screen):
    thought = ObjectProperty()
    negstatements = DictProperty()
    distmeanings = DictProperty()

    def __init__(self, **kwargs):
       super(ThoughtsClass, self).__init__(**kwargs)
       self.distmeanings = distortions
       self.name = 'Thoughts'


    def nextthought(self):
        print ('Thought: ' + self.thought.text)
        if self.thought.text:
            self.negstatements[self.thought.text] = {'distortion':'', 'rational':''}
        self.thought.text = ''

    def donethought(self):
        print ('Done pressed')
        if self.thought.text not in self.negstatements:
            self.nextthought()
        root.current = 'Distortions'
        root.current_screen.thoughts = self.negstatements


class Distortion(Screen):
    disttext = ObjectProperty()
    thoughts = DictProperty()
    thoughtdisplay = ObjectProperty()

    def __init__(self, **kwargs):
        super(Distortion, self).__init__(**kwargs)
        self.name = 'Distortions'

    def display_thought(self):
        for thoughttext in self.thoughts:
            self.thoughtdisplay.text = thoughttext
        dist_dd = SelectDist()
        dist_dd.build_dd()
        distbutton = Button(text='Choose a distortion:')
        distbutton.bind(on_release=dist_dd.open)
        dist_dd.bind(on_select=self.choose_dist())

    def choose_dist(self, instance, value):
        print ("Chose " + value)

class SelectDist(DropDown):

    def __init__(self, **kwargs):
        super(SelectDist, self).__init__(**kwargs)

    def build_dd(self):
        for diststring in distortions:
            btn = Button(text = diststring)
            btn.bind(on_release=lambda btn: self.select(btn.text))
            self.add_widget(btn)


class ThreeColumnApp(App):

    def build(self):
        global root
        root = Root()
        root.add_widget(ThoughtsClass())
        root.add_widget(Distortion())
        return root

if __name__ == '__main__':
    distortions = {}
    cbtinstance = ThreeColumnApp()
    cbtinstance.run()

我不明白为什么会这样...

Distortion Screen 显示为空白,因为您的 kv 规则 Screen 没有定义显示时显而易见的任何内容。尝试更改:

Label:
    id:thoughtdisplay
    text: ''

至:

Label:
    id:thoughtdisplay
    text: 'Nothing yet!!'

当显示 Distortion Screen 时,您应该会看到 Label