Kivy 将下拉菜单添加到现有布局中。

问题描述 投票:0回答:1

我想在我已有的布局中添加下拉菜单,但当我添加它时,我遇到了一些问题。是否有什么东西,我添加了不正确的小部件?

注意:我喜欢动态地创建布局,所以我没有使用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()

截图:enter image description here

kivy dropdown dynamic-programming python-3.7
1个回答
0
投票

你缺少的主要是添加了 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

0
投票

对于那些也有同样问题的人 我发现了一个叫微调器的替代品 这个微调器对象的工作原理和我对弹出式菜单的打算是一样的。

这是我添加的代码,而不是下拉菜单。

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))
© www.soinside.com 2019 - 2024. All rights reserved.