【问题标题】:Kivy Clear_widget strange behaviour (reproducible code attached)Kivy Clear_widget 奇怪的行为(附上可重现的代码)
【发布时间】:2013-03-04 02:09:36
【问题描述】:

只是我正在尝试做的事情的简要说明。我在 FloaLayout 小部件中有两个 TextInput 小部件,它们都在 .kv 文件中定义,如下所示。 (我在 kivy 论坛上问过这个问题,但提供的解决方案没有奏效,所以我在这里问它以获得一些新想法)

test.kv

<TESTGUI>:
    t2: TI2
    t4: TI4
    fl1: FL1

FloatLayout:
    orientation: 'lr-bt'
    id: FL1
    size: root.size
    TextInput:
        id: TI2
        size_hint: 1, 0.1  
        pos_hint: {'top': 1}
        font_size: 35
        on_text: root.realTimeSearch(TI2, TI2.text)
    TextInput:
        id: TI4
        size_hint: 1, 0.1
        pos_hint: {'top': 0.86}
        font_size: 15

现在,当我在其中一个 TextInput 小部件 (t2) 中输入任何文本时,程序所做的就是在字符串中搜索该文本。每当 TextInput 小部件文本更改时,都会执行此搜索。所以基本上只要你开始输入搜索就会动态地开始。搜索结果可以有很多匹配项(所有这些匹配项都存储在一个名为 result_list 的列表中(参见下面的代码)),并且根据匹配项的数量,我添加了一个 GridLayout,其按钮数量等于结果数量(即搜索结果列表中的元素数)。现在,当我单击按钮时,发生的情况是将按钮文本传输到另一个 TextInput 小部件(如上图所示的 t4)。下面是 .py 文件中的完整代码 该程序基本上是一个具有自动完成功能的搜索实用程序。我遇到的问题是 clear_widgets 在当前上下文中似乎不起作用。所以我得到了很多小部件,我需要点击它们中的每一个来摆脱它们(请参阅下面的代码以获得清晰的解释)

我建议您在您的机器上运行代码以了解正在发生的事情(尝试在 t2 textinput 小部件中输入“silicon”,您会看到 clear_widgets 无法正常工作)。

import re
import sys
import kivy
kivy.require('1.5.1')
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.popup import Popup
from kivy.uix.scrollview import ScrollView
from collections import Counter
from functools import partial
reload(sys)
sys.setdefaultencoding('utf-8')

rtsstr =",,,Substrate1,,,Substrate1,,,Substrate1,,,Substrate1,,,Substrate1,,,Substrate_coating,,,silicon,,,silicon_Substrate,,,substrate_silicon,,,"
#rtsstr is the string on which search is being performed


class TESTGUI(Widget):

t2 = ObjectProperty(None)
t4 = ObjectProperty(None)
fl1 = ObjectProperty(None)

def realTimeSearch(self, instance, value):
    """
    this function is used as a real time update of search results based on the search query. It also identifies partial matches (e.g. a search for silicon can result in entries such as silicon, silicon nitride, silicon dioxide etc. being displayed
    """
    if value != '':            
        match = re.findall("(?<=,{3})(?:(?!,{3}).)*?%s.*?(?=,{3})" % value, rtsstr, re.IGNORECASE)
        result_list = list(set(match)) #using a set to remove duplicates, if any.
        self.create_search(result_list)

def create_search(self, result_list):
    layt = GridLayout(cols=3, size_hint_y = None)
    layt.bind(minimum_height=layt.setter('height'))
    scrlv = ScrollView(size_hint=(1, 0.8), pos_hint={'top': 0.8})
    self.fl1.remove_widget(scrlv)

    for result in result_list:
        buttn2 = Button(text = str(result), size_hint = (0.3, None), height = 40)
        buttn2.bind(on_press = partial(self.transferSearchText, buttn2.text, scrlv))
        layt.add_widget(buttn2)
    scrlv.add_widget(layt)
    self.fl1.add_widget(scrlv)   

def transferSearchText(self, text, scrlv, *args):
    self.t4.insert_text(text + ',')
    scrlv.clear_widgets()
    self.fl1.remove_widget(scrlv) 
    self.t2.text = ''                     

class TestApp(App):
    def build(self):
        return TESTGUI()


if __name__ == '__main__':
    TestApp().run() 

谢谢!

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    您正在尝试清除 Scrollview,您应该清除添加到 ScrollView 的布局。

    Widget.Clear_widget() 只清除当前小部件的子级,它不是递归的,也不是这样的。

    Kivy 不提供传统的可编辑组合框。 Kivy 如何让您非常轻松地创建自己的结合 TextInput 和 DropDown 的小部件。

    您应该使用类似 snippets wiki 中所示的 ComboEdit,并对其进行修改以满足您的需要。

    所以,你想要达到的目标可以这样完成::

    from kivy.app import App
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.dropdown import DropDown
    from kivy.uix.button import Button
    from kivy.uix.textinput import TextInput
    from kivy.properties import ListProperty, StringProperty
    
    import re
    
    from kivy.lang import Builder
    
    Builder.load_string('''
    <MainView>:
        ComboEdit:
            size_hint: .5, .3
            pos_hint: {'center':(.5, .5)}
            # `args` is the keyword for arguments passed to `on_text` in kv language
            on_text: root.on_text(self, args[1])
    ''')
    
    class ComboEdit(TextInput):
        '''
        This class defines a Editable Combo-Box in the traditional sense
        that shows it's options 
        '''
    
        options = ListProperty(('', ))
        '''
        :data:`options` defines the list of options that will be displayed when
        touch is released from this widget.
        '''
    
        def __init__(self, **kw):
            ddn = self.drop_down = DropDown()
            ddn.bind(on_select=self.on_select)
            super(ComboEdit, self).__init__(**kw)
    
        def on_options(self, instance, value):
            ddn = self.drop_down
            # clear old options
            ddn.clear_widgets()
            for option in value:
                # create a button for each option
                but = Button(text=option,
                            size_hint_y=None,
                            height='36sp',
                            # and make sure the press of the button calls select
                            # will results in calling `self.on_select`
                            on_release=lambda btn: ddn.select(btn.text))
                ddn.add_widget(but)
    
        def on_select(self, instance, value):
            # on selection of Drop down Item... do what you want here
            # update text of selection to the edit box
            self.text = value
    
    class MainView(FloatLayout):
    
        rtsstr = StringProperty("".join(("Substrate1,,,Substrate1,,,Substrate1,,,",
                                "Substrate1,,,Substrate1,,,Substrate_coating",
                                ",,,silicon,,,silicon_Substrate,,,substrate_",
                                "silicon,,,")))
    
        def on_text(self, instance, value):
            if value == '':
                instance.options=[]
            else:
                match = re.findall("(?<=,{3})(?:(?!,{3}).)*?%s.*?(?=,{3})" % value,\
                                    self.rtsstr, re.IGNORECASE)
                #using a set to remove duplicates, if any.
                instance.options = list(set(match))
            instance.drop_down.open(instance)
    
    class TestApp(App):
    
        def build(self):
            return MainView()
    
    if __name__ == '__main__':
        TestApp().run()
    

    【讨论】:

    • 感谢您提供非常详细的回答。完美运行!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多