[英]How do I get values of an individual row?
I'm making a program that uses a list and has 2 rows and 5 columns. 我正在制作一个使用列表的程序,有2行5列。 Every element is an entry.
每个元素都是一个条目。 I want to be able to access all the columns of an entire row in the list.
我希望能够访问列表中整行的所有列。
When I access all the elements across all rows at once, it works. 当我一次访问所有行中的所有元素时,它可以工作。 But when I try to get the values of a single row it doesn't work
但是,当我尝试获取单行的值时,它不起作用
This is the code that works: 这是有效的代码:
#Build the grid and append to the listCounter list.
for i in range(2):
for x in range(5):
entry = Entry(frame, text="", width=5)
entry.grid(row=i, column=x)
listCounter.append(entry)
#Get the value of every row
def btnClick():
sum = 0
for puntuation in listCounter:
sum += int(puntuation.get())
print(sum)
But when I try to get all entries of a single row using listCounter[0]
, it doesn't work: 但是当我尝试使用
listCounter[0]
获取单行的所有条目时,它不起作用:
def btnClick():
sum = 0
for puntuation in listCounter[0]:
sum += int(puntuation.get())
print(sum)
And I get the following error: 我收到以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Rober\AppData\Local\Programs\Python\Python37-
32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "D:/PyProjects/TestCanvas/Connect4.py", line 25, in btnClick
for puntuation in listCounter[0]:
File "C:\Users\Rober\AppData\Local\Programs\Python\Python37-
32\lib\tkinter\__init__.py", line 1489, in cget
return self.tk.call(self._w, 'cget', '-' + key)
TypeError: can only concatenate str (not "int") to str
So I've tried to get info about the listCounter
: 所以我试图获取有关
listCounter
信息:
print(type(listCounter)) --> <class 'list'>
print(type(listCounter[0]) --> <class 'tkinter.Entry'>
What exactly is the problem here and how do I solve it? 究竟是什么问题,如何解决?
If you want to create a proper 2D list (a list-of-lists), you need to create the nested elements explicitly: 如果要创建正确的2D列表(列表列表),则需要显式创建嵌套元素:
for i in range(2):
row = []
for x in range(5):
entry = Entry(frame, text="", width=5)
entry.grid(row=i, column=x)
row.append(entry)
listCounter.append(row)
Now you will be able to index listCounter[0]
and get the elements of the first row. 现在,您将能够索引
listCounter[0]
并获取第一行的元素。 Previously, you had listCounter
as a single list of 10 elements, and listCounter[0]
was understandably just an Entry
object. 以前,您将
listCounter
作为10个元素的单个列表,而listCounter[0]
只是一个Entry
对象。
So, looking at this, I think your confusion is of the "type" of listCounter. 所以,看看这个,我认为你的混淆是listCounter的“类型”。 It is not a list of rows, each of which is a list of items (columns).
它不是行列表,每个行都是项目列(列)。 It's a list of each
Entry
, each of which independently controls the row and column it is in. It's like if you took apart a chessboard's rows and put them all next to each other. 它是每个
Entry
的列表,每个Entry
独立地控制它所在的行和列。就像你拆开棋盘的行并将它们全部放在一起。 You can still go through each row independently, it just takes a little work. 您仍然可以独立完成每一行,只需要一点点工作。
It looks like there's 2 rows and 5 columns, so there's 10 entries total. 看起来有2行5列,所以总共有10个条目。 If you try to print out
len(puntuation)
, I'd guess it's 10
. 如果你试图打印
len(puntuation)
,我猜它是10
。 It also looks like it's stored in row-major order . 它看起来也像以行主要顺序存储。 So, the sum of the first row would be:
所以,第一行的总和是:
def btnClick():
sum = 0
for puntuation in listCounter[0:5]:
sum += puntuation.get()
print(sum)
Let me know if that helps! 如果有帮助,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.