2016-04-21 111 views
0

我正在爲一個學校項目的幻想足球類型的應用程序。 我們創建了一個滾動視圖,其中包含一個團隊中的角色列表,每個角色都分配給一個按鈕。在按下按鈕時,顯示顯示第二個「不活動的字符按鈕」列表的新的滾動視圖,允許用戶按下一個以將團隊中的第一個和第二個角色交換。kivy按鈕ID的功能

我們的問題來自於難以管理'定位'按下哪個按鈕,以便告訴我們的交換功能哪兩個字符在列表中交換。在按下所述按鈕時是否可以保留按鈕的ID並將其調用爲新功能?

我們的代碼是有點亂,但波紋管顯示:

class SMApp(App): 
    teamlist = [] 
    idvar = "" 
    btnlist = [] 

    def popupfunc(self, event): 

     """ 
     creates a popup asking if the user wishes to swap a character from team to subs 
     then proceeds to allow user to choose who swaps 
     """ 

     def subscroll(self): 
      """ 
      opens scroll list of substitute characters in a popup 
      """ 
      sublist = [] 
      curs.execute('SELECT * FROM Subs') 
      for row in curs: 
       sublist.append([row[0], row[2]]) 

      layout = GridLayout(cols=2, spacing=10, size_hint_y=None) 
      layout.bind(minimum_height=layout.setter('height')) 
      for i in range(len(sublist)): 
       btn = Button(text=str(sublist[i][0]), size_hint_y=None, height=40) 
       layout.add_widget(btn) 
       lbl = Label(text=str(sublist[i][1]), size_hinty=None, height=40) 
       layout.add_widget(lbl) 
      root = ScrollView(size_hint=(None, None), size=(400, 400)) 
      root.add_widget(layout) 
      popup2 = Popup(content=root, size=(7, 10), size_hint=(0.55, 0.8), title="list of subs") 
      popup2.open() 

     box = BoxLayout() 
     btn1 = Button(text='yeah ok') 
     btn2 = Button(text='nope') 
     popup1 = Popup(content=box, size=(10, 10), size_hint=(0.3, 0.3), title="add to team?") 
     btn2.bind(on_press=popup1.dismiss) 
     btn1.bind(on_press=subscroll) 
     box.add_widget(btn1) 
     box.add_widget(btn2) 

     popup1.open() 


    def build(self): 

     curs.execute('SELECT * FROM Team') 
     for row in curs: 
      self.teamlist.append([row[0], row[2]]) 

     layout = GridLayout(cols=2, spacing=10, size_hint_y=None) 
     layout.bind(minimum_height=layout.setter('height')) 

     for i in range(len(self.teamlist)): 
      btn = Button(text=str(self.teamlist[i][0]), size_hint_y=None, height=40, id=str(i)) 
      btn.bind(on_press=self.popupfunc) 
      self.btnlist.append(btn) 
      layout.add_widget(btn) 
      lbl = Label(text=str(self.teamlist[i][1]), size_hinty=None, height=40) 
      layout.add_widget(lbl) 
     for item in self.btnlist: 
      print item.id 
     root = ScrollView(size_hint=(None, None), size=(400, 400), 
      pos_hint={'center_x':.5, 'center_y':.5}) 
     root.add_widget(layout) 
     return root 

if __name__ == '__main__': 
    SMApp().run() 

回答

0

btn = Button(...)您創建一個不同的對象,因此,你可以告訴被按下。事情是你選擇的方式。

您可以使用:

  • STR(您的按鈕),並獲得特定對象的地址一樣0xAABBCCEE
    • 壞了,不這樣做,
  • 按鈕((?) id ='something',...)
  • 來自kv語言的ID

或創建自己的小部件,具有特定標識符的屬性。然後你會使用父母的孩子一個循環會檢查是否有識別器,並做一些事情:

for child in layout.children: 
    if child.id == 'something': 
     # do something 

而且似乎你需要這個循環您subscroll內,或訪問layout一些其他的方式。