Search code examples
kivydropdowndynamic-programmingpython-3.7

Kivy Add Dropdown Programmtically to existing layout


I would like to add dropdown to a layout that I already have but I'm having issues when I add it. the layout randomly displays the options but not the mainbutton. Is there something am I adding the incorrect widget?

NOTE: I prefer to create my layouts dynamically, so I am not using the kv files. All of the examples I have seen just use the runapp function instead of adding the dropdown to an existing layout.

My Code:

Window.size = (Config.getint('graphics','width'),Config.getint('graphics','height'))
Window.top  = 30
Window.left  = 10
screen_width = 700
screen_height = 775
Window.clearcolor = color_palette["Royal Purple"]
Window.size = (screen_width,screen_height)
Config.write()

##~ dropdown function ~##
def createDropDown():

    dropdown = DropDown()
    categories = ['Cat1','Cat2','Cat3','Cat4']
    for index in range(len(categories)):
        btn = Button(text=categories[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))

    return dropdown

##~~ My main layout class ~~##
class SimpleKivy0(App):
    App.title = "New GUI"
    btn_width = 0.2
    btn_height = 0.05

    txt_fld1 = None
    entry_fld_Label = None

    def build(self):
        layout = FloatLayout()      
        banner_bground = layout.canvas.before
        with banner_bground:
            Rectangle(pos=(0,screen_height-(screen_height*0.1)),size=(screen_width, screen_height*0.1))
            Color(68/255,210/255,201/255,1)
        banner_Lbl = Label(text='Quick Query GUI',halign='left',valign='middle',font_size=((screen_height*0.1)*0.7),pos=(0,screen_height-(screen_height*0.1)),size_hint=(1,0.1))
        banner_Lbl.bind(size=banner_Lbl.setter('text_size'))    
        layout.canvas.add(banner_bground)
        layout.add_widget(banner_Lbl)


        dropdown = createDropDown()     
        dropdown.size_hint=(.25,.1)
        dropdown.pos = (screen_width*0.2,screen_height*0.2)
        return layout


if __name__ == "__main__":
    SimpleKivy0().run()

The screenshot: enter image description here


Solution

  • For those who are also having an issue with this, I came across an alternative called a spinner. The spinner object worked the same as I intended for the popup.

    This is the code I added instead of the dropdown:

    def get_spinner_selection(spinner,text):
        print(text)
    
    spinner = Spinner(text='Option1',values=('Option1','Option2','Option3'),size_hint=(0.2,0.1),background_color=(169/255,80/255,18/255,1))
    spinner.bind(text=get_spinner_selection)
    layout.add_widget(spinner)
    
    queryType_Lbl = Label(text='Query Type:',valign='middle',size_hint=self.spinner.size_hint,pos=(spinner.x,spinner.top))