Skip to content Skip to sidebar Skip to footer

Kivy: Sizing Buttons To Fit Wrapped Text Within Dropdown

I'm having issues building a Kivy dropdown with word wrapping enabled for the text, such that the button widgets size accordingly to accommodate the full text. I have followed guid

Solution 1:

The solution is to replace text_size : self.size with text_size : self.width, None. Please refer to the example and output for details.

Example

main.py

from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
from kivy.base import runTouchApp
from kivy.lang import Builder

Builder.load_string('''
<WrapButton>:
    halign: "center"
    valign: "center"
    font_size: 20
    size_hint_y: None
    text_size : self.width, None
    height: self.texture_size[1]
''')


classWrapButton(Button):
    pass


dropdown2 = DropDown()

layout = BoxLayout(padding=0, orientation='vertical')

mainbutton2 = WrapButton(text='Select...', size_hint=(1, None), height=95, pos_hint={'center_x': .5, 'center_y': 0})
mainbutton2.bind(on_release=dropdown2.open)
layout.add_widget(mainbutton2)

for index inrange(20):
    btn2 = WrapButton(text=('|' + str('long text..is long...%d' % (21-index) * index) + '|'), size_hint=(1, None))
    btn2.bind(on_release=lambda btn2: dropdown2.select(btn2.text))
    dropdown2.add_widget(btn2)

dropdown2.bind(on_select=lambda instance, x: setattr(mainbutton2, 'text', x))

runTouchApp(layout)

Output

Img01

Post a Comment for "Kivy: Sizing Buttons To Fit Wrapped Text Within Dropdown"