我试图从另一个类(这里是“GetInfoFromAnotherClass”)访问一个类(这里是“UserInput”)的TextInput.text。但是,“检索信息”按钮仅提供初始输入,不进行更新。而在类“UserInput”中,则没有问题-->;按钮“获取信息”。无论我在文本字段中输入什么,“检索信息”按钮总是返回“第一次输入”。我不知道该用谷歌搜索什么了。希望你们能帮帮我
下面是我的问题的一个“接近最小”的例子:
import kivy
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
class Container(GridLayout):
pass
class UserInput(BoxLayout):
first_input = ObjectProperty(None)
second_input = ObjectProperty(None)
def __init__(self,**kwargs):
super().__init__(**kwargs)
def ui_btn(self):
print(self.first_input.text)
print(self.second_input.text)
class GetInfoFromAnotherClass(BoxLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.ui = UserInput()
def retrieve_info(self):
print(self.ui.first_input.text)
print(self.ui.second_input.text)
class MainApp(App):
def build(self):
return Container()
if __name__=='__main__':
MainApp().run()
和主电压(千伏):
#:kivy 1.11.0
# Well this is just for Beauty ;-)
<MyTextInput@TextInput>:
size_hint_y: None
height: 50
multiline: False
write_tab: False
<MyButton@Button>:
size_hint_y: None
height: 50
<Container>:
cols: 1
UserInput
GetInfoFromAnotherClass
<UserInput>:
first_input: first_input
second_input: second_input
size_hint_y: None
height: self.minimum_height
padding: 20
MyTextInput:
id: first_input
text: "First Entry"
MyTextInput:
id: second_input
text: "Second Entry"
MyButton:
text: "Get Info in the same class"
on_press: root.ui_btn()
<GetInfoFromAnotherClass>:
size_hint_y: None
height: self.minimum_height
padding: 20
MyButton:
text: "Retrieve Info from another Class"
on_press: root.retrieve_info()
self.ui = UserInput()
调用创建了一个没有人使用的UserInput
实例访问文本框的一种方法是:
UserInput
实例一个id相关问题 更多 >
编程相关推荐