【发布时间】:2019-01-11 04:10:44
【问题描述】:
我有一个包含许多按钮的下拉菜单和一个包含许多字符串的列表,因此按钮中的每个文本都应该是列表中的字符串(button1 具有列表中的第一个字符串,button2 具有第二个字符串,依此类推。问题是我所有的按钮文本都只有列表中的最后一个字符串。
我基本上尝试做一个简单的循环,在列表中循环并将结果设置在 StringProperty 中,然后将 StringProperty 放在 kv 文件中按钮的“文本”上。
.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”
【问题讨论】:
-
您的所有按钮都使用
root.client_text作为其文本。这只是一个StringProperty,所以所有这些按钮最终都会以StringProperty中的任何字符串结束。你的clientes列表会改变吗? -
@JohnAnderson 我将更新列表是的,它将添加更多名称和按钮。
-
您将添加名称和按钮,但是任何按钮的名称会改变吗?或者在
GuiApp运行时分配给特定按钮的名称是否保持不变。 -
@JohnAnderson 前面的按钮永远不会改变,但下一个添加的按钮应该有与其他按钮不同的文本。 (它来自列表)。
标签: python python-3.x kivy kivy-language