Unable to recieve given number of entry data: TypeError: list indices must be integers or slices, not Entry
Unable to recieve given number of entry data: TypeError: list indices must be integers or slices, not Entry
我编写了一些代码,可以根据数字用户输入自动创建输入框。
当尝试通过常规按钮命令 .get()
来自这些输入框的数据时,出现以下错误:
TypeError: list indices must be integers or slices, not Entry
代码如下:
ply_name = []
ply_θ = []
tk = []
i = 0
for i in name_count:
n_n = (n_count[i]).get()
n.append(n_n)
n_a = (a_count[i]).get()
a.append(n_a)
n_t = (t_count[i]).get()
t.append(n_t)
i += 1
if i == given_count:
break
我知道 given_count 值是正确的,因为它进一步用于将有问题的输入框添加到我的网格系统。
错误似乎出在 i 值上。
无论我如何尝试布局,只要我不能将我作为索引值,我都会得到同样的错误。
如有任何帮助,我们将不胜感激!
要跟踪您的条目,您需要保留参考。
如果你只是存储一个数字或一个字符,它没有任何参考。
这是 x 的参考。
让我们考虑这段代码:
import tkinter as tk
root = tk.Tk()
my_entries = []
for _ in range(5):
x = tk.Entry(root)
x.pack()
my_entries.append(x)
print(x)
root.mainloop()
如果您 运行 此代码将打印以下内容:
.!entry
.!entry2
.!entry3
.!entry4
.!entry5
这些是为您将创建的每个小部件创建的 tkinter 的 ID。
Python 本身通过 python_id 知道这些元素,我们可以打印出来:
print(id(x))
因此,如果我们 运行 带有 id(x) 的代码,则会为我打印出以下内容:
54022032
59532048
59532976
59532144
59533040
因此,您尝试执行的操作的完整示例是:
import tkinter as tk
root = tk.Tk()
my_entries = []
for _ in range(5):
x = tk.Entry(root)
x.pack()
my_entries.append(x)
print(x)
def auto_fill():
for entry in (my_entries):
entry.insert(0, str(entry))
b = tk.Button(root, text='autofill', command=auto_fill)
b.pack()
def pprint():
for entry in my_entries:
print(entry.get())
b2 = tk.Button(root, text='print', command=pprint)
b2.pack()
root.mainloop()
希望对您有所帮助
我编写了一些代码,可以根据数字用户输入自动创建输入框。
当尝试通过常规按钮命令 .get()
来自这些输入框的数据时,出现以下错误:
TypeError: list indices must be integers or slices, not Entry
代码如下:
ply_name = []
ply_θ = []
tk = []
i = 0
for i in name_count:
n_n = (n_count[i]).get()
n.append(n_n)
n_a = (a_count[i]).get()
a.append(n_a)
n_t = (t_count[i]).get()
t.append(n_t)
i += 1
if i == given_count:
break
我知道 given_count 值是正确的,因为它进一步用于将有问题的输入框添加到我的网格系统。
错误似乎出在 i 值上。 无论我如何尝试布局,只要我不能将我作为索引值,我都会得到同样的错误。
如有任何帮助,我们将不胜感激!
要跟踪您的条目,您需要保留参考。 如果你只是存储一个数字或一个字符,它没有任何参考。 这是 x 的参考。
让我们考虑这段代码:
import tkinter as tk
root = tk.Tk()
my_entries = []
for _ in range(5):
x = tk.Entry(root)
x.pack()
my_entries.append(x)
print(x)
root.mainloop()
如果您 运行 此代码将打印以下内容:
.!entry
.!entry2
.!entry3
.!entry4
.!entry5
这些是为您将创建的每个小部件创建的 tkinter 的 ID。 Python 本身通过 python_id 知道这些元素,我们可以打印出来:
print(id(x))
因此,如果我们 运行 带有 id(x) 的代码,则会为我打印出以下内容:
54022032
59532048
59532976
59532144
59533040
因此,您尝试执行的操作的完整示例是:
import tkinter as tk
root = tk.Tk()
my_entries = []
for _ in range(5):
x = tk.Entry(root)
x.pack()
my_entries.append(x)
print(x)
def auto_fill():
for entry in (my_entries):
entry.insert(0, str(entry))
b = tk.Button(root, text='autofill', command=auto_fill)
b.pack()
def pprint():
for entry in my_entries:
print(entry.get())
b2 = tk.Button(root, text='print', command=pprint)
b2.pack()
root.mainloop()
希望对您有所帮助