Failing to create a combo box with kivy

491 views
Skip to first unread message

Tatenda Mukandi

unread,
Sep 30, 2021, 8:33:37 AM9/30/21
to Kivy users support
please help, I want to create a combo box on my Kivy project but I can't find the way to go about it

Elliot Garbus

unread,
Sep 30, 2021, 11:43:27 AM9/30/21
to kivy-...@googlegroups.com

Just to be clear, you want to combine a textInput with a dropdown.

The TextInput has a button next to it that will open the drop down, or the user can just type in the TextInput.

Is this correct?

 

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/9f7d5600-65da-43c5-a354-b219d7ed2428n%40googlegroups.com.

 

Elliot Garbus

unread,
Oct 3, 2021, 4:27:42 PM10/3/21
to kivy-...@googlegroups.com

I went ahead and created a ComboBox.  There are a number of things you could do to make this more configurable, this this is a good start.

 

# Example Combo box
# combination of a drop-down list or list box and a single-line editable textbox,
# allowing the user to either type a value directly or select a value from the list.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.dropdown import DropDown
from kivy.uix.label import Label
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import ListProperty

kv =
"""
<ComboButton>:
    size_hint_y: None
    height: 30
    color: 'black'
    on_release: print(self.text)
    canvas.before:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

<ComboBox>:
    values: [f'Value {i}' for i in range(10)]
    BoxLayout:
        id: box
        size_hint_y: None
        height: 30
        TextInput:
            id: ti
        Button:
            size_hint_x: None
            width: 24
            border: 0, 0, 0, 0
            font_name: 'arial'
            text: '
\u02C5'
            on_release: root.open_drop(box)

FloatLayout:
    ComboBox:
        pos_hint: {'center_x': 0.5, 'y': 0.5}
        size_hint_x: None
        width: 200
"""


class ComboButton(ButtonBehavior, Label):
   
pass


class
ComboBox(RelativeLayout):
    values = ListProperty()

   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.dropdown = DropDown()  # Create the dropdown once and keep a reference to it
       
self.dropdown.bind(on_select=lambda instance, x: self.set_text(x))

   
def on_kv_post(self, base_widget):
       
for v in self.values:
            cb = ComboButton(
text=v)
            cb.bind(
on_release=lambda btn: self.dropdown.select(btn.text))
           
self.dropdown.add_widget(cb)

   
def set_text(self, v):
       
self.ids.ti.text = v

   
def open_drop(self, w):
       
self.dropdown.open(w)


class ComboBoxTestApp(App):
   
def build(self):
       
return Builder.load_string(kv)


ComboBoxTestApp().run()
D75384C1B08541BD9900820B921BF4F7.png
Reply all
Reply to author
Forward
0 new messages