Kivy:在 TwoLineListItem 中设置 ID
Kivy: set ID in TwoLineListItem
我有这段代码可以读取我存储中的文件,它 return 在 TwoLineListItem 中。
示例:如果我有 2 个文件,将 return 2 TwoLineListItem。
def listarReceitaDoce(self):
dir1 = '/storage/emulated/0/Receitas/BOLOS E TORTAS DOCES'
os.chdir(dir1)
for file in os.listdir():
if file.endswith('.txt'):
file_path = f"{dir1}/{file}"
with open(file_path, 'r') as f:
lines = f.readlines()
lines = [line.strip() for line in lines]
st_idx = lines.index("receita")
ed_idx = lines.index("ingredientes")
l = lines[st_idx:ed_idx]
self.root.ids.telaselecionada.ids.mostraReceita.add_widget(TwoLineListItem(text = str(l[1]), secondary_text = str(l[3])))
我正在尝试为每个 TwoLineListItem 设置一个 ID,因此当我按下 TwoLineListItem 时,我可以读取带有它们信息的准确文件。
示例:如果我按第一个 TwoLineListItem,它会打开第一个文件。
父级小部件具有 属性 ids
。 属性 是一个字典,包含所有被引用的子项(所有具有 ID 的子项)。
为了将 id
添加到 for
循环中的一堆对象,检查以下示例:
parent = GridLayout(cols=1)
for i in range(10): #Iterate 10 times
btn = Button(text = f'Button {i}') #Create item
parent.add_widget(btn) #Add item
parent.ids["Button"+str(i)] = btn #Add the object to the dict with a key.
print(parent.ids) #This will print all the button's ids
现在,在您的代码中:
i = 1 #Create a counter to reference the objects
for file in os.listdir():
if file.endswith('.txt'):
file_path = f"{dir1}/{file}"
with open(file_path, 'r') as f:
lines = f.readlines()
lines = [line.strip() for line in lines]
st_idx = lines.index("receita")
ed_idx = lines.index("ingredientes")
l = lines[st_idx:ed_idx]
item = TwoLineListItem(text = str(l[1]), secondary_text = str(l[3])) #Create your item
self.root.ids.telaselecionada.ids.mostraReceita.add_widget(item) #Add your widget to parent
self.root.ids.telaselecionada.ids.mostraReceita.ids['item '+str(i)] = item #Create a new key in the dict and givi it the value of your current item
i+=1 #Increment the counter
我有这段代码可以读取我存储中的文件,它 return 在 TwoLineListItem 中。
示例:如果我有 2 个文件,将 return 2 TwoLineListItem。
def listarReceitaDoce(self):
dir1 = '/storage/emulated/0/Receitas/BOLOS E TORTAS DOCES'
os.chdir(dir1)
for file in os.listdir():
if file.endswith('.txt'):
file_path = f"{dir1}/{file}"
with open(file_path, 'r') as f:
lines = f.readlines()
lines = [line.strip() for line in lines]
st_idx = lines.index("receita")
ed_idx = lines.index("ingredientes")
l = lines[st_idx:ed_idx]
self.root.ids.telaselecionada.ids.mostraReceita.add_widget(TwoLineListItem(text = str(l[1]), secondary_text = str(l[3])))
我正在尝试为每个 TwoLineListItem 设置一个 ID,因此当我按下 TwoLineListItem 时,我可以读取带有它们信息的准确文件。
示例:如果我按第一个 TwoLineListItem,它会打开第一个文件。
父级小部件具有 属性 ids
。 属性 是一个字典,包含所有被引用的子项(所有具有 ID 的子项)。
为了将 id
添加到 for
循环中的一堆对象,检查以下示例:
parent = GridLayout(cols=1)
for i in range(10): #Iterate 10 times
btn = Button(text = f'Button {i}') #Create item
parent.add_widget(btn) #Add item
parent.ids["Button"+str(i)] = btn #Add the object to the dict with a key.
print(parent.ids) #This will print all the button's ids
现在,在您的代码中:
i = 1 #Create a counter to reference the objects
for file in os.listdir():
if file.endswith('.txt'):
file_path = f"{dir1}/{file}"
with open(file_path, 'r') as f:
lines = f.readlines()
lines = [line.strip() for line in lines]
st_idx = lines.index("receita")
ed_idx = lines.index("ingredientes")
l = lines[st_idx:ed_idx]
item = TwoLineListItem(text = str(l[1]), secondary_text = str(l[3])) #Create your item
self.root.ids.telaselecionada.ids.mostraReceita.add_widget(item) #Add your widget to parent
self.root.ids.telaselecionada.ids.mostraReceita.ids['item '+str(i)] = item #Create a new key in the dict and givi it the value of your current item
i+=1 #Increment the counter