我想綁定一個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)