2015-10-16 62 views
1

的功能我Kivy玩耍,并有建立一些按钮有:Kivy传递变量在

for i in range(30): 
     self.user = str(i) 
     self.btn = Button(text=self.user, size_hint_y=None, height=40) 
     self.btn.bind(on_press=self.auth(self.user)) 
     self.layout.add_widget(self.btn) 

这些按钮调用验证:

def auth(mytext,instance): 
    print "auth called" 
    popup = Popup(title="success", 
     content=Label(text=mytext), 
     size=(100, 100), 
     size_hint=(0.3, 0.3), 
     auto_dismiss=False) 
    popup.open() 

为什么我没有获得通过自我.user包含字符串的auth函数mytext variabe?

我收到AssertionError: None is not callable

回答

0

实例包含按下按钮的所有内容。

for i in range(30): 
    self.user = str(i) 
    self.btn = Button(text=self.user, size_hint_y=None, height=40) 
    self.btn.bind(on_press=self.auth) 
    self.layout.add_widget(self.btn) 

def auth(self, instance): 
    print "auth called" 
    popup = Popup(title="success", 
     content=Label(content=Label(text='The button <%s> is being pressed' % instance.text), 
     size=(100, 100), 
     size_hint=(0.3, 0.3), 
     auto_dismiss=False) 
    popup.open() 

它也可以给按钮的ID喜欢

self.btn = Button(text="Test User", size_hint_y=None, height=40, id=self.user) 

和并获得ID与instance.id

content=Label(content=Label(text='The button <%s> is being pressed' % instance.id)