如何将 TextInput 值拉入不同的 class 并在 Kivy 中使用它?
How to pull a TextInput value into a different class and use it in Kivy?
我只想从我的第二个 Class 的文本输入中提取一个数值到我的主页 Class,然后能够在一个函数中处理它。我想我在掌握这里的父子关系和语法时遇到了麻烦。
请帮忙...
Python 文件:
class Home(Screen):
#Generic function
def test(self):
#grab numbers from text input here and be able to use the numeric value using ids
#distance=..? I think normally it's self.root.distance.text
print("distance text input variable here")
pass
class Second(Screen):
pass
class WindowManager(ScreenManager):
pass
class HelpMe(App):
def build(self):
kv = Builder.load_file("help.kv")
self.Home=Home()
return kv
if __name__ == "__main__":
HelpMe().run()
kivy 文件:
WindowManager:
Home:
Second:
<Home>:
name:"Home"
GridLayout:
cols:1
Button:
text:"Go"
on_release:
app.root.current="Second"
root.manager.transition.direction="left"
<Second>:
name:"Second"
distance:distance
GridLayout:
cols:1
TextInput:
id:distance
input_filter:"int"
Button:
text:"Back"
#Initiate test code so I can work with values
on_release:
app.Home.test()
app.root.current="Home"
root.manager.transition.direction="right"
您可以在单击按钮时将它从 kv 语言传递给测试函数:
on_release:
app.Home.test(distance)
app.root.current="Home"
root.manager.transition.direction="right"
然后你在函数中阅读它:
def test(self, dist):
print(dist.text)
请记住,它是字符串形式的。如果要用作数字,则必须将其更改为 int()。
我只想从我的第二个 Class 的文本输入中提取一个数值到我的主页 Class,然后能够在一个函数中处理它。我想我在掌握这里的父子关系和语法时遇到了麻烦。
请帮忙...
Python 文件:
class Home(Screen):
#Generic function
def test(self):
#grab numbers from text input here and be able to use the numeric value using ids
#distance=..? I think normally it's self.root.distance.text
print("distance text input variable here")
pass
class Second(Screen):
pass
class WindowManager(ScreenManager):
pass
class HelpMe(App):
def build(self):
kv = Builder.load_file("help.kv")
self.Home=Home()
return kv
if __name__ == "__main__":
HelpMe().run()
kivy 文件:
WindowManager:
Home:
Second:
<Home>:
name:"Home"
GridLayout:
cols:1
Button:
text:"Go"
on_release:
app.root.current="Second"
root.manager.transition.direction="left"
<Second>:
name:"Second"
distance:distance
GridLayout:
cols:1
TextInput:
id:distance
input_filter:"int"
Button:
text:"Back"
#Initiate test code so I can work with values
on_release:
app.Home.test()
app.root.current="Home"
root.manager.transition.direction="right"
您可以在单击按钮时将它从 kv 语言传递给测试函数:
on_release:
app.Home.test(distance)
app.root.current="Home"
root.manager.transition.direction="right"
然后你在函数中阅读它:
def test(self, dist):
print(dist.text)
请记住,它是字符串形式的。如果要用作数字,则必须将其更改为 int()。