1 回答

TA貢獻1833條經驗 獲得超4個贊
實際上,我建議您將 ScreenManager 與 kivy 一起使用。這樣您就可以輕松地使用 Properties。
但是對于此解決方案,您可以使用:
from kivy.app import App
from kivy.lang import Builder
kv_string = """
#:import choice random.choice
BoxLayout:
orientation: 'vertical'
Label:
id: mylabel
text: "Truth or Dare?"
TextInput:
id: myinput
hint_text: "Two Things To Try: Truth or Dare"
Button:
text: "Generate"
on_release: root.ids.mylabel.text = choice(app.my_list)
"""
class MyApp(App):
def build(self):
with open('asd.txt', 'r') as mytxt:
self.my_list = mytxt.readlines()
return Builder.load_string(kv_string)
if __name__ == '__main__':
MyApp().run()
添加回答
舉報