Kivy 以编程方式将下拉列表添加到现有布局

Kivy Add Dropdown Programmtically to existing layout

我想将下拉菜单添加到我已有的布局中,但在添加时遇到问题。布局随机显示选项但不显示主按钮。是不是我添加的小部件不正确?

注意:我更喜欢动态创建布局,所以我没有使用 kv 文件。我看到的所有示例都只使用 runapp 函数,而不是将下拉列表添加到现有布局。

我的代码:

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()

截图:

您缺少的主要内容是将 mainbutton 添加到您的布局中。这是您的部分代码的工作版本:

def createDropDown(layout):

    dropdown = DropDown()
    categories = ['Cat1','Cat2','Cat3','Cat4']
    for index in range(len(categories)):
        btn = Button(text=categories[index], size_hint=(None, None), size=(100,44))

        btn.bind(on_release=lambda btn: dropdown.select(btn.text))

        dropdown.add_widget(btn)

    mainbutton = Button(text='Hello', size_hint=(None, None), size=(100,40), pos=(100,100))
    mainbutton.bind(on_release=dropdown.open)
    dropdown.bind(on_select=lambda instance, x: setattr(mainbutton, 'text', x))

    # Add mainbutton to the layout
    layout.add_widget(mainbutton)

##~~ 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)

        # create the dropdown and add the mainbutton to the layout
        createDropDown(layout)

        return layout

对于那些也有此问题的人,我遇到了一种称为微调器的替代方法。微调器对象的工作方式与我为弹出窗口设计的一样。

这是我添加的代码,而不是下拉列表:

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))