python kivy事件_kivy,如何通过文本改变触发事件

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值