on_text不是^{}事件。要在文本更改时运行回调,可以绑定text属性(textinput的文本存储在其中):from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class LoginScreen(BoxLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.orientation = 'horizontal'
self.mytext = TextInput(text='500', multiline = False)
self.add_widget(self.mytext)
self.mytext.bind(text = self.calc)
def calc(self, instance, text):
print(text)
class MyApp(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
MyApp().run()
您可以使用^{}sintax创建属性更改时自动调用的回调:Kivy Languaje:from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string('''\
:
orientation: "horizontal"
TextInput:
text: "500"
on_text: root.calc(self.text)
''')
class LoginScreen(BoxLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
def calc(self, text):
print(text)
class MyApp(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
MyApp().run()
扩展widget类:from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class My_TextInput(TextInput):
def __init__(self, **kwargs):
super(My_TextInput, self).__init__(**kwargs)
def on_text(self, instance, text):
print(text)
class LoginScreen(BoxLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.mytext = My_TextInput(text='500', multiline = False)
self.add_widget(self.mytext)
class MyApp(App):
def build(self):
return LoginScreen()