【问题标题】:How to clear textinputs by button from another page in Kivy?如何通过 Kivy 中另一个页面的按钮清除文本输入?
【发布时间】:2019-06-04 10:04:04
【问题描述】:

我的问题可能主要是因为缺乏技能,但我找不到任何类似的帖子。所以我在主屏幕上有文本输入。我需要在 secondscreen 中有按钮来清除这些文本输入。

我不知道如何调用 clear_inputs 方法并将 textinput 作为参数传递。我想用这个 clear_inputs 方法我可以清空那些文本字段,但是如何将它绑定到另一个页面中的那个按钮?

派。

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()

千伏。


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:


【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

    需要以下增强功能(kv 文件和 Python 脚本)才能在另一个屏幕中清除 TextInput 的文本。

    kv 文件

    • 为了访问TextInput 小部件,将id: container 添加到实例化对象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
    • 使用ScreenManagerget_screen('Main')函数获取实例化对象MainScreen
    • 使用for循环通过ids.container遍历GridLayout:的孩子
    • 使用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 = ''
    

    【讨论】:

    • 嗨,再次感谢这似乎工作。顺便说一句,如果同一页面中还有其他布局需要清空文本字段,您将如何使其工作?我尝试了一些不值得一提的测试,但无法成功。
    • 您是指MainScreen 中的两个或多个容器,因此TextInput 的文本需要清晰?
    • 你好伊科林姆。是的,我确实指的是那个。你会怎么做?
    • 假设容器的idsbox1box2,为每个容器添加for循环。例如for child in reversed(main.ids.box1.children):
    • 谢谢!这需要一些时间才能让我的头脑像编码器一样思考:P 如此简单的解决方案!
    【解决方案2】:

    如果我没有正确理解,您想要做的是使用页面 X (Main?) 中的按钮来更改页面 Y (Page2?) 中的文本。我不是 Kivy 方面的专家,所以可能有更好的方法,但这里有一些想法:

    1) 我尝试为所有屏幕赋予类属性parent,结果证明这是个坏主意,因为 Kivy 已经使用了该名称。您可以简单地将其更改为 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()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2018-09-20
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多