访问列表中的 Tkinter 小部件时出现问题

发布于 2024-11-25 13:18:42 字数 771 浏览 1 评论 0原文

我正在尝试使用 Python 和 Tkinter 编写一个小程序,其中有一个标签和条目字段列表(请参见下面的代码)。添加小部件没有问题。但是,当我想使用其中一个实例的方法(例如在一个条目字段上使用 Insert())时,我无法找到一种方法来实现它。

我的代码如下所示:

from Tkinter import *
import random
root = Tk()

attributes = {'Strength':10, 'Dexterity':10, 'Constitution':10, 'Intelligence':10, 'wisdom':10, 'charisma':10}
entries = []
labels = []

i = 0
for a in attributes:
   labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W))
   entries.append(Entry(root).grid(column = 1, row = i))
   i = i+1

root.mainloop()

我尝试了一个简单的

entries[i].insert("text to insert")

e = Entry
e = entries[i]
e.insert...

没有帮助。我见过其他人尝试使用列表中的对象的例子,看起来他们只是像我第一次尝试时那样做。我错过了什么吗?

谢谢

I'm trying to write a small program where I have a list of Labels and Entry-fields using Python and Tkinter (see code below). Adding the widgets is no problem. However, when I want to use a method of one of the instances (like Insert() on one of the Entry-fields) I can't figure out a way to do it.

My code looks like this:

from Tkinter import *
import random
root = Tk()

attributes = {'Strength':10, 'Dexterity':10, 'Constitution':10, 'Intelligence':10, 'wisdom':10, 'charisma':10}
entries = []
labels = []

i = 0
for a in attributes:
   labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W))
   entries.append(Entry(root).grid(column = 1, row = i))
   i = i+1

root.mainloop()

and I have tried a simple

entries[i].insert("text to insert")

and

e = Entry
e = entries[i]
e.insert...

but it hasn't helped. I've seen other examples of people trying to use an object in a list, and it seems they are just doing as I did in my first attempt. Have I missed something?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-12-02 13:18:42

Entry(root).grid() 返回一个 NoneType 对象,因此您在列表中存储的所有内容都是 None。您可以先创建 Entry 小部件,调用 grid(),然后将其附加到列表中。

from Tkinter import *
import random
root = Tk()
attributes = {'Strength':10, 'Dexterity':10, 'Constitution':10, 'Intelligence':10, 'wisdom':10, 'charisma':10}
entries = []
labels = []


for i,a in enumerate(attributes):
   labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W))
   e = Entry(root)
   e.grid(column=1, row=i)
   entries.append(e)
   entries[i].insert(INSERT,"text to insert")



root.mainloop()

Entry(root).grid() returns a NoneType object, so all you are storing in your list is None. You can create the Entry widget first, call grid() and then append it to your list.

from Tkinter import *
import random
root = Tk()
attributes = {'Strength':10, 'Dexterity':10, 'Constitution':10, 'Intelligence':10, 'wisdom':10, 'charisma':10}
entries = []
labels = []


for i,a in enumerate(attributes):
   labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W))
   e = Entry(root)
   e.grid(column=1, row=i)
   entries.append(e)
   entries[i].insert(INSERT,"text to insert")



root.mainloop()
女中豪杰 2024-12-02 13:18:42

我之前刚开始Python的时候也遇到过这个问题。我当时想,“为什么要用两行来创建一些东西并设置网格位置。我会把它们全部放在一行上。”有趣的故事,“.grid”返回 None。所以我认为你实际上并没有在这里添加任何内容。

I ran into this problem before when I started Python. I was like, "Why take up two lines creating something and setting the grid location. I'll put it all on one." Funny story, the '.grid' returns None. So I don't think you're actually appending anything here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文