For loop 如何将弹出窗口链接到从for循环生成的按钮?-基维
我正在尝试制作一个应用程序,根据用户输入的时间生成一个开放餐馆的列表。点击每个餐厅按钮后,用户将看到一个弹出窗口,其中包含与餐厅相关的特定信息 按钮是使用for循环生成的,但是我在使每个弹出标题成为按钮的文本时遇到了问题。到目前为止,我的代码只将弹出标题设置为生成的最后一个按钮的文本For loop 如何将弹出窗口链接到从for循环生成的按钮?-基维,for-loop,kivy,popupwindow,For Loop,Kivy,Popupwindow,我正在尝试制作一个应用程序,根据用户输入的时间生成一个开放餐馆的列表。点击每个餐厅按钮后,用户将看到一个弹出窗口,其中包含与餐厅相关的特定信息 按钮是使用for循环生成的,但是我在使每个弹出标题成为按钮的文本时遇到了问题。到目前为止,我的代码只将弹出标题设置为生成的最后一个按钮的文本 nameres=0 class openedpopup(FloatLayout): #the content of the popup def __init__(self, **kwargs):
nameres=0
class openedpopup(FloatLayout): #the content of the popup
def __init__(self, **kwargs):
super(openedpopup, self).__init__(**kwargs)
self.list_of_openrest()
def list_of_openrest(self):
global restaurants
global nameres
count=0
for key in restaurants:
if restaurants.get(key)[0]=="Open":
openedpopupbut = Button(text=key,...)
openedpopupbut.bind(on_press=self.btn)
self.add_widget(openedpopupbut)
count+=1
nameres=openedpopupbut.text
def btn(self, instance):
global nameres
store_popup_open(nameres)
def store_popup_open(nameres): # to determine the size and formatting of popup
show = storepopupopen() # class containing widgets present in popup
popupWindow = Popup(title= nameres,\
content=show,...)
popupWindow.open()
...
我是一个kivy初学者,不确定如何解决这个问题。我知道在kv文件中使用ID引用变量是很常见的,但由于循环的原因,我不确定它是否适用于我的情况
如果您有任何建议,我将不胜感激。您不需要在kv文件中指定某些内容。所以在代码中这样做是可以的 你试过了吗
def btn(self, instance):
store_popup_open(instance.text)
我得到了错误“AttributeError:'函数'对象没有属性'text'”,我还尝试在方法list\u of openrest
中返回openedpopupbut.text
,然后写入store\u popup\u open(self.list\u of openrest)
。这不像对象类是方法那样有效。谢谢你的建议!