Python 如何在Kivy中通过按钮从另一个页面清除文本输入?
我的问题可能主要是因为缺乏技能,但我找不到任何类似的职位。所以我在主屏幕上有文本输入。我需要在第二个屏幕上有一个按钮来清除这些文本输入 我不知道如何调用clear_inputs方法并将textinput作为参数传递。我想用这个clear_inputs方法我可以清空那些文本字段,但是如何将它绑定到另一个页面中的按钮呢 皮耶 千伏Python 如何在Kivy中通过按钮从另一个页面清除文本输入?,python,python-3.x,kivy,kivy-language,Python,Python 3.x,Kivy,Kivy Language,我的问题可能主要是因为缺乏技能,但我找不到任何类似的职位。所以我在主屏幕上有文本输入。我需要在第二个屏幕上有一个按钮来清除这些文本输入 我不知道如何调用clear_inputs方法并将textinput作为参数传递。我想用这个clear_inputs方法我可以清空那些文本字段,但是如何将它绑定到另一个页面中的按钮呢 皮耶 千伏 屏幕管理: 主屏幕: 姓名:'Main' 第二屏: 姓名:'Page2' : 姓名:'Main' 盒子布局: 方向:'垂直' 网格布局: 科尔斯:2 标签: 文本:'t
屏幕管理:
主屏幕:
姓名:'Main'
第二屏:
姓名:'Page2'
:
姓名:'Main'
盒子布局:
方向:'垂直'
网格布局:
科尔斯:2
标签:
文本:'testfield1'
文本输入:
id:textfield1
标签:
文本:'testfield2'
文本输入:
id:textfield2
按钮:
正文:“下一页”
发布时:app.root.current='Page2'
:
姓名:'Page2'
按钮:
文本:'清除文本字段'
发布时:
如果我理解正确,您要做的是使用第X页(Main
?)中的按钮更改第Y页(Page2
?)中的文本。我不是Kivy的专家,所以可能有更好的方法,但这里有一些想法:
1) 我试着给所有屏幕赋予class属性parent
,结果证明这是个坏主意,因为Kivy已经使用了这个名称。你可以简单地把它改成parent\uuu
或者其他什么,然后自己试试看。您需要的是在创建时将“parent”作为参数传递给\uuuu init\uuuu
:
class ScreenManagement(ScreenManager):
def __init__(self, children_, **kwargs):
# you might need to save the children too
self.children_ = children_
def add_child(self, child):
# maybe as dict
self.children_[child.name] = child
class SecondScreen(Screen):
def __init__(self, parent_, **kwargs):
super().__init__(**kwargs)
# maybe the screen manager or the main app?
self.parent_ = parent_
self.name_ = "Second"
....
def clear_inputs(self, text_inputs):
....
class MainScreen(Screen):
def __init__(self, parent_, **kwargs):
super().__init__(**kwargs)
# maybe the screen manager or the main app?
self.parent_ = parent_
# you may want to
....
# Get the appropriate screen from the parent
self.parent_.children_["Second"].clear_inputs(...)
2) 我也从另一个角度看问题。不要直接运行应用程序,而是将其分配给变量并引用该变量。对于高级用例,这可能仍然需要篡改:
# Use the global variable within your classes/methods
class Whatever:
def whatever2(self, params):
app.something()
....
app = testiApp()
app.run()
要在另一个屏幕中清除
TextInput
的文本,需要以下增强功能(kv文件和Python脚本)
kv文件
- 为了访问
小部件,向实例化对象TextInput
添加一个GridLayout:
id:container
- 默认情况下,每个屏幕都有一个属性
,该属性为您提供所使用的manager
- 将发布时的
事件绑定到方法,
,不带任何参数清除输入()
嗨,再次感谢。这似乎有效。顺便问一下,如果在同一页面中有其他的布局,而文本字段也需要清空,您将如何使其工作?我尝试了一些不值得一提的测试,但无法正常工作。您指的是
main屏幕中的两个或多个容器,其中TextInput的文本需要清晰?Hi-Ikolim。是的,我指的是那个。你会怎么做?假设容器的id
是box1
和box2
,为每个容器添加for
循环。e、 g.用于反向(main.ids.box1.children)中的儿童:
谢谢!这将需要一些时间让我的头脑像编码器一样思考:P这么简单的解决方案!
class ScreenManagement(ScreenManager):
def __init__(self, children_, **kwargs):
# you might need to save the children too
self.children_ = children_
def add_child(self, child):
# maybe as dict
self.children_[child.name] = child
class SecondScreen(Screen):
def __init__(self, parent_, **kwargs):
super().__init__(**kwargs)
# maybe the screen manager or the main app?
self.parent_ = parent_
self.name_ = "Second"
....
def clear_inputs(self, text_inputs):
....
class MainScreen(Screen):
def __init__(self, parent_, **kwargs):
super().__init__(**kwargs)
# maybe the screen manager or the main app?
self.parent_ = parent_
# you may want to
....
# Get the appropriate screen from the parent
self.parent_.children_["Second"].clear_inputs(...)
# Use the global variable within your classes/methods
class Whatever:
def whatever2(self, params):
app.something()
....
app = testiApp()
app.run()
<MainScreen>:
name:'Main'
BoxLayout:
orientation:'vertical'
GridLayout:
id: container
...
Button:
text:'Next Page'
on_release: root.manager.current ='Page2'
<SecondScreen>:
name:'Page2'
Button:
text:'Clear textfields'
on_release: root.clear_inputs()
from kivy.uix.textinput import TextInput
...
class SecondScreen(Screen):
def clear_inputs(self):
main = self.manager.get_screen('Main')
for child in reversed(main.ids.container.children):
if isinstance(child, TextInput):
child.text = ''