首页 > 解决方案 > 如果我必须给它们一个唯一的名称,我如何从字典键(数量未知)创建按钮,以便我可以在它们上使用 call_back() 函数

问题描述

我正在制作一个 GUI。有一个主数据库(字典),从中导入其密钥。为了轻松导入数据,我添加了一个 TextInput 并添加了一个搜索过滤器视图。每当在 Textinput 中键入一个字母时,它都会显示与文本字符串匹配的所有键(在标签中)。

在一个键(标签文本)旁边有一个按钮,可将标签的文本添加到新滚动视图中的另一个标签,在该滚动视图中形成不同的计算。

所以我之前认为进入下一个标签我应该尝试通过将它打印到控制台来获取按钮的值,并且每当我按下标签旁边的按钮时,我只会在搜索视图中获得最后一个标签的文本而不是 Label1s它连接到。(但我不知道我是否连接正确,它以某种方式与标签的命名有关,但我不知道如何)

这是一张可以更好地理解我的问题的图片(它显示了我想要做的事情):

图片:我描述的屏幕

py:

class CalculationView(BoxLayout):
    SearchGridShown = BooleanProperty()
    SLB = ObjectProperty()

    def find_searched_text(self, text):
        Dict = db.databaseDict
        self.ids.SearchGrid.clear_widgets()
        FD = {k: v for (k, v) in Dict.items() if text in k and len(text) > 0}
        for kk, vv in FD.items():

            self.SLB = Label(
                color=(.2, .1, 0, 1),
                text=str(kk))

            self.BTN = Button(
                text='OK',
                background_color=(.05, .3, .05, 1),
                font_size='13sp',
                size_hint=(.1, 1))
            self.BTN.bind(on_press=lambda x: self.add_product_for_calculation(self.SLB.text))
            self.BL = BoxLayout()
            self.BL.add_widget(self.SLB)
            self.BL.add_widget(self.BTN)
            self.ids.SearchGrid.add_widget(self.BL)

    def add_product_for_calculation(self, product):
        print('0' + product + '0')
        Dict = db.databaseDict
        V = Dict[product]
        print(V)

千伏:

<CalculationView>:
    orientation: 'vertical'
    size_hint_x: .96
    size_hint_y: .9
    pos_hint: {'x': .02,'y': .0875}

    BoxLayout:
        spacing: 2, 0
        padding: 1
        size_hint: 1, .06
        TextInput:
            size_hint: 1.5, 1
            text: 'Kereses'
            on_focus: self.text = ""
            font_size: '13sp'
            on_text: root.find_searched_text(self.text)
        Button:
            size_hint: .25, 1
            text: 'Keres'
            on_release: root.SearchGridShown = not root.SearchGridShown

    BoxLayout:
        ...

    ScrollView:
        do_scroll_x: False
        do_scroll_y: True
        size_hint_x: 1
        size_hint_y: .9
        pos_hint: {'x': 0,'y': .1}

        GridLayout:
            id: CalculationContainer
            size_hint_y: None
            height: self.minimum_height
            row_force_default: True
            row_default_height: '30dp'
            cols: 4
            spacing: 2, 0
            padding: 1

    BoxLayout:
        ...

    BoxLayout:
        size_hint: 1, .001
        FloatLayout:
            size: self.size
            pos: self.pos
            GridLayout:
                id: SearchGrid
                spacing: 2, 2
                cols:1
                size_hint: .975, None
                height: self.minimum_height
                row_force_default: True
                row_default_height: '35dp'
                pos_hint: {'x': 0 if root.SearchGridShown else 0,'top': 990}
                opacity: .75
                canvas.before:
                    Color:
                        rgba: 1, 1, 1, 1
                    Rectangle:
                        size: self.size
                        pos: self.pos

标签: pythonloopskivy

解决方案


推荐阅读