我正在为一个学校项目开发一个梦幻足球类应用程序。 我们创建了一个滚动视图,其中包含一个团队中的字符列表,每个字符都分配给一个按钮。按下按钮时,新的滚动视图显示第二个“非活动字符按钮列表”。显示,允许用户按一个将第一个和第二个角色从团队交换到团队。
我们的问题来自于难以设法定位'按下哪个按钮以告诉我们的交换功能在列表中交换哪两个字符。是否可以保留按钮的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 :(得分:0)
您创建的每个btn = Button(...)
都是一个不同的对象,因此您可以分辨出哪个被按下。事情就是你选择的方式。
您可以使用:
或使用特定标识符的属性创建自己的小部件。然后你为父母的孩子使用一个循环来检查识别者并做一些事情:
for child in layout.children:
if child.id == 'something':
# do something
您似乎需要在子列表中使用此循环,或以其他方式访问layout
。