我是一个新手,想要写一个供我个人使用的应用程序。我使用的是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()我不明白为什么会发生这种事。
发布于 2020-11-18 23:17:39
Distortion Screen显示为空,因为该Screen的kv规则没有定义任何在显示时会很明显的内容。尝试更改:
Label:
id:thoughtdisplay
text: ''至:
Label:
id:thoughtdisplay
text: 'Nothing yet!!'在显示Distortion Screen时,您应该会看到该Label。
https://stackoverflow.com/questions/64894687
复制相似问题