sanodinnagval

suggests words when entering letters of coincidence

May 11th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.uix.floatlayout import FloatLayout
  3. from kivy.uix.dropdown import DropDown
  4. from kivy.uix.button import Button
  5. from kivy.uix.textinput import TextInput
  6. from kivy.properties import ListProperty, StringProperty
  7. from kivy.clock import Clock
  8.  
  9. import re
  10.  
  11. from kivy.lang import Builder
  12.  
  13. Builder.load_string('''
  14. <MainView>:
  15.    ComboEdit:
  16.        size_hint: .5, .3
  17.        pos_hint: {'center':(.5, .5)}
  18.        # `args` is the keyword for arguments passed to `on_text` in kv language
  19.        on_text: root.on_text(self, args[1])
  20.  
  21.    
  22. ''')
  23.  
  24. class ComboEdit(TextInput):
  25.     '''
  26.    This class defines a Editable Combo-Box in the traditional sense
  27.    that shows it's options
  28.    '''
  29.  
  30.     options = ListProperty(('', ))
  31.     '''
  32.    :data:`options` defines the list of options that will be displayed when
  33.    touch is released from this widget.
  34.    '''
  35.  
  36.     def __init__(self, **kw):
  37.         ddn = self.drop_down = DropDown()
  38.         ddn.bind(on_select=self.on_select)
  39.         super(ComboEdit, self).__init__(**kw)
  40.  
  41.     def on_options(self, instance, value):
  42.        
  43.         ddn = self.drop_down
  44.         # clear old options
  45.         ddn.clear_widgets()
  46.         for option in value:
  47.             # create a button for each option
  48.             but = Button(text=option,
  49.                         size_hint_y=None,
  50.                         height='36sp',
  51.                         # and make sure the press of the button calls select
  52.                         # will results in calling `self.on_select`
  53.                         on_release=lambda btn: ddn.select(btn.text))
  54.             ddn.add_widget(but)
  55.  
  56.     def on_select(self, instance, value):
  57.         # on selection of Drop down Item... do what you want here
  58.         # update text of selection to the edit box
  59.        
  60.         self.text = value
  61.  
  62. class MainView(FloatLayout):
  63.  
  64.     rtsstr = StringProperty("".join(("Substrate1,,,Substrate1,,,Substrate1,,,",
  65.                             "Substrate1,,,Substrate1,,,Substrate_coating",
  66.                             ",,,silicon,,,silicon_Substrate,,,substrate_",
  67.                             "silicon,,,")))
  68.     #print(rtsstr)
  69.     def on_text(self, instance, value):
  70.         print(instance, value)
  71.         #instance.drop_down.dismiss()
  72.         if value == '':
  73.             instance.options=[]
  74.         else:
  75.             match = re.findall("(?<=,{3})(?:(?!,{3}).)*?%s.*?(?=,{3})" % value,\
  76.                                 self.rtsstr, re.IGNORECASE)
  77.             #using a set to remove duplicates, if any.
  78.             instance.options = list(set(match))
  79.  
  80.        
  81.         instance.drop_down.open(instance)
  82.        
  83.  
  84. class TestApp(App):
  85.  
  86.     def build(self):
  87.         return MainView()
  88.  
  89. if __name__ == '__main__':
  90.     TestApp().run()
Add Comment
Please, Sign In to add comment