【问题标题】:Binding a function with togglebutton in Kivy在 Kivy 中使用切换按钮绑定函数
【发布时间】:2017-09-20 20:08:21
【问题描述】:

我是 Python 和 Kivy 的新手,所以我的问题可能很“简单”,但我找不到解决方案。请帮忙。

我正在尝试在 for 循环中创建几个切换按钮,但在将每个切换按钮与另一个类的函数绑定时遇到问题(通常在我的代码中是:on_state: root.on_state(self.state, self.text)

我尝试使用 lambda 函数来做到这一点。我没有错误,但是当按下切换按钮时(状态 =“向下”)没有任何反应。

如果有人能告诉我问题出在哪里,我将不胜感激。 谢谢!

class MainScreen(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        Window.size = (1400, 800)
        self.ingredients_sites = []
        self.count = 0

    def display_text(self, count):
        # displaying result in TextInput from ingredient_sites
        result = zielone_koktajle.result(self.ingredients_sites, count)
        if result:
            self.display.text = ", ".join(result)
        else:
            self.display.text = ""
            self.display.hint_text = "brak wspolnych stron.."

    def on_state(self, state, ingredient_name):
        if state == "down":
            # dd sites to the list "ingredient_sites" by extracting value of the key from dictionary
            self.ingredients_sites.extend(zielone_koktajle.get_ing_from_csv().get(ingredient_name))
            self.count += 1
            self.display_text(self.count)

        if state == "normal":
            for i in (zielone_koktajle.get_ing_from_csv().get(ingredient_name)):
                if i in self.ingredients_sites:
                    self.ingredients_sites.remove(i)
            self.count -= 1
            self.display_text(self.count)


class ToggleButtons(StackLayout):
    lista = ["aloes", "blonnik", "herbata biala", "herbata czerwona", "herbata zielona", "mleko kokosowe", "mleko migdalowe", "mleko owsiane", "mleko ryzowe", "mleko sojowe", "woda kokosowa"]
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_togglebuttons()

    def add_togglebuttons(self):
        for i in ToggleButtons.lista:
            tgbtn = ToggleButton(id = "togglebtn",
                                 text = i,
                                 size_hint = (.1, .30)
                                 )

            tgbtn.bind(on_state = lambda x: MainScreen.on_state(self, tgbtn.state, i))

            self.add_widget(tgbtn)


class ZieloneKoktajleApp(App):
     def build(self):
        self.title = "Zielone koktajle - index"
        return MainScreen()

以及我的 KV 文件的简短示例:

<MainScreen>:
    orientation: "vertical"
    id_togglebtn: togglebtn

    BoxLayout:
        id: togglebtn
        orientation: "vertical"

        Label:
            text: "Zielone koktajle"

    BoxLayout:
        id: id_togglebuttons
        orientation: "vertical"
        spacing: 10

        CustLabel:
            text: "WARZYWA"
        StackLayout:
            id: warzywa
            CustWidget:
                text: "awokado"
                on_state: root.on_state(self.state, self.text)

        CustLabel:
            text: "NAPOJE"
        StackLayout:
            id: napoje
            ToggleButtons:

【问题讨论】:

    标签: python for-loop button kivy togglebutton


    【解决方案1】:

    你可以在.kv中绑定它。
    试试这个:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.stacklayout import StackLayout
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.togglebutton import ToggleButton
    
    
    Builder.load_string('''
    
    <ToggleButton>:
        on_state: app.root.on_state(self)
    
    <MainScreen>:
    
        BoxLayout:
    
            ToggleButtons:
    
    
    
    ''')
    
    class MainScreen(BoxLayout):
    
        def on_state(self, togglebutton):
            tb = togglebutton
            print(tb,tb.state,tb.text)
    
    
    class ToggleButtons(StackLayout):
        lista = ["aloes", "blonnik", "herbata biala", "herbata czerwona", "herbata zielona", "mleko kokosowe", "mleko migdalowe", "mleko owsiane", "mleko ryzowe", "mleko sojowe", "woda kokosowa"]
    
        def __init__(self,**kwargs):
            super(ToggleButtons,self).__init__(**kwargs)
            for i in self.lista:
                tgbtn = ToggleButton(text = i,size_hint = (.1, .30))
    
                self.add_widget(tgbtn)
    
    
    class MyApp(App):
         def build(self):
            return MainScreen()
    
    
    MyApp().run()
    

    【讨论】:

    • @r3nia 你很高兴
    【解决方案2】:

    对于那些想在 python 中执行此操作的人,请使用 state 而不是 on_state 进行绑定。

    【讨论】:

      猜你喜欢
      • 2017-09-26
      • 2018-11-11
      • 2016-02-08
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多