繁体   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