按下按钮时,屏幕管理器设置/获取文本输入



我所要做的就是从TextInput获取和/或设置输入:在GridLayout内部,在SeccondWindow内部。(id:"text_input"(但在谷歌搜索之后。。。我所有的尝试都失败了。

我试过

main_screen = self.manager.get_screen('second')
main_screen.ids.text_input.text = "Something..."

只得到错误";AttributeError:"super"对象没有属性">getattr">

我试过

text = self.root.ids["text_input"].text

但我得到了错误";AttributeError:"SecondWindow"对象没有属性"root">

我试过很多东西。。。我快疯了!!(我很笨,所以请帮帮我!(

这是我的新_window.kv文件

WindowManager:
transition: NoTransition()
FirstWindow:
SecondWindow:

<FirstWindow>:
name: "first"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Button:
text: "Go To Next Screen"
on_release:
app.root.current = "second"

<SecondWindow>:
name: "second"
GridLayout:
id: "Container"
cols: 2
rows: 1
ScrollView:
id: "SideMenuScrollView"
size_hint: ("0.3dp", 1)
do_scroll_y: True
do_scroll_x: False
StackLayout:
id: "SideMenuStack"
size_hint_y: None
height: self.minimum_height
Button:
size_hint: (None, None)
size: ("92dp", "92dp")
Button:
size_hint: (None, None)
size: ("92dp", "92dp")

GridLayout:
size_hint: (1, 1)
id: "MyGrid"
size: (1, 1)
spacing: 10
padding: 10
cols: 1
rows: 2
TextInput:
id: "text_input"
multiline: False
text: ""
size_hint: (1, None)
height: "30dp"

Button:
text: "Do Stuff"
on_release: root.DoStuff()
size_hint: (1, None)
height: "70dp"

这是我的代码

from kivy.app import App
from kivy.uix.screenmana.........
class WindowManager(ScreenManager):
pass

class FirstWindow(Screen):
pass

class SecondWindow(Screen):
def DoStuff(self):
#
# text_input.text = "what ever..."
#

kv = Builder.load_file('new_window.kv')

class AwesomeApp(App):
def build(self):
return kv

if __name__ == "__main__":
AwesomeApp().run()

有人知道如何获取/设置TextInput.text值和DoStuff吗??

主要问题出现在kv文件中:

TextInput:
id: "text_input"

如果用封闭的"定义id,则这些"将成为id的一部分。您可以这样做,但访问id会变得复杂。一个更简单的方法是消除",如下所示:

TextInput:
id: text_input

然后您可以访问TextInput作为:

class SecondWindow(Screen):
def DoStuff(self):
self.ids.text_input.text = 'Abba'

最新更新