kivy动态添加和删除下拉条目

时间:2016-07-06 22:29:50

标签: python kivy

我的应用应该在下拉菜单中动态添加和删除项目。添加按钮有效,但我没有让它删除添加的按钮。

吡啶:

import kivy
kivy.require('1.7.2') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown

class HomeScreen(Screen):
    addButton = ObjectProperty(None)
    removeButton = ObjectProperty(None)
    top_layout = ObjectProperty(None)

    def __init__(self, *args, **kwargs):
        super(HomeScreen, self).__init__(*args, **kwargs)

    def resetBoxes(self):
        self.ids.btn_release.text = "Release"
        self.ids.btn_version.text = "Version"
        self.ids.btn_device.text = "Device"
        return

    def removeButtonPressed(self):
        self.dropdown.remove_widget(self)
        return

    def addButtonPressed(self):
        self.dropdown = DropDown()
        notes = ['Features', 'Suggestions', 'Abreviations', 'Miscellaneous']
        for note in notes:
            btn = Button(text=note, size_hint_y=None, height=20)
            btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
            self.dropdown.add_widget(btn)
            self.ids.btn_release.bind(on_release=self.dropdown.open)
        self.dropdown.bind(on_select=lambda instance, x: setattr(self.ids.btn_release, 'text', x))
        return

    def btn_releaseClicked(self):
        self.ids.btn_release.text="clicked"
    def btn_versionClicked(self):
        self.ids.btn_version.text="clicked"
    def btn_deviceClicked(self):
        self.ids.btn_device.text="clicked"
class dropdApp(App):
    def build(self):
        return HomeScreen()


if __name__ == '__main__':
    dropdApp().run()

KV:

<HomeScreen>:
    id: home_screen
    addButton: addButtonID
    removeButton: removeButtonID
    top_layout: topLayoutID

    orientation: 'vertical'
    FloatLayout: 
        size_hint: 1, 1
        Button:
            id: addButtonID
            text: 'Add'
            pos_hint: {'x': .35, 'y': .70}
            size_hint: .3, .08
            valign: 'middle'
            halign: 'center'
            text_size: self.size
            on_release: root.addButtonPressed()
        Button:
            id: removeButtonID
            text: 'Remove'
            pos_hint: {'x': .35, 'y': .60}
            size_hint: .3, .08
            valign: 'middle'
            halign: 'center'
            text_size: self.size
            on_release: root.removeButtonPressed()


    BoxLayout:
        id: topLayoutID
        size_hint: 1, .05
        pos_hint: {'x': 0, 'y': .90}
        Button:
            id: btn_release
            text: 'Release'
            on_press: root.btn_releaseClicked()
        Button:
            id: btn_version
            text: 'Version'
            on_press: root.btn_versionClicked()
        Button:
            id: btn_device
            text: 'Device'
            on_press: root.btn_deviceClicked()

1 个答案:

答案 0 :(得分:0)

使用clear_widgets()获取您想要的结果 - 从下拉列表中删除所有按钮 - 或者在某个位置的下拉列表中存储对这些按钮的一些引用,并仅删除所需的按钮。

def removeButtonPressed(self):
    self.dropdown.clear_widgets()