Skip to content

Instantly share code, notes, and snippets.

@pamolloy
Created October 21, 2013 22:49
Show Gist options
  • Save pamolloy/7092215 to your computer and use it in GitHub Desktop.
Save pamolloy/7092215 to your computer and use it in GitHub Desktop.
An example of overloading open() in kivy/uix/dropdown.py to allow the function to be bound to a TextInput object
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.textinput import TextInput
class TextDropDown(DropDown):
def __init__(self, *args, **kwargs):
super(TextDropDown, self).__init__(*args, **kwargs)
def open(self, widget, text):
# ensure we are not already attached
if self.attach_to is not None:
self.dismiss()
# we will attach ourself to the main window, so ensure the widget we are
# looking for have a window
self._win = widget.get_parent_window()
if self._win is None:
raise DropDownException(
'Cannot open a dropdown list on a hidden widget')
self.attach_to = widget
widget.bind(pos=self._reposition, size=self._reposition)
self._reposition()
# attach ourself to the main window
self._win.add_widget(self)
class Test(App):
def build(self):
main = TextInput(text='Television', size_hint=(None, None))
dropdown = TextDropDown()
dropdown.add_widget(Button(text='Breaking Bad', size_hint_y=None))
dropdown.add_widget(Button(text='Homeland', size_hint_y=None))
main.bind(text=dropdown.open)
return main
Test().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment