亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Kivy 上按下 Button 時如何顯示文本

在 Kivy 上按下 Button 時如何顯示文本

至尊寶的傳說 2023-03-01 17:06:12
我想知道如何使用 Kivy 使我在平臺上創建的按鈕在按下時顯示文本。我希望它在 kivy 窗口中顯示文本,而不是在我的集成終端中。所以我想知道是否有人可以幫助我。我希望它顯示我擁有的 txt 文件中的隨機敢。這是我的代碼: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 hereScrollView:Label:如果有人可以提供幫助,將不勝感激!
查看完整描述

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


查看完整回答
反對 回復 2023-03-01
  • 1 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號