我正在尝试制作一个应用程序,根据用户输入的时间生成开放餐厅列表。点击每个餐厅按钮,用户就会看到一个弹出窗口,其中包含与餐厅相关的特定信息。
按钮是使用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引用变量是很常见的,但由于循环的原因,我不确定它是否适用于我的情况。
如有任何建议,我将不胜感激。
您不需要在kv文件中指定某些内容。所以在代码中这样做是可以的
你试过吗
def btn(self, instance):
store_popup_open(instance.text)