为什么使用此代码更新textinput值?当我点击按钮时,我需要捕获用户键入的值,但无论看起来我只能获得默认文本。
这是python 3.4.3上的一个可运行的例子:
# -*- coding: utf-8 -*-
import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from functools import partial
class TestApp(App):
def check(self, texto, *args):
print(texto)
def build(self):
layout = FloatLayout()
txtinput = TextInput(background_color=(1,1,1,1),
font_size=16,
size_hint=(None, None),
size=(200, 35),
pos_hint={"center_x":0.5, "bottom":0.8},
multiline=False,
text="write here...",
use_bubble=True)
boton = Button(size_hint=(None, None),
size=(150, 50),
pos_hint={"left":1, "bottom":1},
text="Check",
on_release=partial(self.check, txtinput.text))
layout.add_widget(txtinput)
layout.add_widget(boton)
return layout
if __name__ == "__main__":
TestApp().run()
我喜欢纯Python的答案,我不喜欢使用kv文件。
答案 0 :(得分:0)
在创建check
之后,txtinput
方法与txtinput
绑定到txtinput = TextInput(background_color=(1,1,1,1), ...
txtinput.bind(text=self.check)
:
App
输入值在相关代码段中可用。
目前检查方法是实例方法,我认为它总是接受check
的实例作为第一个属性,txtinput作为第二个,输入文本作为第三个。
要在def check(self, txt_input, texto, *args):
print(texto)
中访问您的文字,方法签名应更新为:{/ p>
'app/bower_components/raven-js/plugins/angular.js'