三天之后无法弄清楚如何按下“下一步”按钮,并且会在标签小部件中为sight_word显示列表中的随机单词。任何帮助是极大的赞赏。
class SightWord(BoxLayout):
pass
class SightWordApp(App):
def build(self):
return SightWord()
# Function that selects random sight word from list
def sight_word(self):
word_list = ['What', 'How', 'My', 'Know', 'You']
random_index = randrange(0,len(word_list))
word = word_list[random_index]
print(word)
if __name__ == '__main__':
SightWordApp().run()
Kivy档案:
#:kivy 1.9.1
<SightWord>:
orientation: 'vertical'
padding: root.width * 0.01, root.height * 0.01
BoxLayout:
orientation: 'horizontal'
Label:
id: sight_word
# This is where I want the word to change values
text: 'XXXX'
size_hint: 1, None
font_size: '100sp'
height: self.texture_size[1] + (2 * root.padding[1])
BoxLayout:
orientation: 'horizontal'
Button:
id: next_btn
text: 'Next'
on_press: app.sight_word()
size_hint: 1, None
height: self.texture_size[1] + (4 * root.padding[1])
width: self.texture_size[1] + (4 * root.padding[1])
答案 0 :(得分:0)
这非常简单,您只需找到正确的对象(Label
)并将值传递给其text
属性。我发现您已为该标签制作了id
,并通过app
进行了调用,所以让我们使用它:
<强> .kv 强>
Button:
id: next_btn
text: 'Next'
on_press: app.sight_word(sight_word) # pass id to a function
size_hint: 1, None
height: self.texture_size[1] + (4 * root.padding[1])
width: self.texture_size[1] + (4 * root.padding[1])
<强>的.py 强>
def sight_word(self, label): # use the id/object
word_list = ['What', 'How', 'My', 'Know', 'You']
random_index = randrange(0,len(word_list))
word = word_list[random_index]
label.text = word # change text property to your string