首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在我的kv文件中,即使列出了类名称,kivy也不能处理规则?

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

Stack Overflow用户
提问于 2020-11-18 22:07:28
回答 1查看 33关注 0票数 0

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

这是我的kv文件:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
#!/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()

我不明白为什么会发生这种事。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-18 23:17:39

Distortion Screen显示为空,因为该Screenkv规则没有定义任何在显示时会很明显的内容。尝试更改:

代码语言:javascript
复制
Label:
    id:thoughtdisplay
    text: ''

至:

代码语言:javascript
复制
Label:
    id:thoughtdisplay
    text: 'Nothing yet!!'

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64894687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档