2017-04-24 80 views
0

我是Python和Kivy中的新成員,所以可能我的問題很「容易」,但我找不到解決方案..請幫助。在Kivy中使用togglebutton綁定函數

我試圖創建幾個的ToggleButtons在for循環,和我有從其它類結合使用功能切換按鈕各(通常在我的代碼的問題是:on_state: root.on_state(self.state, self.text)

我試着做這與lambda函數,我沒有errros,但在按下切換按鈕時(狀態=「向下」)沒有任何反應。

如果有誰能夠告訴我問題出在哪裏,我將非常感激。 謝謝!

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: 

回答

0

您可以將其綁定到.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() 
+0

謝謝! :)它的作品 – r3nia

+0

@ r3nia你很好 – EL3PHANTEN