Kivy Clear_widget奇怪的行为(附带可重现的代码)

时间:2013-03-04 02:09:36

标签: python kivy

只是我想要做的事情的简要说明。我在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,其中按钮的数量等于结果的数量(即搜索中result_list中的元素数量。现在,当我单击按钮时,会发生的是它将按钮文本传输到另一个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() 

谢谢!

1 个答案:

答案 0 :(得分:3)

您正在尝试清除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()