我有一个包含许多按钮的下拉列表和一个包含许多字符串的列表,因此按钮中的每个文本都应该是列表中的字符串(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”
因为按钮文本不会改变,所以不需要
client_text
StringProperty
。如果您需要更改Button
或Label
的文本,StringProperty
是非常好的,但在这里不是必需的。你知道吗有几种方法可以实现你想要的。我的第一个想法是直接使用
clientes
列表。我已经将clientes
列表移到了CustomDropDown1
类中,因为它就是在那里使用的。你知道吗我还向按钮添加了ID,并删除了
kv
文件中的text:
属性:因此,每个按钮都有一个
id
形式的“button#”,其中“#”是按钮编号,然后CustomDropDown1
的__init__()
方法中的循环根据编号构建按钮id,并使用编号作为clientes
列表的索引来设置文本。你知道吗另一种方法是完全消除循环,只需将按钮文本设置为
对于第一个按钮
对于下一个,依此类推(使用
CustomDropDown1
类中的clientes
列表)。你知道吗相关问题 更多 >
编程相关推荐