在列表中增加列表(封装列表) [英] Incrementing over a list in a list (encapsulated list)
问题描述
我有一些麻烦让我的循环工作,这应该在窗口中创建一些标签。
这里是代码片段:
pre $ t $ t $ t $ t $ t $ t
#Frame 1:组件
frame1 = tk.Frame(root).grid(row = 0)
items = ['REED','PIR','VIBR','REED', 'PIR']
for items in:
tk.Label(frame1,text = items [r])。grid(row = r,column = 0)
#Frame 2:statusList
frame2 = tk.Frame(root).grid(row = 1)
status = [[True,False,True,False],
['14:00:00',' 13:24:31','15:58:32','08:33:01']]
在状态r:
在状态[r]中s:
tk .Label(frame2,text = status [r] [s])。grid(row = s,column = r)
root.mainloop()
$ p它输出错误
TypeError:列表索引必须是整数或切片,不是str
这表明我使用for循环是错误的。我查了一些信息,发现我可以循环遍历一个列表,并通过每个项目,例如打印这个项目,但是至今我不知道如何实现这个想法,当有一个封装列表
<$ p $
tk.Label(frame1,text = items [r])。grid(row = r,column = 0)
这不是python for循环的工作原理。在for循环体中, r
不是项中的项的索引
,它 项目,所以你需要:
项目中的项目:
tk.Label(frame1, text = item).grid(row = ???,column = 0)
还希望该项目的索引作为 .grid()
的行
参数传递。为此,您可以使用 enumerate(seq)
,它为<$ $的每个元素生成一个(index,item)
元组c $ c> seq :
用于索引,枚举(item)中的项目:
tk.Label(frame1,text = item).grid(row = index,column = 0)
请注意,您也有类似的问题在这里:
$ $ p $ status = [[True,False,True,False],
['14:00:00','13:24:31','15:58:32','08:33:01']]
for r in status:
for s在状态[r]:
tk.Label(frame2,text = status [r] [s])。grid(row = s,column = r)
I'm having some troubles getting my for loop working, which is supposed to create a number of labels in a window. Here is the code snippet:
import tkinter as tk
root = tk.Tk()
#Frame 1: components
frame1 = tk.Frame(root).grid(row=0)
items = ['REED', 'PIR', 'VIBR', 'REED', 'PIR']
for r in items:
tk.Label(frame1, text=items[r]).grid(row=r,column=0)
#Frame 2: statusList
frame2 = tk.Frame(root).grid(row=1)
status = [[True, False, True, False],
['14:00:00', '13:24:31', '15:58:32', '08:33:01']]
for r in status:
for s in status[r]:
tk.Label(frame2, text=status[r][s]).grid(row=s,column=r)
root.mainloop()
It outputs the Error
TypeError: list indices must be integers or slices, not str
Which indicated me that my usage of the for-loop is wrong. I've looked up some information and found that I can certainly loop over a list and have it go through each item and for example print that item, but as of yet I don't know how to implement that idea when there's an encapsulated list that I want to loop through.
Here:
for r in items:
tk.Label(frame1, text=items[r]).grid(row=r,column=0)
That's not how python's for loop works. In the for loop's body, r
is not the index of the item in items
, it is the item, so you'd need :
for item in items:
tk.Label(frame1, text=item).grid(row=???,column=0)
but it seems you also want the index of the item to pass as the row
argument for .grid()
. For this you can use enumerate(seq)
which yields a (index, item)
tuple for each element of seq
:
for index, item in enumerate(items):
tk.Label(frame1, text=item).grid(row=index,column=0)
Note that you also have a similar issue here:
status = [[True, False, True, False],
['14:00:00', '13:24:31', '15:58:32', '08:33:01']]
for r in status:
for s in status[r]:
tk.Label(frame2, text=status[r][s]).grid(row=s,column=r)
这篇关于在列表中增加列表(封装列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!