2016-10-19 124 views
0

我想綁定一個python類(從BoxLayout小部件inheres)的屬性,以及該類的kv配置文件。Kivy Python - 傳遞參數並綁定kv和python文件

我想要做的是將一些值傳遞給類,並在kv文件TextInput id(這是該類的kv描述)旁邊打印該值。當我跑我的代碼時,我得到一個錯誤:

TypeError: addBoxClass() missing 1 required positional argument: 'someNumber' 

我認爲我過於複雜的事情,因爲這應該是相當簡單的。 我的一些代碼位,我希望它是明確的:

#at some point in my main code I call SomeClass  
box=SomeClass() 
box.add_widget(SomeClass.addBoxClass(someNumber)) #someNumber is a value that I want to print in def printInput, next to the kv file's TextInput id. 

#then this is the class 
class SomeClass(BoxLayout): 
def __init__(self, **kwargs): 
    super(NumOfRoomsClass, self).__init__(**kwargs) 
    self.number = None 

def addBoxClass(self,someNumber):   
    self.number = someNumber #This is where I get my error 
    #create and rern a label 
    boxLabel = BoxLayout()   
    Lab = Label(text= self.number) 
    boxLabel.add_widget(Lab) 
    return (boxLabel) 

def printInput(self, text): 
    input= int(text) 
    print ("self.number = ", self.number , "input = ",input) #This is what I'm aftre 

,並在我的.kv文件I`ve得到這個:

<SomeClass>: 
    Label: 
     text: "Number of rooms" 

    TextInput: 
     multiline: False 
     id: inputId 
     on_text_validate: root.printInput(inputId.text) 

回答

0

當我看着你的代碼,我沒有看到你在哪裏調用了addBoxClass()函數。

您創建了SomeClass的一個實例。其中:

self.number = None 

如果您驗證了您的textInput,它將調用printInput。 addBoxClass()不被調用。

0

看起來像someNumber,你試圖從你的python文件調用尚未定義。

#at some point in my main code I call SomeClass  
box=SomeClass() 
box.add_widget(SomeClass.addBoxClass(someNumber)) #someNumber is a value that I want to print in def printInput, next to the kv file's TextInput id. 

應該

#at some point in my main code I call SomeClass 
someNumber = 1 
box=SomeClass() 
box.add_widget(SomeClass.addBoxClass(someNumber)) #someNumber is a value that I want to print in def printInput, next to the kv file's TextInput id. 

someNumber必須給出一個值,你用它