2015-10-16 27 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)