簡體   English   中英

有沒有辦法在獼猴桃中生成多個按鈕?

[英]Is there a way to generate multiple buttons in kivy?

我正在嘗試制作一個簡單的烹飪應用程序,以顯示逐步食譜指南如何制作菜餚。 我在為每個配方創建按鈕時遇到問題。

我不熟悉如何在kivy文件中進行循環,因此我嘗試在“配方屏幕”打開時從kivy文件中調用函數。 問題在於它僅使用kivy文件中的代碼,而不是在python文件中的函數中生成小部件。

Python部分:

def build(self):
    self.ins = GridLayout()
    self.ins.cols = 3

    self.ins.add_widget(Label(text="Test"))

獼猴桃部分:

<BakeRecipes>:
    name: 'bkrecipes'
    on_parent: root.build()

我希望看到標簽,但是我只有一個黑屏,所以我假設它不使用他的python零件作為設計的一部分。 有沒有一種方法可以使用循環在kivy文件中生成按鈕? 我是否需要在代碼中添加一些內容,以便程序理解,以便與kivy代碼一起生成python部件代碼?

是的,您可以使用Python代碼生成按鈕,然后將其添加到小部件中

py文件

class BakeRecipes():
    def on_pre_enter(self):
        for i in range(0,5): #assuming you need to create 5 buttons
            button = Button(text=str(i)) #the text on the button
            button.bind(on_press=self.pressed) #when the button is clicked
            self.ids.grid.add_widget(button) #added to the grid

KV文件

<BakeRecipes>:
    name: 'bkrecipes'
    GridLayout:
        cols:3
        id:grid

如果使用的是ScreenScreenManager則將class BakeRecipes(): class BakeRecipes(Screen):

另外,您需要為上面創建的buttons編寫pressed功能。 單個功能將綁定到所有按鈕。

希望這對您有所幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM