简体   繁体   English

在 Kivy 上按下按钮时如何显示文本

[英]How to display text when Button is pressed on Kivy

I was wondering how I could make the button I've created on my platform using Kivy, display text when pressed.我想知道如何使用 Kivy 在我的平台上创建按钮,按下时显示文本。 I want it to display text on the kivy window, not in my integrated terminal.我希望它在 kivy window 上显示文本,而不是在我的集成终端中。 So I was wondering if anyone could help me with that.所以我想知道是否有人可以帮助我。 I want it to display a random dare from my txt file that I have.我希望它从我拥有的 txt 文件中随机显示。

Here is my code:这是我的代码:

Label:
    text: "Truth or Dare?"
TextInput:
    hint_text: "Two Things To Try: Truth or Dare"
Button:
    text: "Generate"
    on_press: #I dont know what to do here
ScrollView:
Label:

If anyone could help it would be greatly appreciated!如果有人可以提供帮助,将不胜感激!

Actually i suggest you to use ScreenManager with kivy.So you can easily use Properties.实际上,我建议您将 ScreenManager 与 kivy 一起使用。因此您可以轻松使用属性。

But for this solution you can use that:但是对于这个解决方案,您可以使用它:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM