Reputation: 4903
Is there any way to limit the size of DropDown box? I would like to limit it to size of maybe 4-5 choices, instead of having it running all the way to the edge of screen.I started experimenting with DropDown as Spinner didn't have any interface for doing so.. Both elements in my use case should do the same, but it seemed like DropDown would provide more flexibility as I had to compose the picker myself. Unfortunately, changing hint or size for DropDown seems to do nothing, as the list still gets fully expanded.
Here is code for what I got now:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
class DropDownPicker(Button):
def __init__(self, listOfChoices, **kwargs):
result = super(DropDownPicker, self).__init__(**kwargs)
self.text='Select'
pickerList = DropDown()
for item in listOfChoices:
choiceButton = Button(text=item, size_hint_y=None, height=25)
choiceButton.bind(on_release=lambda btn: pickerList.select(btn.text))
pickerList.add_widget(choiceButton)
self.bind(on_release=pickerList.open)
pickerList.bind(on_select=lambda instance, x: setattr(self, 'text', x))
pickerList.size_hint_y = None
pickerList.size = (0, 100)
return result
class TestApp(App):
def build(self):
choices = []
for x in range(50):
choices.append("Choice " + str(x))
return DropDownPicker(choices)
if __name__ == '__main__':
TestApp().run()
Those last 2 lines before return
should be doing the trick, but they aren't. I also tried to specify this in DropDown constructor, however I changed this because at that time there might have been problem that final size is not yet known. This wasn't the case as it still doesn't work.
Upvotes: 0
Views: 1425
Reputation: 8747
Try modyfing size_hint_y
property, but not to None
(it's default) but to the decimal value, for example:
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.base import runTouchApp
dropdown = DropDown()
dropdown.size_hint_y = 0.5
for index in range(100):
btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
btn.bind(on_release=lambda btn: dropdown.select(btn.text))
dropdown.add_widget(btn)
mainbutton = Button(text='Hello', size_hint=(None, None))
mainbutton.bind(on_release=dropdown.open)
dropdown.bind(on_select=lambda instance, x: setattr(mainbutton, 'text', x))
runTouchApp(mainbutton)
Upvotes: 1