通过python列表递归获取按钮文本

2024-04-19 19:57:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含许多按钮的下拉列表和一个包含许多字符串的列表,因此按钮中的每个文本都应该是列表中的字符串(button1包含列表中的第一个字符串,button2包含秒字符串,依此类推)。问题是我所有的按钮文本都只有列表中的最后一个字符串。你知道吗

我基本上试着做一个简单的循环,在列表上循环,在StringProperty中设置结果,并将StringProperty放在kv文件中按钮的“text”上。你知道吗

.py码:

# Here is the list
clientes = ['Sercom', 'Lideranca', 'Winover']



class MyScreenManager(ScreenManager):
    button_text = StringProperty('Clients')

    def __init__(self, **kwargs):
        super(MyScreenManager, self).__init__(**kwargs)
        self.dropdown = CustomDropDown1(self)

    def open_drop_down(self, widget):
        self.dropdown.open(widget)

class MyScreen(Screen):
    def __init__(self, **kwargs):
        super(MyScreen, self).__init__(**kwargs)



class CustomDropDown1(DropDown):
    client_text = StringProperty()

    def __init__(self, screen_manager, **kwargs):
        super(CustomDropDown1, self).__init__(**kwargs)
        self.sm = screen_manager
        self.is2Displayed = False

        # Here is my loop
        for message in clientes: 
            self.client_text = message



class GuiApp(App):
    def build(self):
        return MyScreenManager()

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

.kv电压:

<MyScreenManager>:
    MyScreen:
        FloatLayout:
            Button:
                text: root.button_text
                size: (200, 50)
                size_hint:(None,None)
                pos_hint: {'center_x': 0.5, 'top': 1.0}
                on_release: root.open_drop_down(self)

<CustomDropDown1>:
    padding: [0,0,0,0]
    Button:
        text: root.client_text
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (10,0)
        on_release: root.select(self.text)
    Button:
        text: root.client_text
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (10,0)
        on_release: root.select(self.text)

在我的例子中,第一个按钮的文本应该是“Sercom”,第二个按钮应该是“Lideranca”,第三个按钮应该是“Winover”。你知道吗

但所有按钮上的文字都是“Winover”


Tags: 字符串textselfnone列表sizeinitdef
1条回答
网友
1楼 · 发布于 2024-04-19 19:57:08

因为按钮文本不会改变,所以不需要client_textStringProperty。如果您需要更改ButtonLabel的文本,StringProperty是非常好的,但在这里不是必需的。你知道吗

有几种方法可以实现你想要的。我的第一个想法是直接使用clientes列表。我已经将clientes列表移到了CustomDropDown1类中,因为它就是在那里使用的。你知道吗

class CustomDropDown1(DropDown):
    clientes = ['Sercom', 'Lideranca', 'Winover']

    def __init__(self, screen_manager, **kwargs):
        super(CustomDropDown1, self).__init__(**kwargs)
        self.sm = screen_manager
        self.is2Displayed = False

        for button_num in range(2):
            id = 'button' + str(button_num)
            self.ids[id].text = self.clientes[button_num]

我还向按钮添加了ID,并删除了kv文件中的text:属性:

<CustomDropDown1>:
    padding: [0,0,0,0]
    Button:
        id: button0
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (10,0)
        on_release: root.select(self.text)
    Button:
        id: button1
        size:(200,50)
        size_hint:(None,None)
        text_size: self.size
        valign: 'center'
        padding: (10,0)
        on_release: root.select(self.text)

因此,每个按钮都有一个id形式的“button#”,其中“#”是按钮编号,然后CustomDropDown1__init__()方法中的循环根据编号构建按钮id,并使用编号作为clientes列表的索引来设置文本。你知道吗

另一种方法是完全消除循环,只需将按钮文本设置为

text: root.clientes[0]

对于第一个按钮

text: root.clientes[1]

对于下一个,依此类推(使用CustomDropDown1类中的clientes列表)。你知道吗

相关问题 更多 >