with open("file.kv", encoding='utf-8') as f:
Builder.load_string(f.read())
class content(Screen):
pass
class resistor(Screen):
pass
sm = ScreenManager()
sm.add_widget(content())
sm.add_widget(resistor())
def printthecontentsofthetextinput():
do something
class MyApp(App):
def build(self):
return sm
这是我的kv文件。
<resistor>
name:"r"
Image:
source:"lamp1.jpg"
allow_stretch:True
keep_ratio:False
FloatLayout:
size:root.width,root.height
allow_stretch:False
keep_ratio:False
Label:
text:"R"
color:1,1,1,1
pos_hint:{"x":0.03,"y":0.23}
size_hint:0.1,0.05
TextInput:
id:rprim
focus:True
background_color:0.8,0.96,0.88,1
pos_hint:{"x":0.15,"y":0.23}
size_hint:0.1,0.05
Label:
text:"Ω"
color:1,1,1,1
pos_hint:{"x":0.28,"y":0.23}
size_hint:0.05,0.05
我试图访问文本输入,ID为:rprim。
我试过print(resistor().ids.rprim.text),但总是返回空白,即使我在textinput中写了一些东西。
你的例子有一些问题,其中之一是它在示例屏幕上添加了一个空白的屏幕,所以什么都看不到。尝试 访问 TextInput
从你的kv。总之,如果你在你的 kv 中添加一个 on_text
事件,你可以检查输入的内容。
一种方法是首先在你的 app
类。
class MyApp(App):
def build(self):
return sm
def on_text_input(self, text_input):
print(text_input.text)
...然后将回调添加到... TextInput
:
TextInput:
id:rprim
focus:True
background_color:0.8,0.96,0.88,1
pos_hint:{"x":0.15,"y":0.23}
size_hint:0.1,0.05
on_text: app.on_text_input(self)