Python 多个kivy下拉列表
因此,我尝试使用kivy框架编写一个简单的代码,我的代码在字典中查找某个值,该值在稍后阶段告诉我的代码哪些字典值需要作为下拉菜单输出,哪些dict值只需要作为常规标签和文本输入输出。现在的问题在于,当我正在浏览的字典描述了多个需要下拉的值时。我希望下拉菜单工作的方式是,当在下拉菜单中选择一个值时,触发下拉菜单的按钮必须将文本更改为单击的按钮的文本。这是代码,我决定编写一个更简单的版本,以便于阅读。我想在代码下面输入这个,但我真的很不擅长在堆栈溢出上写问题。您将要查看的代码创建了两个按钮,分别用于触发下拉列表,一个命名为组0,另一个命名为组1。但是,当我单击组0下的其中一个下拉按钮时,组1文本属性会更改,但我希望实际更改组0按钮的文本属性。我有一个不合理的代表,当我解释,所以让我知道,如果你需要澄清,任何帮助将不胜感激Python 多个kivy下拉列表,python,kivy,Python,Kivy,因此,我尝试使用kivy框架编写一个简单的代码,我的代码在字典中查找某个值,该值在稍后阶段告诉我的代码哪些字典值需要作为下拉菜单输出,哪些dict值只需要作为常规标签和文本输入输出。现在的问题在于,当我正在浏览的字典描述了多个需要下拉的值时。我希望下拉菜单工作的方式是,当在下拉菜单中选择一个值时,触发下拉菜单的按钮必须将文本更改为单击的按钮的文本。这是代码,我决定编写一个更简单的版本,以便于阅读。我想在代码下面输入这个,但我真的很不擅长在堆栈溢出上写问题。您将要查看的代码创建了两个按钮,分别用于
from kivy.app import App
稍微简化您的代码: 从functools导入部分 测试浮点数=浮点数布局 我们想创建两个按钮,触发下拉列表 def selectdrop_按钮,文本,btn: 下拉按钮。文本=文本 创建两个按钮的循环 对于[左,右]中的位置: 触发下拉列表的按钮 dropButton=Buttontext=组+位置,大小提示=0.4,0.3, pos_hint={top:1,position:1} 创建一个下拉列表 下拉菜单 对于范围为10的索引: 创建我们的按钮 btn=Buttontext=position+strindex,size\u hint\u y=None,height=44 将按钮信息发送到下拉选择功能 btn.bindon_release=lambda btn:setattrdropButton,text,btn.text btn.bindon\u release=lambda btn:selectdropButton,btn.text,btn btn.bindon_release=partialselect,dropButton,btn.text 选择后关闭下拉列表 btn.bindon_release=dropdown.disease 向下拉列表中添加按钮 dropdown.add_widgetbn 将按钮绑定到触发下拉列表的函数 dropButton.bindon\u release=dropdown.open 测试\u floatie.add\u widgetdropButton 运行洛杉矶应用程序 类别mainappp: def buildself: 返回试验 MainApp.run 注意使用了partial而不是注释掉的lambda。 看起来他们是同谋,但他们没有。。看 如果在循环中创建回调,并使用循环变量作为回调的参数,则应使用partial。lambda在运行时计算变量,而partial在创建partial函数时计算
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
test_floatie = FloatLayout()
# We wanna create 2 button that trigger a drop down
i = 0
# The loop that creates the two buttons
while i < 2:
# Create a drop down
dropdown = DropDown()
for index in range(10):
# Creating our button
btn = Button(text="Button"+str(index), size_hint_y=None, height=44)
# Sending button info to dropdown select function
btn.bind(on_release=lambda btn: dropdown.select(btn.text))
# Closing drop down after selection
btn.bind(on_release=dropdown.dismiss)
# Adding buttons to the drop down
dropdown.add_widget(btn)
# Simple if statement for positioning
if i == 0:
position = "left"
else:
position = "right"
# The button that triggers the drop down
dropButton = Button(text="Group"+str(i), size_hint=(0.4, 0.3), pos_hint={"top":1, position:1})
print(dropButton)
# Binding the button to a function that triggers the drop down
dropButton.bind(on_release=dropdown.open)
# Using the drop down select function to change the text of the button that triggers the drop down
dropdown.bind(on_select=lambda instance, x: setattr(dropButton, "text", x))
test_floatie.add_widget(dropButton)
i = i +1
# Running la app
class MainApp(App):
def build(self):
return test_floatie
MainApp().run()