如何将弹出窗口链接到由for循环生成的按钮? -猕猴桃

我正在尝试制作一个可根据用户输入的时间生成开放餐厅列表的应用。然后,点击每个餐厅按钮,将使用户跳至包含与餐厅相关的特定信息的弹出窗口。

按钮是使用for循环生成的,但是我在使每个弹出式标题成为按钮所来自的文本时遇到问题。到目前为止,我所拥有的代码仅将弹出式标题设置为生成的最后一个按钮的文本。

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()        

...

我是一个初学者,不确定如何解决这个问题。我知道在kv文件中使用id来引用变量是很常见的,但是由于循环,我不确定它是否适用于我的情况。

如果有任何建议,我将不胜感激。

tx5897932 回答:如何将弹出窗口链接到由for循环生成的按钮? -猕猴桃

您无需在kv文件中指定某些内容。因此,在代码中执行此操作是可以的。

您尝试过

def btn(self,instance):
    store_popup_open(instance.text)
本文链接:https://www.f2er.com/3116818.html

大家都在问