如何通过 Kivy 中另一个页面的按钮清除文本输入?
How to clear textinputs by button from another page in Kivy?
我的问题可能主要是因为缺乏技能,但我找不到任何类似的帖子。所以我在主屏幕上有文本输入。我需要在第二个屏幕上有一个按钮来清除这些文本输入。
我不知道如何调用 clear_inputs 方法并将 textinput 作为参数传递。我想用这个 clear_inputs 方法我可以清空那些文本字段,但是如何将它绑定到另一个页面中的那个按钮?
Py.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import StringProperty, BooleanProperty
class MainScreen(Screen):
pass
class SecondScreen(Screen):
def clear_inputs(self, text_inputs):
for text_input in text_inputs:
text_input.text = ''
class ScreenManagement(ScreenManager):
def changescreen(self, value):
try:
if value !='main':
self.current = value
except:
print('No Screen named'+ value)
class testiApp(App):
def build(self):
self.title = 'Hello'
testiApp().run()
KV.
ScreenManagement:
MainScreen:
name:'Main'
SecondScreen:
name:'Page2'
<MainScreen>:
name:'Main'
BoxLayout:
orientation:'vertical'
GridLayout:
cols:2
Label:
text:'testfield1'
TextInput:
id: textfield1
Label:
text:'testfield2'
TextInput:
id: textfield2
Button:
text:'Next Page'
on_release: app.root.current ='Page2'
<SecondScreen>:
name:'Page2'
Button:
text:'Clear textfields'
on_release:
如果我没理解错的话,您要做的是使用第 X 页 (Main
?) 中的按钮来更改第 Y 页 (Page2
?) 中的文本。我不是 Kivy 专家,所以可能有更好的方法,但这里有一些想法:
1) 我尝试为所有屏幕提供 class 属性 parent
,结果证明这是个坏主意,因为该名称已被 Kivy 使用。您可以简单地将其更改为 parent_
或其他内容,然后自己尝试一下。您想要的是在创建时将 "parent" 作为参数传递给 __init__
:
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) 我也从youtube tutorial看到了另一种方式。不要直接 运行 您的应用程序,而是将其分配给一个变量并引用该变量。对于高级用例,这可能仍需要篡改:
# Use the global variable within your classes/methods
class Whatever:
def whatever2(self, params):
app.something()
....
app = testiApp()
app.run()
需要以下增强功能(kv 文件和 Python 脚本)才能在另一个屏幕中清除 TextInput
的文本。
kv 文件
- 为了访问
TextInput
小部件,将 id: container
添加到实例化的 object、GridLayout:
- 默认情况下,每个屏幕都有一个 属性
manager
,它为您提供了所用 ScreenManager
的实例。
- 将
on_release
事件绑定到方法,clear_inputs()
不带任何参数
片段 - kv 文件
<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()
Py文件
- 添加导入语句,
from kivy.uix.textinput import TextInput
- 使用
ScreenManager
的get_screen('Main')
函数得到实例化的object,MainScreen
- 使用
for
循环通过ids.container
遍历GridLayout:
的children
- 使用
isinstance()
函数检查 TextInput
小部件
片段 - Py 文件
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 = ''
我的问题可能主要是因为缺乏技能,但我找不到任何类似的帖子。所以我在主屏幕上有文本输入。我需要在第二个屏幕上有一个按钮来清除这些文本输入。
我不知道如何调用 clear_inputs 方法并将 textinput 作为参数传递。我想用这个 clear_inputs 方法我可以清空那些文本字段,但是如何将它绑定到另一个页面中的那个按钮?
Py.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import StringProperty, BooleanProperty
class MainScreen(Screen):
pass
class SecondScreen(Screen):
def clear_inputs(self, text_inputs):
for text_input in text_inputs:
text_input.text = ''
class ScreenManagement(ScreenManager):
def changescreen(self, value):
try:
if value !='main':
self.current = value
except:
print('No Screen named'+ value)
class testiApp(App):
def build(self):
self.title = 'Hello'
testiApp().run()
KV.
ScreenManagement:
MainScreen:
name:'Main'
SecondScreen:
name:'Page2'
<MainScreen>:
name:'Main'
BoxLayout:
orientation:'vertical'
GridLayout:
cols:2
Label:
text:'testfield1'
TextInput:
id: textfield1
Label:
text:'testfield2'
TextInput:
id: textfield2
Button:
text:'Next Page'
on_release: app.root.current ='Page2'
<SecondScreen>:
name:'Page2'
Button:
text:'Clear textfields'
on_release:
如果我没理解错的话,您要做的是使用第 X 页 (Main
?) 中的按钮来更改第 Y 页 (Page2
?) 中的文本。我不是 Kivy 专家,所以可能有更好的方法,但这里有一些想法:
1) 我尝试为所有屏幕提供 class 属性 parent
,结果证明这是个坏主意,因为该名称已被 Kivy 使用。您可以简单地将其更改为 parent_
或其他内容,然后自己尝试一下。您想要的是在创建时将 "parent" 作为参数传递给 __init__
:
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) 我也从youtube tutorial看到了另一种方式。不要直接 运行 您的应用程序,而是将其分配给一个变量并引用该变量。对于高级用例,这可能仍需要篡改:
# Use the global variable within your classes/methods
class Whatever:
def whatever2(self, params):
app.something()
....
app = testiApp()
app.run()
需要以下增强功能(kv 文件和 Python 脚本)才能在另一个屏幕中清除 TextInput
的文本。
kv 文件
- 为了访问
TextInput
小部件,将id: container
添加到实例化的 object、GridLayout:
- 默认情况下,每个屏幕都有一个 属性
manager
,它为您提供了所用ScreenManager
的实例。 - 将
on_release
事件绑定到方法,clear_inputs()
不带任何参数
片段 - kv 文件
<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()
Py文件
- 添加导入语句,
from kivy.uix.textinput import TextInput
- 使用
ScreenManager
的get_screen('Main')
函数得到实例化的object,MainScreen
- 使用
for
循环通过ids.container
遍历 - 使用
isinstance()
函数检查TextInput
小部件
GridLayout:
的children
片段 - Py 文件
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 = ''