I've coded this little app with Kivy that has 3 textbox + 1 dropdown:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
class Inremapp(App):
def AddDropDownButton(self, name):
self.btn = Button(text=name, size_hint_y=None, height=44)
self.btn.bind(on_release=lambda btn: self.dropdown.select(self.btn.text))
self.dropdown.add_widget(self.btn)
def build(self):
self.operaciones = ["1", "123", "1241", "124121"]
self.window = GridLayout()
self.window.cols = 1
self.window.size_hint = (0.6, 0.7)
self.window.pos_hint = {"center_x": 0.5, "center_y":0.5}
# entrada
labelentrada = Label(text='Entrada: (Día/Mes/Año)')
self.window.add_widget(labelentrada)
txtinday = TextInput(text='', multiline=False)
self.window.add_widget(txtinday)
# salida
labelsalida = Label(text='Salida: (Día/Mes/Año)')
self.window.add_widget(labelsalida)
txtoutday = TextInput(text='', multiline=False)
self.window.add_widget(txtoutday)
# obra
txtobra = TextInput(text='Obra', multiline=False)
self.window.add_widget(txtobra)
# operación
self.dropdown = DropDown()
for operacion in self.operaciones:
self.AddDropDownButton(operacion)
mainbutton = Button(text='Operación')
mainbutton.bind(on_release=self.dropdown.open)
self.dropdown.bind(on_select=lambda instance, x: setattr(mainbutton, 'text', x))
self.window.add_widget(self.dropdown)
self.window.add_widget(mainbutton)
return self.window
# run app
if __name__ == "__main__":
Inremapp().run()
The app looks like this when running:
But when I click the dropdown button I get the following error:
Exception has occurred: WidgetException Cannot add <kivy.uix.dropdown.DropDown object at 0x000002D18787E6C0> to window, it already has a parent <kivy.uix.gridlayout.GridLayout object at 0x000002D1873CEF10> File "D:\Inrema\TabletConnect\main.py", line 68, in Inremapp().run()
The basic example from Kivy docs is working fine but... what I'm missing in my code?
EDIT:
After removing self.window.add_widget(self.dropdown) error disappeared, dropdown is showing up but when I select any element I will always get the latest value, doesn't matters what I select.
EDIT 2:
Changed this line:
self.btn.bind(on_release=lambda btn: self.dropdown.select(self.btn.text))
To this:
self.btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
And worked fine :)
