python - 为什么即使列出了类名,kivy 也不处理我的 kv 文件中的规则?
问题描述
我是一个 kivy 新手,正在尝试编写一个供我个人使用的应用程序。我正在使用ScreenManager。第一个屏幕 ( ThoughtsClass
) 的类规则从 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!!'
Label
当显示 时,您应该会看到Distortion
Screen
。
推荐阅读
- javascript - Is there a better way to write this nested map in JavaScript?
- c++ - How to iterate through entire Windows registry and delete any keys with the name/value of a string/char/data?
- ios - Swift I can't manage to hide segmented Control
- angular - Stick to bottom when scrolling down and stick to top when scrolling up
- mysql - 我如何循环两个列值
- selenium - Selenium-如何验证元素是否具有某些样式
- vpython - 使用 Vpython 创建表
- android - 无法在 android 的 kotlin 中的活动之间传递数据
- java - 无法解析我的 Maven 项目的依赖项
- ansible - 如何能够使用 ansible 对所有用户执行 pm2 命令?