Python、Kivy 中的多个按钮循环

问题描述 投票:0回答:2

我在 Python/Kivy 中通过 for 循环创建了多个按钮。我无法弄清楚如何为每个按钮实现 on_press 和 on_release 函数,因此每个按钮(具有定义的颜色)在释放后都会恢复到其原始颜色。

下面您将找到Python和kivy的部分代码。

Python:

class PrButton(GridLayout):

def __init__(self, **kwargs):
    super(PrButton, self).__init__(**kwargs)
    self.build_grid()

def build_grid(self):
    for i in hi_cat():
        btn = ButtonDD(text=i[0].upper())
        btn.background_color = i[1]
        self.ids[i[0]] = btn
        self.add_widget(btn)

kivy 文件:

<ButtonDD>
bold: True
python loops button kivy
2个回答
0
投票
from kivymd.app import MDApp 
from kivy.lang import Builder 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.button import Button 


KV = """
<MainWidget>:
    size : root.size 
    
 
"""


class MainWidget(BoxLayout):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        
        for i in range(5):
            
            btn = Button(text = str(i))
            btn.bind(on_release = lambda i=i:self.clicked(i))
            self.add_widget(btn)
            
    
    def clicked(self,text):
        print(text)

class MainApp(MDApp):
    def build(self):
        
        Builder.load_string(KV)
    
        return MainWidget()

MainApp().run()

通过将

lambda x: self.clicked()
更改为
lambda i=i:self.clicked()

来实现

我被此类问题困扰已经有一段时间了。 但我最近得到了答案。所以我希望这对你有帮助


0
投票

您可以继承按钮对象并将其作为参数传递,并将其存储在变量中以供使用或稍后在按下时添加它,您可以使用 on_press 或 on_release (如果是 kivymd),回到 kivy,我们可以使用:

from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.button import Button
from kivy.properties import BooleanProperty, ListProperty, NumericProperty, ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout

material_app="""
Screen:
    BoxLayout:
        id: whiteboard
        orientation: "vertical"
        Label:
            text: "The buttons will appear after you press the button:"
            pos_hint: {'x': 0.0, 'y': 0.5}
        Button:
            text: "Press me to see 5 more buttons"
            size: (275,50)
            size_hint: None,None
            pos_hint: {'x': 0.35, 'y': 0.9}
            on_press: app.access_to_the_class().build_grid(root.ids.whiteboard)
<ButtonDD@Label>
    bold: True
"""

class ButtonDD(Button):
    text=StringProperty()
    background_color=ListProperty()
    pass

def hi_cat():
    return "Hi cat! *(255, 100, 0, 0.5)@Hi cat! *(215, 0, 255, 0.5)@Hi cat! *(185, 0, 0, 0.5)@Hi cat! *(150, 0, 0, 0.5)@Hi cat! *(100, 0, 0, 0.5)".split("@")

class PrButton(GridLayout):
    def __init__(self, **kwargs):
        super(PrButton, self).__init__(**kwargs)
        #self.build_grid() #we will use this but in a different trigger

    def build_grid(self,the_body_of_the_app):
        for i in hi_cat():
            j=i.split("*")
            btn = ButtonDD(text=j[0].upper()) #add the text
            
            btn.background_color = eval(str(j[1])) #add the second value in text or color
            the_body_of_the_app.add_widget(btn)

class MainProgramjbsidis(App):
    def access_to_the_class(self):
        the_class=PrButton()
        return the_class
    def build(self):
        return Builder.load_string(material_app)

MainProgramjbsidis().run()

结果是: 按下按钮之前: 在此输入图片描述

按下按钮后: 在此输入图片描述

© www.soinside.com 2019 - 2024. All rights reserved.