Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- from kivy.clock import Clock
- 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,,,")))
- #print(rtsstr)
- def on_text(self, instance, value):
- print(instance, value)
- #instance.drop_down.dismiss()
- 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()
Add Comment
Please, Sign In to add comment